From abd0a027db557331afb843b0456b0eb44b8d25b1 Mon Sep 17 00:00:00 2001 From: "@pawel.szwarnowski" Date: Wed, 28 Aug 2024 12:09:58 +0000 Subject: [PATCH] Use Zephyr heap for Zephyr targets --- src/coap_mem.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/src/coap_mem.c b/src/coap_mem.c index bdd7095295..8e92751242 100644 --- a/src/coap_mem.c +++ b/src/coap_mem.c @@ -480,7 +480,65 @@ coap_realloc_type(coap_memory_tag_t type, void *p, size_t size) { } #else /* ! RIOT_VERSION && ! MODULE_MEMARRAY */ -#if defined(HAVE_MALLOC) || defined(__MINGW32__) +#if defined(__ZEPHYR__) + +#include + +void +coap_memory_init(void) { +} + +void * +coap_malloc_type(coap_memory_tag_t type, size_t size) { + void *ptr; + + (void)type; + ptr = k_malloc(size); +#if COAP_MEMORY_TYPE_TRACK + assert(type < COAP_MEM_TAG_LAST); + if (ptr) { + track_counts[type]++; + if (track_counts[type] > peak_counts[type]) + peak_counts[type] = track_counts[type]; + } else { + fail_counts[type]++; + } +#endif /* COAP_MEMORY_TYPE_TRACK */ + return ptr; +} + +void * +coap_realloc_type(coap_memory_tag_t type, void *p, size_t size) { + void *ptr; + + (void)type; + ptr = k_realloc(p, size); +#if COAP_MEMORY_TYPE_TRACK + if (ptr) { + assert(type < COAP_MEM_TAG_LAST); + if (!p) + track_counts[type]++; + if (track_counts[type] > peak_counts[type]) + peak_counts[type] = track_counts[type]; + } else { + fail_counts[type]++; + } +#endif /* COAP_MEMORY_TYPE_TRACK */ + return ptr; +} + +void +coap_free_type(coap_memory_tag_t type, void *p) { + (void)type; +#if COAP_MEMORY_TYPE_TRACK + assert(type < COAP_MEM_TAG_LAST); + if (p) + track_counts[type]--; +#endif /* COAP_MEMORY_TYPE_TRACK */ + k_free(p); +} + +#elif defined(HAVE_MALLOC) || defined(__MINGW32__) #include void @@ -537,7 +595,7 @@ coap_free_type(coap_memory_tag_t type, void *p) { free(p); } -#else /* ! HAVE_MALLOC && !__MINGW32__ */ +#else /* ! HAVE_MALLOC && !__MINGW32__ && !__ZEPHYR__*/ #ifdef WITH_CONTIKI #include "lib/heapmem.h"