diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-01-07 22:07:57 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-01-07 22:07:57 +0100 |
commit | 456efce00c0ed85061352a38edddc0a3f39c7804 (patch) | |
tree | d2d7c0a821b5c9e07d4ca24dd34f48b4038d3ca2 | |
parent | aacaa86e621b18103f5ebf0bfe56f302d258c0e8 (diff) |
change malloc and free to use macro's
-rw-r--r-- | confui/confui.pro | 3 | ||||
-rw-r--r-- | shared/bin.c | 14 | ||||
-rw-r--r-- | shared/bin.h | 5 | ||||
-rw-r--r-- | shared/memory.h | 21 | ||||
-rw-r--r-- | shared/pclient.c | 6 | ||||
-rw-r--r-- | shared/protocol.c | 2 | ||||
-rw-r--r-- | shared/serial_parse.c | 3 |
7 files changed, 41 insertions, 13 deletions
diff --git a/confui/confui.pro b/confui/confui.pro index f73465b..69b35e5 100644 --- a/confui/confui.pro +++ b/confui/confui.pro @@ -36,3 +36,6 @@ CONFIG += force_debug_info QMAKE_CXXFLAGS += -Wno-missing-field-initializers QMAKE_CFLAGS += -std=c11 QMAKE_CFLAGS += -Wno-c99-designator + +DEFINES += CD_MALLOC=malloc +DEFINES += CD_FREE=free diff --git a/shared/bin.c b/shared/bin.c index 875d013..22873b6 100644 --- a/shared/bin.c +++ b/shared/bin.c @@ -1,6 +1,8 @@ #include <stdlib.h> #include <string.h> +#include "memory.h" + #include "bin.h" #ifdef __cplusplus @@ -23,7 +25,7 @@ extern "C" { #pragma GCC diagnostic ignored "-Wshift-count-overflow" cd_s_bin *cd_bin_from_uint8_t(uint8_t data) { size_t size = 1; - cd_s_bin *ret = malloc(sizeof(cd_s_bin) + sizeof(uint8_t) * size); + cd_s_bin *ret = CD_MALLOC(sizeof(cd_s_bin) + sizeof(uint8_t) * size); ret->bytes = size; ret->data[0] = data; return ret; @@ -31,7 +33,7 @@ cd_s_bin *cd_bin_from_uint8_t(uint8_t data) { cd_s_bin *cd_bin_from_uint16_t(uint16_t data) { size_t size = 2; - cd_s_bin *ret = malloc(sizeof(cd_s_bin) + sizeof(uint8_t) * size); + cd_s_bin *ret = CD_MALLOC(sizeof(cd_s_bin) + sizeof(uint8_t) * size); data = cd_bin_hton16(data); ret->bytes = size; ret->data[0] = (data & _BYTE_1) >> _SHIFT_1B; @@ -41,7 +43,7 @@ cd_s_bin *cd_bin_from_uint16_t(uint16_t data) { cd_s_bin *cd_bin_from_uint32_t(uint32_t data) { size_t size = 4; - cd_s_bin *ret = malloc(sizeof(cd_s_bin) + sizeof(uint8_t) * size); + cd_s_bin *ret = CD_MALLOC(sizeof(cd_s_bin) + sizeof(uint8_t) * size); data = cd_bin_hton32(data); ret->bytes = size; ret->data[0] = (data & _BYTE_3) >> _SHIFT_3B; @@ -75,7 +77,7 @@ uint32_t cd_bin_htond(uint8_t* h, size_t s) { } cd_s_bin *cd_bin_s_alloc(uint16_t bytes, uint8_t *data) { - cd_s_bin *temp = malloc(sizeof(cd_s_bin) + sizeof(uint8_t) * bytes); + cd_s_bin *temp = CD_MALLOC(sizeof(cd_s_bin) + sizeof(uint8_t) * bytes); temp->bytes = bytes; memcpy(&temp->data, data, bytes); return temp; @@ -86,8 +88,8 @@ cd_s_bin *cd_bin_s_cat(cd_s_bin *a, cd_s_bin *b) { memcpy(data, a->data, a->bytes); memcpy(data + a->bytes, b->data, b->bytes); cd_s_bin *c = cd_bin_s_alloc(a->bytes + b->bytes, data); - free(a); - free(b); + CD_FREE(a); + CD_FREE(b); return c; } diff --git a/shared/bin.h b/shared/bin.h index 7506655..4a932c2 100644 --- a/shared/bin.h +++ b/shared/bin.h @@ -10,7 +10,8 @@ */ #include <stdint.h> -#include <malloc.h> + +#include "memory.h" #ifdef __cplusplus extern "C" { @@ -23,7 +24,7 @@ extern uint8_t g_cd_endianness; #define CD_CREATE_MSG_BIN(type, normal, bin) CD_CREATE_MSG_SIZE_BIN(type, sizeof(type), normal, bin) /** @brief */ #define CD_CREATE_MSG_SIZE_BIN(type, size, normal, bin) \ - cd_s_bin *bin = malloc(sizeof(cd_s_bin) + size); \ + cd_s_bin *bin = CD_MALLOC(sizeof(cd_s_bin) + size); \ bin->bytes = size; \ type *normal = (type *)&bin->data; diff --git a/shared/memory.h b/shared/memory.h new file mode 100644 index 0000000..236f68e --- /dev/null +++ b/shared/memory.h @@ -0,0 +1,21 @@ +#pragma once + +/** + * @file memory.h macro's for malloc() and free() + * + * these are usually defined in clib's stdlib.h, but zephyr requires the use of + * the k_malloc and k_free functions for memory management, thus this file is + * used to set aliases for each respecive platform's native memory management + * functions as they have the same function signature. these macro's should be + * defined using a compiler flag + */ + +#ifndef CD_MALLOC +#warning CD_MALLOC is not defined, please update build flags to add -DCD_MALLOC <implementation> +#define CD_MALLOC(n) 0 +#endif + +#ifndef CD_FREE +#warning CD_FREE is not defined, please update build flags to add -DCD_FREE <implementation> +#define CD_FREE(n) ((void)(0)) +#endif diff --git a/shared/pclient.c b/shared/pclient.c index 1b8e4e5..a7d9b00 100644 --- a/shared/pclient.c +++ b/shared/pclient.c @@ -1,4 +1,4 @@ -#include <memory.h> +#include <string.h> #include "protocol.h" #include "pclient.h" @@ -119,7 +119,7 @@ cd_s_cmd_node* cd_cmd_node_alloc(const char* name, cd_s_cmd_node base, uint16_t size_t name_len = strlen(name); size_t links_size = sizeof(cd_uuid_t) * link_count; size_t remaining_size = sizeof(char) * name_len + links_size; - cd_s_cmd_node* node = malloc(sizeof(cd_s_cmd_node) + remaining_size); + cd_s_cmd_node* node = CD_MALLOC(sizeof(cd_s_cmd_node) + remaining_size); memcpy(node->uuid, base.uuid, sizeof(cd_uuid_t)); memcpy(node->address, base.address, sizeof(cd_mac_addr_t)); @@ -144,7 +144,7 @@ cd_s_cmd_response_get_node* cd_cmd_get_node_res_from_node_arr(uint16_t size, cd_ remaining_size += sizeof(cd_s_cmd_node) + cd_bin_ntoh16(arr[i]->remaining_size); } - cd_s_cmd_response_get_node* response = malloc(sizeof(cd_s_cmd_response_get_node) + remaining_size); + cd_s_cmd_response_get_node* response = CD_MALLOC(sizeof(cd_s_cmd_response_get_node) + remaining_size); response->node_count = cd_bin_hton16(size); response->remaining_size = cd_bin_hton16(remaining_size); diff --git a/shared/protocol.c b/shared/protocol.c index fcc0f41..943604c 100644 --- a/shared/protocol.c +++ b/shared/protocol.c @@ -15,7 +15,7 @@ size_t cd_cmd_sizeof(uint8_t data[CD_SERIAL_READ_BUFFER_SIZE], uint8_t data_leng cd_s_bin *copy = cd_bin_s_alloc(data_length, data); size_t length = (*CD_CMD_HANDLERS_SIZEOF[opcode])(copy); - free(copy); + CD_FREE(copy); return length; } diff --git a/shared/serial_parse.c b/shared/serial_parse.c index bfc374a..1397918 100644 --- a/shared/serial_parse.c +++ b/shared/serial_parse.c @@ -1,4 +1,5 @@ #include <string.h> +#include <memory.h> #include "consts.h" #include "serial_parse.h" @@ -52,7 +53,7 @@ void cd_cmd_handle(uint8_t data[CD_SERIAL_READ_BUFFER_SIZE], uint8_t data_length if (handler == NULL) return; (*handler)(copy); - free(copy); + CD_FREE(copy); } #ifdef __cplusplus |