18#if ! (defined(WITH_LWIP) && MEMP_USE_CUSTOM_POOLS)
19#if COAP_MEMORY_TYPE_TRACK
26#if defined(RIOT_VERSION) && defined(MODULE_MEMARRAY)
48#ifndef COAP_MAX_STRING_SIZE
49#define COAP_MAX_STRING_SIZE (64U)
56#ifndef COAP_MAX_STRINGS
57#define COAP_MAX_STRINGS (16U)
64#ifndef COAP_MAX_ENDPOINTS
66#define COAP_MAX_ENDPOINTS (4U)
68#define COAP_MAX_ENDPOINTS (2U)
76#ifndef COAP_MAX_RESOURCES
77#define COAP_MAX_RESOURCES (8U)
84#ifndef COAP_MAX_ATTRIBUTES
85#define COAP_MAX_ATTRIBUTES \
86 ((COAP_MAX_RESOURCES) * 4U)
95#ifndef COAP_MAX_ATTRIBUTE_STRINGS
96#define COAP_MAX_ATTRIBUTE_STRINGS (COAP_MAX_ATTRIBUTES)
103#ifndef COAP_MAX_ATTRIBUTE_SIZE
104#define COAP_MAX_ATTRIBUTE_SIZE (16U)
111#ifndef COAP_MAX_PACKETS
112#define COAP_MAX_PACKETS (4U)
119#ifndef COAP_MAX_NODES
120#define COAP_MAX_NODES (COAP_MAX_PACKETS)
127#ifndef COAP_MAX_CONTEXTS
128#define COAP_MAX_CONTEXTS (1U)
136#define COAP_MAX_PDUS (4U)
143#ifndef COAP_MAX_DTLS_SESSIONS
144#define COAP_MAX_DTLS_SESSIONS (2U)
151#ifndef COAP_MAX_SESSIONS
152#define COAP_MAX_SESSIONS (4U)
159#ifndef COAP_MAX_OPTIONS
160#define COAP_MAX_OPTIONS (16U)
167#ifndef COAP_MAX_OPTION_SIZE
168#define COAP_MAX_OPTION_SIZE (16U)
175#ifndef COAP_MAX_CACHE_KEYS
176#define COAP_MAX_CACHE_KEYS (2U)
183#ifndef COAP_MAX_CACHE_ENTRIES
184#define COAP_MAX_CACHE_ENTRIES (2U)
191#ifndef COAP_MAX_LG_CRCVS
192#if COAP_CLIENT_SUPPORT
193#define COAP_MAX_LG_CRCVS (1U)
195#define COAP_MAX_LG_CRCVS (0U)
203#ifndef COAP_MAX_LG_SRCVS
204#if COAP_SERVER_SUPPORT
205#define COAP_MAX_LG_SRCVS (2U)
207#define COAP_MAX_LG_SRCVS (0U)
215#ifndef COAP_MAX_LG_XMITS
216#if COAP_SERVER_SUPPORT
217#define COAP_MAX_LG_XMITS (2U)
219#define COAP_MAX_LG_XMITS (1U)
238static union memstr_t string_storage_data[COAP_MAX_STRINGS];
239static memarray_t string_storage;
241#if COAP_SERVER_SUPPORT
243static memarray_t endpoint_storage;
245static union attrstr_t attr_storage_data[COAP_MAX_ATTRIBUTE_STRINGS];
246static memarray_t attr_storage;
248static coap_attr_t resattr_storage_data[COAP_MAX_ATTRIBUTES];
249static memarray_t resattr_storage;
253static memarray_t pkt_storage;
256static memarray_t node_storage;
259static memarray_t context_storage;
261static coap_pdu_t pdu_storage_data[COAP_MAX_PDUS];
262static memarray_t pdu_storage;
271static union pdubuf_t pdubuf_storage_data[COAP_MAX_PDUS];
272static memarray_t pdubuf_storage;
274#if COAP_SERVER_SUPPORT
276static memarray_t resource_storage;
279#if COAP_WITH_LIBTINYDTLS
280#undef PACKAGE_BUGREPORT
283static session_t dtls_storage_data[COAP_MAX_DTLS_SESSIONS];
284static memarray_t dtls_storage;
288static memarray_t session_storage;
293 char optbuf[COAP_MAX_OPTION_SIZE];
295static struct optbuf_t option_storage_data[COAP_MAX_OPTIONS];
296static memarray_t option_storage;
298#if COAP_SERVER_SUPPORT
300static memarray_t cache_key_storage;
303static memarray_t cache_entry_storage;
306#if COAP_CLIENT_SUPPORT
307static coap_lg_crcv_t cache_lg_crcv_storage_data[COAP_MAX_LG_CRCVS];
308static memarray_t cache_lg_crcv_storage;
311#if COAP_SERVER_SUPPORT
312static coap_lg_srcv_t cache_lg_srcv_storage_data[COAP_MAX_LG_SRCVS];
313static memarray_t cache_lg_srcv_storage;
315static coap_lg_xmit_t cache_lg_xmit_storage_data[COAP_MAX_LG_XMITS];
316static memarray_t cache_lg_xmit_storage;
319#define INIT_STORAGE(Storage, Count) \
320 memarray_init(&(Storage ## _storage), (Storage ## _storage_data), sizeof(Storage ## _storage_data[0]), (Count));
322#define STORAGE_PTR(Storage) (&(Storage ## _storage))
326 INIT_STORAGE(
string, COAP_MAX_STRINGS);
327#if COAP_SERVER_SUPPORT
328 INIT_STORAGE(endpoint, COAP_MAX_ENDPOINTS);
329 INIT_STORAGE(attr, COAP_MAX_ATTRIBUTE_STRINGS);
331 INIT_STORAGE(pkt, COAP_MAX_PACKETS);
332 INIT_STORAGE(node, COAP_MAX_NODES);
333 INIT_STORAGE(context, COAP_MAX_CONTEXTS);
334 INIT_STORAGE(pdu, COAP_MAX_PDUS);
335 INIT_STORAGE(pdubuf, COAP_MAX_PDUS);
336#if COAP_SERVER_SUPPORT
337 INIT_STORAGE(resource, COAP_MAX_RESOURCES);
338 INIT_STORAGE(resattr, COAP_MAX_ATTRIBUTES);
340#if COAP_WITH_LIBTINYDTLS
341 INIT_STORAGE(dtls, COAP_MAX_DTLS_SESSIONS);
343 INIT_STORAGE(session, COAP_MAX_SESSIONS);
344 INIT_STORAGE(option, COAP_MAX_OPTIONS);
345#if COAP_SERVER_SUPPORT
346 INIT_STORAGE(cache_key, COAP_MAX_CACHE_KEYS);
347 INIT_STORAGE(cache_entry, COAP_MAX_CACHE_ENTRIES);
349#if COAP_CLIENT_SUPPORT
350 INIT_STORAGE(cache_lg_crcv, COAP_MAX_LG_CRCVS);
352#if COAP_SERVER_SUPPORT
353 INIT_STORAGE(cache_lg_srcv, COAP_MAX_LG_SRCVS);
354 INIT_STORAGE(cache_lg_xmit, COAP_MAX_LG_XMITS);
361#if COAP_SERVER_SUPPORT
365 return &attr_storage;
370 return &node_storage;
372 return STORAGE_PTR(context);
373#if COAP_SERVER_SUPPORT
375 return &endpoint_storage;
380 return &pdubuf_storage;
381#if COAP_SERVER_SUPPORT
383 return &resource_storage;
385 return &resattr_storage;
387#if COAP_WITH_LIBTINYDTLS
389 return &dtls_storage;
392 return &session_storage;
394 return &option_storage;
395#if COAP_SERVER_SUPPORT
397 return &cache_key_storage;
399 return &cache_entry_storage;
401#if COAP_CLIENT_SUPPORT
403 return &cache_lg_crcv_storage;
405#if COAP_SERVER_SUPPORT
407 return &cache_lg_srcv_storage;
409 return &cache_lg_xmit_storage;
414 return &string_storage;
420 memarray_t *container = get_container(type);
424 if (size > container->size) {
425 coap_log_warn(
"coap_malloc_type: Requested memory exceeds maximum object "
426 "size (type %d, size %" PRIuS ", max %" PRIdS ")\n",
427 type, size, container->size);
431 ptr = memarray_alloc(container);
433 coap_log_warn(
"coap_malloc_type: Failure (no free blocks) for type %d\n",
435#if COAP_MEMORY_TYPE_TRACK
438 track_counts[type]++;
439 if (track_counts[type] > peak_counts[type])
440 peak_counts[type] = track_counts[type];
450#if COAP_MEMORY_TYPE_TRACK
453 track_counts[type]--;
456 memarray_free(get_container(type),
object);
461 memarray_t *container = get_container(type);
466 if (size > container->size) {
467 coap_log_warn(
"coap_realloc_type: Requested memory exceeds maximum object "
468 "size (type %d, size %" PRIuS ", max %" PRIdS ")\n",
469 type, size, container->size);
483#elif defined(__ZEPHYR__)
485#include <zephyr/kernel.h>
486#include <zephyr/version.h>
497 ptr = k_malloc(size);
498#if COAP_MEMORY_TYPE_TRACK
501 track_counts[type]++;
502 if (track_counts[type] > peak_counts[type])
503 peak_counts[type] = track_counts[type];
516#if KERNEL_VERSION_NUMBER >= 0x30700
517 ptr = k_realloc(p, size);
520 ptr = k_malloc(size);
526#if COAP_MEMORY_TYPE_TRACK
530 track_counts[type]++;
531 if (track_counts[type] > peak_counts[type])
532 peak_counts[type] = track_counts[type];
543#if COAP_MEMORY_TYPE_TRACK
546 track_counts[type]--;
552#elif defined(WITH_LWIP) && ! MEMP_USE_CUSTOM_POOLS && ! MEM_LIBC_MALLOC
561 void *ptr = mem_malloc(size +
sizeof(
size_t));
564#if COAP_MEMORY_TYPE_TRACK
567 track_counts[type]++;
568 if (track_counts[type] > peak_counts[type])
569 peak_counts[type] = track_counts[type];
575 size_t *s_ptr = (
size_t *)ptr;
576 u_char *b_ptr = (u_char *)ptr;
579 return b_ptr +
sizeof(size_t);
586 void *
new = mem_malloc(size +
sizeof(
size_t));
589#if COAP_MEMORY_TYPE_TRACK
593 track_counts[type]++;
594 if (track_counts[type] > peak_counts[type])
595 peak_counts[type] = track_counts[type];
601 size_t *s_ptr = (
size_t *)
new;
602 u_char *b_ptr = (u_char *)
new;
606 size_t *o_ptr = (
size_t *)p;
609 memcpy(b_ptr +
sizeof(
size_t), p, *o_ptr);
611 return b_ptr +
sizeof(size_t);
618 u_char *ptr = (u_char *)p;
621#if COAP_MEMORY_TYPE_TRACK
624 track_counts[type]--;
627 mem_free(ptr -
sizeof(
size_t));
632#elif defined(HAVE_MALLOC) || defined(__MINGW32__)
645#if COAP_MEMORY_TYPE_TRACK
648 track_counts[type]++;
649 if (track_counts[type] > peak_counts[type])
650 peak_counts[type] = track_counts[type];
663 ptr = realloc(p, size);
664#if COAP_MEMORY_TYPE_TRACK
668 track_counts[type]++;
669 if (track_counts[type] > peak_counts[type])
670 peak_counts[type] = track_counts[type];
681#if COAP_MEMORY_TYPE_TRACK
684 track_counts[type]--;
692#include "lib/heapmem.h"
700 void *ptr = heapmem_alloc(size);
702#if COAP_MEMORY_TYPE_TRACK
705 track_counts[type]++;
706 if (track_counts[type] > peak_counts[type])
707 peak_counts[type] = track_counts[type];
717 void *ptr = heapmem_realloc(p, size);
718#if COAP_MEMORY_TYPE_TRACK
722 track_counts[type]++;
723 if (track_counts[type] > peak_counts[type])
724 peak_counts[type] = track_counts[type];
734#if COAP_MEMORY_TYPE_TRACK
737 track_counts[type]--;
745#if ! (defined(WITH_LWIP) && MEMP_USE_CUSTOM_POOLS)
746#define MAKE_CASE(n) case n: name = #n; break
749#if COAP_MEMORY_TYPE_TRACK
752 coap_log(level,
"* Memory type counts\n");
754 const char *name =
"?";
791 coap_log(level,
"* %-20s in-use %3d peak %3d failed %2d\n",
792 name, track_counts[i], peak_counts[i], fail_counts[i]);
struct coap_lg_crcv_t coap_lg_crcv_t
struct coap_cache_key_t coap_cache_key_t
struct coap_endpoint_t coap_endpoint_t
struct coap_cache_entry_t coap_cache_entry_t
struct coap_attr_t coap_attr_t
struct coap_resource_t coap_resource_t
struct coap_lg_srcv_t coap_lg_srcv_t
Library specific build wrapper for coap_internal.h.
void coap_dump_memory_type_counts(coap_log_t level)
Dumps the current usage of malloc'd memory types.
void coap_memory_init(void)
Initializes libcoap's memory management.
coap_memory_tag_t
Type specifiers for coap_malloc_type().
void * coap_realloc_type(coap_memory_tag_t type, void *p, size_t size)
Reallocates a chunk p of bytes created by coap_malloc_type() or coap_realloc_type() and returns a poi...
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
Pre-defined constants that reflect defaults for CoAP.
Generic resource handling.
Defines the application visible session information.
#define coap_log_warn(...)
#define coap_log(level,...)
Logging function.
#define COAP_DEFAULT_MAX_PDU_RX_SIZE
The CoAP stack's global state is stored in a coap_context_t object.
Structure to hold large body (many blocks) transmission information.
Representation of chained list of CoAP options to install.
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
CoAP string data definition.