diff --git a/src/isotp/allocate.h b/src/isotp/allocate.h new file mode 100644 index 0000000..d187a61 --- /dev/null +++ b/src/isotp/allocate.h @@ -0,0 +1,13 @@ +#ifndef __ISOTP_ALLOCATE_H__ +#define __ISOTP_ALLOCATE_H__ + +#include + +/* The implementation of the two functions is platform-specific and is left to + * the user. Implementation used for testing is based on stdlib's malloc/free + * and can be found in tests/common.c */ + +uint8_t* allocate (size_t size); +void free_allocated (uint8_t* data); + +#endif // __ISOTP_ALLOCATE_H_ diff --git a/src/isotp/isotp.h b/src/isotp/isotp.h index 3a3658c..8a09c42 100644 --- a/src/isotp/isotp.h +++ b/src/isotp/isotp.h @@ -6,7 +6,6 @@ #include #include #include -#include #ifdef __cplusplus extern "C" { diff --git a/src/isotp/receive.c b/src/isotp/receive.c index 35b7a2a..4ab90f9 100644 --- a/src/isotp/receive.c +++ b/src/isotp/receive.c @@ -1,8 +1,8 @@ #include +#include #include #include #include -#include #define ARBITRATION_ID_OFFSET 0x8 @@ -111,7 +111,7 @@ IsoTpMessage isotp_continue_receive(IsoTpShims* shims, //messages. That way we don't have to allocate 4k of memory //for each multi-frame response. uint8_t* combined_payload = NULL; - combined_payload = (uint8_t*)malloc(sizeof(uint8_t)*payload_length); + combined_payload = allocate(payload_length); if(combined_payload == NULL) { shims->log("Unable to allocate memory for multi-frame response."); @@ -142,12 +142,12 @@ IsoTpMessage isotp_continue_receive(IsoTpShims* shims, handle->received_buffer_size = start_index + remaining_bytes; if(handle->received_buffer_size != handle->incoming_message_size){ - free(handle->receive_buffer); + free_allocated(handle->receive_buffer); handle->success = false; shims->log("Error capturing all bytes of multi-frame. Freeing memory."); } else { memcpy(message.payload,&handle->receive_buffer[0],handle->incoming_message_size); - free(handle->receive_buffer); + free_allocated(handle->receive_buffer); message.size = handle->incoming_message_size; message.completed = true; shims->log("Successfully captured all of multi-frame. Freeing memory."); diff --git a/tests/common.c b/tests/common.c index a9eed39..801f719 100644 --- a/tests/common.c +++ b/tests/common.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -89,3 +90,10 @@ void setup() { can_frame_was_sent = false; } +uint8_t* allocate(size_t size) { + return (uint8_t*) malloc((sizeof(uint8_t))* size); +} + +void free_allocated(uint8_t* data) { + free(data); +}