From 7370fcf6ad42dd0753b432c2eebf110cc8c6d040 Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Fri, 22 May 2026 16:08:50 +0100 Subject: [PATCH] LwIP: Add in support for MEM_USE_POOLS for COAP_STRING malloc()s If both MEMP_USE_CUSTOM_POOLS and MEM_USE_POOLS are defined in lwipopts.h and there is a suitable LWIP_MALLOC_MEMPOOL_START .. LWIP_MALLOC_MEMPOOL_END definition in lwippools.h, then any coap_malloc_type(COAP_STRING, ..) will make use of the defined pools. A new src/coap_mem_lwip.c file has been added. --- Makefile.am | 4 + Makefile.libcoap | 1 + examples/lwip/Makefile | 1 + examples/lwip/client-coap.c | 40 ++++- examples/lwip/config/lwipopts.h | 12 +- examples/lwip/config/lwippools.h | 43 ++++-- include/coap3/coap_mem.h | 70 +++++++-- src/coap_dgrm_lwip.c | 6 +- src/coap_io_lwip.c | 26 ---- src/coap_mem.c | 89 +---------- src/coap_mem_lwip.c | 246 +++++++++++++++++++++++++++++++ src/coap_str.c | 2 +- 12 files changed, 387 insertions(+), 153 deletions(-) create mode 100644 src/coap_mem_lwip.c diff --git a/Makefile.am b/Makefile.am index 1609da5a..6b7e4c97 100644 --- a/Makefile.am +++ b/Makefile.am @@ -137,6 +137,7 @@ EXTRA_DIST = \ src/coap_dgrm_contiki.c \ src/coap_dgrm_lwip.c \ src/coap_dgrm_riot.c \ + src/coap_mem_lwip.c \ src/coap_strm_contiki.c \ src/coap_strm_lwip.c \ src/coap_strm_riot.c \ @@ -295,6 +296,9 @@ coap_pdu_from_pbuf \ coap_lwip_dump_memory_pools \ coap_lwip_set_input_wait_handler \ coap_print_contiki_prefix \ +coap_free_type_string \ +coap_malloc_type_string \ +coap_realloc_type_string \ " # This helper is called by libcoap-$(LIBCOAP_API_VERSION).{map,sym} to see if diff --git a/Makefile.libcoap b/Makefile.libcoap index 4fa431e4..d6218792 100644 --- a/Makefile.libcoap +++ b/Makefile.libcoap @@ -7,6 +7,7 @@ MODULES_SOURCES_EXCLUDES += coap_dgrm_riot.c MODULES_SOURCES_EXCLUDES += coap_io_lwip.c MODULES_SOURCES_EXCLUDES += coap_strm_lwip.c MODULES_SOURCES_EXCLUDES += coap_dgrm_lwip.c +MODULES_SOURCES_EXCLUDES += coap_mem_lwip.c MODULES_SOURCES_EXCLUDES += coap_io_posix.c MODULES_SOURCES_EXCLUDES += coap_strm_posix.c MODULES_SOURCES_EXCLUDES += coap_dgrm_posix.c diff --git a/examples/lwip/Makefile b/examples/lwip/Makefile index 026a26a6..15d861b0 100644 --- a/examples/lwip/Makefile +++ b/examples/lwip/Makefile @@ -161,6 +161,7 @@ COAP_SRC = coap_address.c \ coap_io_lwip.c \ coap_layers.c \ coap_mem.c \ + coap_mem_lwip.c \ coap_net.c \ coap_netif.c \ coap_notls.c \ diff --git a/examples/lwip/client-coap.c b/examples/lwip/client-coap.c index 15935bd0..d78e8f29 100644 --- a/examples/lwip/client-coap.c +++ b/examples/lwip/client-coap.c @@ -155,17 +155,26 @@ client_coap_init(coap_lwip_input_wait_handler_t input_wait, void *input_arg, /* Parse the URI */ len = coap_split_uri((const unsigned char *)use_uri, strlen(use_uri), &uri); - LWIP_ASSERT("Failed to parse uri", len == 0); + if (len != 0) { + coap_log_err("Failed to parse uri '%s'\n", use_uri); + return; + } snprintf(portbuf, sizeof(portbuf), "%d", uri.port); snprintf((char *)buf, sizeof(buf), "%*.*s", (int)uri.host.length, (int)uri.host.length, (const char *)uri.host.s); /* resolve destination address where server should be sent */ len = resolve_address((const char *)buf, portbuf, &dst, &proto, 1 << uri.scheme); - LWIP_ASSERT("Failed to resolve address", len > 0); + if (len < 1) { + coap_log_err("Failed to resolve address '%s'\n", buf); + return; + } main_coap_context = coap_new_context(NULL); - LWIP_ASSERT("Failed to initialize context", main_coap_context != NULL); + if (main_coap_context == NULL) { + coap_log_err("Failed to initialize context\n"); + return; + } coap_context_set_block_mode(main_coap_context, COAP_BLOCK_USE_LIBCOAP); #if NO_SYS @@ -197,7 +206,10 @@ client_coap_init(coap_lwip_input_wait_handler_t input_wait, void *input_arg, proto, NULL, NULL, NULL); } - LWIP_ASSERT("Failed to create session", session != NULL); + if (session == NULL) { + coap_log_err("Failed to create session\n"); + return; + } if (proto == COAP_PROTO_WS || proto == COAP_PROTO_WSS) { coap_ws_set_host_request(session, &uri.host); @@ -211,20 +223,32 @@ client_coap_init(coap_lwip_input_wait_handler_t input_wait, void *input_arg, COAP_REQUEST_CODE_GET, coap_new_message_id(session), coap_session_max_pdu_size(session)); - LWIP_ASSERT("Failed to create PDU", pdu != NULL); + if (pdu == NULL) { + coap_log_err("Failed to create PDU\n"); + return; + } res = coap_uri_into_optlist(&uri, &dst, &optlist, 1); - LWIP_ASSERT("Failed to create options", res == 1); + if (res != 1) { + coap_log_err("Failed to create options\n"); + return; + } /* Add option list (which will be sorted) to the PDU */ if (optlist) { res = coap_add_optlist_pdu(pdu, &optlist); - LWIP_ASSERT("Failed to add options to PDU", res == 1); + if (res != 1) { + coap_log_err("Failed to add options to PDU\n"); + return; + } } /* and send the PDU */ mid = coap_send(session, pdu); - LWIP_ASSERT("Failed to send PDU", mid != COAP_INVALID_MID); + if (mid == COAP_INVALID_MID) { + coap_log_err("Failed to send PDU\n"); + return; + } } void diff --git a/examples/lwip/config/lwipopts.h b/examples/lwip/config/lwipopts.h index d9a33378..c0646d2c 100644 --- a/examples/lwip/config/lwipopts.h +++ b/examples/lwip/config/lwipopts.h @@ -71,9 +71,19 @@ #define MEMP_USE_CUSTOM_POOLS 1 #define MEM_SIZE (4 * 1024) +#if MEMP_USE_CUSTOM_POOLS +/* + * Set to 0 if defined memory pools is not required, only custom pools. + * Memory pools are only used for COAP_SRING type mmemory allocations. + * + * [Update the LWIP_MALLOC_MEMPOOL definitions in lwippools.h.] + */ +#define MEM_USE_POOLS 1 +#endif + #if ! MEMP_USE_CUSTOM_POOLS /* Set if you want to use the standard libc for malloc */ -#define MEM_LIBC_MALLOC 0 +#define MEM_LIBC_MALLOC 1 #endif #if MEM_LIBC_MALLOC diff --git a/examples/lwip/config/lwippools.h b/examples/lwip/config/lwippools.h index fd5a3e5f..01598a90 100644 --- a/examples/lwip/config/lwippools.h +++ b/examples/lwip/config/lwippools.h @@ -108,18 +108,6 @@ typedef struct l_coap_tiny_context_t { #define MEMP_NUM_COAPSTRING 20 #endif -#ifndef MEMP_LEN_COAPSTRING -#ifdef COAP_WS_SUPPORT -#define MEMP_LEN_COAPSTRING 176 -#else /* ! COAP_WS_SUPPORT */ -#ifdef COAP_WITH_LIBTINYDTLS -#define MEMP_LEN_COAPSTRING 120 -#else /* COAP_WITH_TINYDTLS */ -#define MEMP_LEN_COAPSTRING 40 -#endif /* COAP_WITH_TINYDTLS */ -#endif /* ! COAP_WS_SUPPORT */ -#endif - #ifndef MEMP_NUM_COAPCACHE_KEYS #define MEMP_NUM_COAPCACHE_KEYS (2U) #endif /* MEMP_NUM_COAPCACHE_KEYS */ @@ -160,6 +148,35 @@ typedef struct l_coap_tiny_context_t { #define MEMP_NUM_COAPDTLS_CONTEXT 1 #endif +#if MEM_USE_POOLS + +/* Note: libcoap uses 8 byte allocation from defined value for tracking alloc sizes */ +LWIP_MALLOC_MEMPOOL_START +LWIP_MALLOC_MEMPOOL(MEMP_NUM_COAPSTRING, 64) +LWIP_MALLOC_MEMPOOL(4, 256) +LWIP_MALLOC_MEMPOOL(2, 512) +LWIP_MALLOC_MEMPOOL(2, 1512) +LWIP_MALLOC_MEMPOOL_END +LWIP_MEMPOOL(COAP_STRING, 1, 1, "COAP_STRING") + +#else /* ! MEM_USE_POOLS */ + +#ifndef MEMP_LEN_COAPSTRING +#ifdef COAP_WS_SUPPORT +#define MEMP_LEN_COAPSTRING 176 +#else /* ! COAP_WS_SUPPORT */ +#ifdef COAP_WITH_LIBTINYDTLS +#define MEMP_LEN_COAPSTRING 120 +#else /* COAP_WITH_TINYDTLS */ +#define MEMP_LEN_COAPSTRING 40 +#endif /* COAP_WITH_TINYDTLS */ +#endif /* ! COAP_WS_SUPPORT */ +#endif + +LWIP_MEMPOOL(COAP_STRING, MEMP_NUM_COAPSTRING, sizeof(coap_string_t)+MEMP_LEN_COAPSTRING, + "COAP_STRING") +#endif /* ! MEM_USE_POOLS */ + LWIP_MEMPOOL(COAP_CONTEXT, MEMP_NUM_COAPCONTEXT, sizeof(coap_context_t), "COAP_CONTEXT") #ifdef COAP_SERVER_SUPPORT LWIP_MEMPOOL(COAP_ENDPOINT, MEMP_NUM_COAPENDPOINT, sizeof(coap_endpoint_t), "COAP_ENDPOINT") @@ -176,8 +193,6 @@ LWIP_MEMPOOL(COAP_RESOURCEATTR, MEMP_NUM_COAPRESOURCEATTR, sizeof(coap_attr_t), #endif /* COAP_SERVER_SUPPORT */ LWIP_MEMPOOL(COAP_OPTLIST, MEMP_NUM_COAPOPTLIST, sizeof(coap_optlist_t)+MEMP_LEN_COAPOPTLIST, "COAP_OPTLIST") -LWIP_MEMPOOL(COAP_STRING, MEMP_NUM_COAPSTRING, sizeof(coap_string_t)+MEMP_LEN_COAPSTRING, - "COAP_STRING") #ifdef COAP_SERVER_SUPPORT LWIP_MEMPOOL(COAP_CACHE_KEY, MEMP_NUM_COAPCACHE_KEYS, sizeof(coap_cache_key_t), "COAP_CACHE_KEY") LWIP_MEMPOOL(COAP_CACHE_ENTRY, MEMP_NUM_COAPCACHE_ENTRIES, sizeof(coap_cache_entry_t), diff --git a/include/coap3/coap_mem.h b/include/coap3/coap_mem.h index cd125121..b8648769 100644 --- a/include/coap3/coap_mem.h +++ b/include/coap3/coap_mem.h @@ -62,7 +62,16 @@ typedef enum { COAP_MEM_TAG_LAST } coap_memory_tag_t; -#if ! defined(WITH_LWIP) || (defined(WITH_LWIP) && ! MEMP_USE_CUSTOM_POOLS) +/** + * Dumps the current usage of malloc'd memory types. + * + * Requires COAP_MEMORY_TYPE_TRACK to be defined to 1. + * + * @param log_level The logging level to use. + */ +void coap_dump_memory_type_counts(coap_log_t log_level); + +#if ! defined(WITH_LWIP) || (defined(WITH_LWIP) && ! MEMP_USE_CUSTOM_POOLS && ! MEM_USE_POOLS) /** * Initializes libcoap's memory management. @@ -109,15 +118,6 @@ void *coap_realloc_type(coap_memory_tag_t type, void *p, size_t size); */ void coap_free_type(coap_memory_tag_t type, void *p); -/** - * Dumps the current usage of malloc'd memory types. - * - * Requires COAP_MEMORY_TYPE_TRACK to be defined to 1. - * - * @param log_level The logging level to use. - */ -void coap_dump_memory_type_counts(coap_log_t log_level); - /** * Wrapper function to coap_malloc_type() for backwards compatibility. */ @@ -134,7 +134,7 @@ coap_free(void *object) { coap_free_type(COAP_STRING, object); } -#else /* WITH_LWIP && MEMP_USE_CUSTOM_POOLS */ +#else /* WITH_LWIP && (MEMP_USE_CUSTOM_POOLS || MEM_USE_POOLS) */ #include @@ -150,24 +150,64 @@ coap_malloc_error(uint16_t *err) { return NULL; } #endif /* MEMP_STATS */ -/* It would be nice to check that size equals the size given at the memp - * declaration, but i currently don't see a standard way to check that without +/* + * It would be nice to check that size equals the size given at the memp + * declaration, but I currently don't see a standard way to check that without * sourcing the custom memp pools and becoming dependent of its syntax */ +#if MEM_USE_POOLS + +void *coap_malloc_type_string(size_t size); + +void *coap_realloc_type_string(void *p, size_t size); + +void coap_free_type_string(void *p); + #if MEMP_STATS + +#define coap_malloc_type(type, asize) \ + (type == COAP_STRING) ? coap_malloc_type_string(asize) : \ + (((asize) <= memp_pools[MEMP_ ## type]->size) ? \ + memp_malloc(MEMP_ ## type) : coap_malloc_error(&memp_pools[MEMP_ ## type]->stats->err)) + +#else /* ! MEMP_STATS */ + +#define coap_malloc_type(type, asize) \ + (type == COAP_STRING) ? coap_malloc_type_string(asize) : \ + (((asize) <= memp_pools[MEMP_ ## type]->size) ? \ + memp_malloc(MEMP_ ## type) : NULL) +#endif /* ! MEMP_STATS */ + +#define coap_free_type(type, p) \ + (type == COAP_STRING) ? coap_free_type_string(p) : \ + memp_free(MEMP_ ## type, p) + +/* As these are fixed size, return value if already defined */ +#define coap_realloc_type(type, p, asize) \ + (type == COAP_STRING) ? coap_realloc_type_string(p, asize) : \ + ((p) ? ((asize) <= memp_pools[MEMP_ ## type]->size) ? (p) : NULL : coap_malloc_type(type, asize)) + +#else /* ! MEM_USE_POOLS */ + +#if MEMP_STATS + #define coap_malloc_type(type, asize) \ (((asize) <= memp_pools[MEMP_ ## type]->size) ? \ memp_malloc(MEMP_ ## type) : coap_malloc_error(&memp_pools[MEMP_ ## type]->stats->err)) + #else /* ! MEMP_STATS */ + #define coap_malloc_type(type, asize) \ (((asize) <= memp_pools[MEMP_ ## type]->size) ? \ memp_malloc(MEMP_ ## type) : NULL) #endif /* ! MEMP_STATS */ + #define coap_free_type(type, p) memp_free(MEMP_ ## type, p) /* As these are fixed size, return value if already defined */ #define coap_realloc_type(type, p, asize) \ ((p) ? ((asize) <= memp_pools[MEMP_ ## type]->size) ? (p) : NULL : coap_malloc_type(type, asize)) +#endif /* MEM_USE_POOLS */ /* Those are just here to make uri.c happy where string allocation has not been * made conditional. @@ -185,9 +225,7 @@ coap_free(void *pointer) { LWIP_ASSERT("coap_free must not be used in lwIP", 0); } -#define coap_dump_memory_type_counts(l) coap_lwip_dump_memory_pools(l) - -#endif /* WITH_LWIP && MEMP_USE_CUSTOM_POOLS */ +#endif /* WITH_LWIP && (MEMP_USE_CUSTOM_POOLS || MEM_USE_POOLS) */ #ifdef __cplusplus } diff --git a/src/coap_dgrm_lwip.c b/src/coap_dgrm_lwip.c index 266abc32..61a1e115 100644 --- a/src/coap_dgrm_lwip.c +++ b/src/coap_dgrm_lwip.c @@ -491,14 +491,15 @@ coap_socket_dgrm_close(coap_socket_t *sock) { struct udp_pcb *udp_pcb = sock->udp_pcb; sock->udp_pcb = NULL; + sock->flags = COAP_SOCKET_EMPTY; if (udp_pcb) { #if NO_SYS == 0 err_t err; if (coap_lwip_in_call_back_ref == 0) { - err = tcpip_try_callback(coap_lwip_udp_remove, udp_pcb); + err = tcpip_callback_wait(coap_lwip_udp_remove, udp_pcb); if (err < 0) { - coap_log_warn("** %s: tcpip_try_callback: error %d\n", + coap_log_warn("** %s: tcpip_callback_wait: error %d\n", sock->session ? coap_session_str(sock->session) : "", err); errno = EAGAIN; return; @@ -510,7 +511,6 @@ coap_socket_dgrm_close(coap_socket_t *sock) { } #endif /* NO_SYS == 0 */ } - sock->flags = COAP_SOCKET_EMPTY; } return; } diff --git a/src/coap_io_lwip.c b/src/coap_io_lwip.c index 51027a9f..1cde3d27 100644 --- a/src/coap_io_lwip.c +++ b/src/coap_io_lwip.c @@ -30,32 +30,6 @@ uint32_t coap_lwip_in_call_back_ref = 0; /* Used if in LwIP call back, under thread safe libcoap code */ #endif /* NO_SYS == 0 */ -void -coap_lwip_dump_memory_pools(coap_log_t log_level) { -#if MEMP_STATS && LWIP_STATS_DISPLAY && MEMP_USE_CUSTOM_POOLS - int i; - - /* Save time if not needed */ - if (log_level > coap_get_log_level()) - return; - - coap_log(log_level, "* LwIP custom memory pools information\n"); - /* - * Make sure LwIP and libcoap have been built with the same - * -DCOAP_CLIENT_ONLY or -DCOAP_SERVER_ONLY options for - * MEMP_MAX to be correct. - */ - for (i = 0; i < MEMP_MAX; i++) { - coap_log(log_level, "* %-17s avail %3d in-use %3d peak %3d failed %3d\n", - memp_pools[i]->stats->name, memp_pools[i]->stats->avail, - memp_pools[i]->stats->used, memp_pools[i]->stats->max, - memp_pools[i]->stats->err); - } -#else /* !( MEMP_STATS && LWIP_STATS_DISPLAY && MEMP_USE_CUSTOM_POOLS) */ - (void)log_level; -#endif /* !( MEMP_STATS && LWIP_STATS_DISPLAY && MEMP_USE_CUSTOM_POOLS) */ -} - void coap_lwip_set_input_wait_handler(coap_context_t *context, coap_lwip_input_wait_handler_t handler, diff --git a/src/coap_mem.c b/src/coap_mem.c index f7efa2f6..cbacde1b 100644 --- a/src/coap_mem.c +++ b/src/coap_mem.c @@ -15,13 +15,13 @@ #include "coap3/coap_libcoap_build.h" -#if ! (defined(WITH_LWIP) && MEMP_USE_CUSTOM_POOLS) +#if ! (defined(WITH_LWIP) && ! MEM_LIBC_MALLOC) #if COAP_MEMORY_TYPE_TRACK static int track_counts[COAP_MEM_TAG_LAST]; static int peak_counts[COAP_MEM_TAG_LAST]; static int fail_counts[COAP_MEM_TAG_LAST]; #endif /* COAP_MEMORY_TYPE_TRACK */ -#endif /* ! (WITH_LWIP && MEMP_USE_CUSTOM_POOLS) */ +#endif /* ! (WITH_LWIP && ! MEM_LIBC_MALLOC) */ #if defined(RIOT_VERSION) && defined(MODULE_MEMARRAY) #include @@ -546,87 +546,8 @@ coap_free_type(coap_memory_tag_t type, void *p) { } /* End __ZEPHYR__ */ -#elif defined(WITH_LWIP) && ! MEMP_USE_CUSTOM_POOLS && ! MEM_LIBC_MALLOC -#include - -void -coap_memory_init(void) { -} - -void * -coap_malloc_type(coap_memory_tag_t type, size_t size) { - void *ptr = mem_malloc(size + sizeof(size_t)); - - (void)type; -#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 */ - if (ptr) { - size_t *s_ptr = (size_t *)ptr; - u_char *b_ptr = (u_char *)ptr; - - *s_ptr = size; - return b_ptr + sizeof(size_t); - } - return NULL; -} - -void * -coap_realloc_type(coap_memory_tag_t type, void *p, size_t size) { - void *new = mem_malloc(size + sizeof(size_t)); - - (void)type; -#if COAP_MEMORY_TYPE_TRACK - if (new) { - 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 */ - if (new) { - size_t *s_ptr = (size_t *)new; - u_char *b_ptr = (u_char *)new; - - *s_ptr = size; - if (p) { - size_t *o_ptr = (size_t *)p; - - o_ptr--; - memcpy(b_ptr + sizeof(size_t), p, *o_ptr); - } - return b_ptr + sizeof(size_t); - } - return NULL; -} - -void -coap_free_type(coap_memory_tag_t type, void *p) { - u_char *ptr = (u_char *)p; - - (void)type; -#if COAP_MEMORY_TYPE_TRACK - assert(type < COAP_MEM_TAG_LAST); - if (p) - track_counts[type]--; -#endif /* COAP_MEMORY_TYPE_TRACK */ - if (ptr) { - mem_free(ptr - sizeof(size_t)); - } -} -/* Enf of WITH_LWIP && ! MEMP_USE_CUSTOM_POOLS && | MEM_LIBC_MALLOC */ - #elif defined(HAVE_MALLOC) || defined(__MINGW32__) + #include void @@ -739,7 +660,7 @@ coap_free_type(coap_memory_tag_t type, void *ptr) { #endif /* WITH_CONTIKI */ -#if ! (defined(WITH_LWIP) && MEMP_USE_CUSTOM_POOLS) +#if ! (defined(WITH_LWIP) && ! MEM_LIBC_MALLOC) #define MAKE_CASE(n) case n: name = #n; break void coap_dump_memory_type_counts(coap_log_t level) { @@ -792,4 +713,4 @@ coap_dump_memory_type_counts(coap_log_t level) { (void)level; #endif /* COAP_MEMORY_TYPE_TRACK */ } -#endif /* ! (WITH_LWIP && MEMP_USE_CUSTOM_POOLS */ +#endif /* ! (WITH_LWIP && ! MEM_LIBC_MALLOC) */ diff --git a/src/coap_mem_lwip.c b/src/coap_mem_lwip.c new file mode 100644 index 00000000..a17ff3be --- /dev/null +++ b/src/coap_mem_lwip.c @@ -0,0 +1,246 @@ +/* + * Copyright (C) 2012,2014 Olaf Bergmann + * 2014 chrysn + * 2022-2026 Jon Shallow + * + * SPDX-License-Identifier: BSD-2-Clause + * + * This file is part of the CoAP library libcoap. Please see + * README for terms of use. + */ + +/** + * @file coap_dgrm_lwip.c + * @brief LwIP Datagram (UDP) specific functions + */ + +#include "coap3/coap_libcoap_build.h" + +#if defined(WITH_LWIP) && MEMP_USE_CUSTOM_POOLS + +#if MEM_USE_POOLS + +/* Need to handle COAP_STRING separately */ + +void * +coap_malloc_type_string(size_t size) { + void *ptr = mem_malloc(size + sizeof(size_t)); + + if (ptr) { + unsigned int *s_ptr = (unsigned int *)ptr; + u_char *b_ptr = (u_char *)ptr; + + *s_ptr = size; + return b_ptr + sizeof(unsigned int); + } + return NULL; +} + +void * +coap_realloc_type_string(void *p, size_t size) { + void *new; + unsigned int *s_ptr = (unsigned int *)p; + + if (*s_ptr <= size) + return p; + + new = mem_malloc(size + sizeof(unsigned int)); + + if (new) { + u_char *b_ptr = (u_char *)new; + + s_ptr = (unsigned int *)new; + *s_ptr = size; + if (p) { + unsigned int *o_ptr = (unsigned int *)p; + + o_ptr--; + memcpy(b_ptr + sizeof(unsigned int), p, *o_ptr); + coap_free_type_string(o_ptr); + } + return b_ptr + sizeof(unsigned int); + } + return NULL; +} + +void +coap_free_type_string(void *p) { + u_char *ptr = (u_char *)p; + + if (ptr) { + mem_free(ptr - sizeof(unsigned int)); + } +} + +#endif + +void +coap_dump_memory_type_counts(coap_log_t log_level) { +#if MEMP_STATS && LWIP_STATS_DISPLAY && MEMP_USE_CUSTOM_POOLS + int i; + + /* Save time if not needed */ + if (log_level > coap_get_log_level()) + return; + + coap_log(log_level, "* LwIP custom memory pools information\n"); + /* + * Make sure LwIP and libcoap have been built with the same + * -DCOAP_CLIENT_ONLY or -DCOAP_SERVER_ONLY options for + * MEMP_MAX to be correct. + */ + for (i = 0; i < MEMP_MAX; i++) { +#if MEM_USE_POOLS + if (!strcmp("COAP_STRING", memp_pools[i]->stats->name)) + continue; +#endif /* MEM_USE_POOLS */ + coap_log(log_level, "* %-17s avail %3d in-use %3d peak %3d failed %3d\n", + memp_pools[i]->stats->name, memp_pools[i]->stats->avail, + memp_pools[i]->stats->used, memp_pools[i]->stats->max, + memp_pools[i]->stats->err); + } +#else /* !( MEMP_STATS && LWIP_STATS_DISPLAY && MEMP_USE_CUSTOM_POOLS) */ + (void)log_level; +#endif /* !( MEMP_STATS && LWIP_STATS_DISPLAY && MEMP_USE_CUSTOM_POOLS) */ +} + +#elif defined(WITH_LWIP) && ! MEMP_USE_CUSTOM_POOLS && ! MEM_LIBC_MALLOC + +#include + +#if COAP_MEMORY_TYPE_TRACK +static int track_counts[COAP_MEM_TAG_LAST]; +static int peak_counts[COAP_MEM_TAG_LAST]; +static int fail_counts[COAP_MEM_TAG_LAST]; +#endif /* COAP_MEMORY_TYPE_TRACK */ + +void +coap_memory_init(void) { +} + +void * +coap_malloc_type(coap_memory_tag_t type, size_t size) { + void *ptr = mem_malloc(size + sizeof(size_t)); + + (void)type; +#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 */ + if (ptr) { + size_t *s_ptr = (size_t *)ptr; + u_char *b_ptr = (u_char *)ptr; + + *s_ptr = size; + return b_ptr + sizeof(size_t); + } + return NULL; +} + +void * +coap_realloc_type(coap_memory_tag_t type, void *p, size_t size) { + void *new = mem_malloc(size + sizeof(size_t)); + + (void)type; +#if COAP_MEMORY_TYPE_TRACK + if (new) { + 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 */ + if (new) { + size_t *s_ptr = (size_t *)new; + u_char *b_ptr = (u_char *)new; + + *s_ptr = size; + if (p) { + size_t *o_ptr = (size_t *)p; + + o_ptr--; + memcpy(b_ptr + sizeof(size_t), p, *o_ptr); + } + return b_ptr + sizeof(size_t); + } + return NULL; +} + +void +coap_free_type(coap_memory_tag_t type, void *p) { + u_char *ptr = (u_char *)p; + + (void)type; +#if COAP_MEMORY_TYPE_TRACK + assert(type < COAP_MEM_TAG_LAST); + if (p) + track_counts[type]--; +#endif /* COAP_MEMORY_TYPE_TRACK */ + if (ptr) { + mem_free(ptr - sizeof(size_t)); + } +} + +#define MAKE_CASE(n) case n: name = #n; break +void +coap_dump_memory_type_counts(coap_log_t level) { +#if COAP_MEMORY_TYPE_TRACK + int i; + + coap_log(level, "* Memory type counts\n"); + for (i = 0; i < COAP_MEM_TAG_LAST; i++) { + const char *name = "?"; + + + switch (i) { + MAKE_CASE(COAP_STRING); + MAKE_CASE(COAP_ATTRIBUTE_NAME); + MAKE_CASE(COAP_ATTRIBUTE_VALUE); + MAKE_CASE(COAP_PACKET); + MAKE_CASE(COAP_NODE); + MAKE_CASE(COAP_CONTEXT); + MAKE_CASE(COAP_ENDPOINT); + MAKE_CASE(COAP_PDU); + MAKE_CASE(COAP_PDU_BUF); + MAKE_CASE(COAP_RESOURCE); + MAKE_CASE(COAP_RESOURCEATTR); + MAKE_CASE(COAP_DTLS_SESSION); + MAKE_CASE(COAP_SESSION); + MAKE_CASE(COAP_OPTLIST); + MAKE_CASE(COAP_CACHE_KEY); + MAKE_CASE(COAP_CACHE_ENTRY); + MAKE_CASE(COAP_LG_XMIT); + MAKE_CASE(COAP_LG_CRCV); + MAKE_CASE(COAP_LG_SRCV); + MAKE_CASE(COAP_DIGEST_CTX); + MAKE_CASE(COAP_SUBSCRIPTION); + MAKE_CASE(COAP_DTLS_CONTEXT); + MAKE_CASE(COAP_OSCORE_COM); + MAKE_CASE(COAP_OSCORE_SEN); + MAKE_CASE(COAP_OSCORE_REC); + MAKE_CASE(COAP_OSCORE_EX); + MAKE_CASE(COAP_OSCORE_EP); + MAKE_CASE(COAP_OSCORE_BUF); + MAKE_CASE(COAP_COSE); + case COAP_MEM_TAG_LAST: + default: + break; + } + coap_log(level, "* %-20s in-use %3d peak %3d failed %2d\n", + name, track_counts[i], peak_counts[i], fail_counts[i]); + } +#else /* COAP_MEMORY_TYPE_TRACK */ + (void)level; +#endif /* COAP_MEMORY_TYPE_TRACK */ +} + +#endif /* WITH_LWIP && ! MEMP_USE_CUSTOM_POOLS && | MEM_LIBC_MALLOC */ diff --git a/src/coap_str.c b/src/coap_str.c index 111cfb2c..d3d62ea5 100644 --- a/src/coap_str.c +++ b/src/coap_str.c @@ -20,7 +20,7 @@ coap_string_t * coap_new_string(size_t size) { coap_string_t *s; -#if defined(WITH_LWIP) && MEMP_USE_CUSTOM_POOLS +#if defined(WITH_LWIP) && MEMP_USE_CUSTOM_POOLS && ! MEM_USE_POOLS if (size >= MEMP_LEN_COAPSTRING) { coap_log_crit("coap_new_string: size too large (%" PRIuS " +1 > MEMP_LEN_COAPSTRING)\n", size);