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 (120U)
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)
225 char buf[COAP_MAX_STRING_SIZE];
232 char buf[COAP_MAX_ATTRIBUTE_SIZE];
235static struct memstr_t string_storage_data[COAP_MAX_STRINGS];
236static memarray_t string_storage;
238#if COAP_SERVER_SUPPORT
240static memarray_t endpoint_storage;
242static struct attrstr_t attr_storage_data[COAP_MAX_ATTRIBUTE_STRINGS];
243static memarray_t attr_storage;
245static coap_attr_t resattr_storage_data[COAP_MAX_ATTRIBUTES];
246static memarray_t resattr_storage;
250static memarray_t pkt_storage;
253static memarray_t node_storage;
256static memarray_t context_storage;
258static coap_pdu_t pdu_storage_data[COAP_MAX_PDUS];
259static memarray_t pdu_storage;
268static union pdubuf_t pdubuf_storage_data[COAP_MAX_PDUS];
269static memarray_t pdubuf_storage;
271#if COAP_SERVER_SUPPORT
273static memarray_t resource_storage;
276#if COAP_WITH_LIBTINYDTLS
277#undef PACKAGE_BUGREPORT
280static session_t dtls_storage_data[COAP_MAX_DTLS_SESSIONS];
281static memarray_t dtls_storage;
285static memarray_t session_storage;
290 char optbuf[COAP_MAX_OPTION_SIZE];
292static struct optbuf_t option_storage_data[COAP_MAX_OPTIONS];
293static memarray_t option_storage;
295#if COAP_SERVER_SUPPORT
297static memarray_t cache_key_storage;
300static memarray_t cache_entry_storage;
303#if COAP_CLIENT_SUPPORT
304static coap_lg_crcv_t cache_lg_crcv_storage_data[COAP_MAX_LG_CRCVS];
305static memarray_t cache_lg_crcv_storage;
308#if COAP_SERVER_SUPPORT
309static coap_lg_srcv_t cache_lg_srcv_storage_data[COAP_MAX_LG_SRCVS];
310static memarray_t cache_lg_srcv_storage;
312static coap_lg_xmit_t cache_lg_xmit_storage_data[COAP_MAX_LG_XMITS];
313static memarray_t cache_lg_xmit_storage;
316#define INIT_STORAGE(Storage, Count) \
317 memarray_init(&(Storage ## _storage), (Storage ## _storage_data), sizeof(Storage ## _storage_data[0]), (Count));
319#define STORAGE_PTR(Storage) (&(Storage ## _storage))
323 INIT_STORAGE(
string, COAP_MAX_STRINGS);
324#if COAP_SERVER_SUPPORT
325 INIT_STORAGE(endpoint, COAP_MAX_ENDPOINTS);
326 INIT_STORAGE(attr, COAP_MAX_ATTRIBUTE_STRINGS);
328 INIT_STORAGE(pkt, COAP_MAX_PACKETS);
329 INIT_STORAGE(node, COAP_MAX_NODES);
330 INIT_STORAGE(context, COAP_MAX_CONTEXTS);
331 INIT_STORAGE(pdu, COAP_MAX_PDUS);
332 INIT_STORAGE(pdubuf, COAP_MAX_PDUS);
333#if COAP_SERVER_SUPPORT
334 INIT_STORAGE(resource, COAP_MAX_RESOURCES);
335 INIT_STORAGE(resattr, COAP_MAX_ATTRIBUTES);
337#if COAP_WITH_LIBTINYDTLS
338 INIT_STORAGE(dtls, COAP_MAX_DTLS_SESSIONS);
340 INIT_STORAGE(session, COAP_MAX_SESSIONS);
341 INIT_STORAGE(option, COAP_MAX_OPTIONS);
342#if COAP_SERVER_SUPPORT
343 INIT_STORAGE(cache_key, COAP_MAX_CACHE_KEYS);
344 INIT_STORAGE(cache_entry, COAP_MAX_CACHE_ENTRIES);
346#if COAP_CLIENT_SUPPORT
347 INIT_STORAGE(cache_lg_crcv, COAP_MAX_LG_CRCVS);
349#if COAP_SERVER_SUPPORT
350 INIT_STORAGE(cache_lg_srcv, COAP_MAX_LG_SRCVS);
351 INIT_STORAGE(cache_lg_xmit, COAP_MAX_LG_XMITS);
358#if COAP_SERVER_SUPPORT
362 return &attr_storage;
367 return &node_storage;
369 return STORAGE_PTR(context);
370#if COAP_SERVER_SUPPORT
372 return &endpoint_storage;
377 return &pdubuf_storage;
378#if COAP_SERVER_SUPPORT
380 return &resource_storage;
382 return &resattr_storage;
384#if COAP_WITH_LIBTINYDTLS
386 return &dtls_storage;
389 return &session_storage;
391 return &option_storage;
392#if COAP_SERVER_SUPPORT
394 return &cache_key_storage;
396 return &cache_entry_storage;
398#if COAP_CLIENT_SUPPORT
400 return &cache_lg_crcv_storage;
402#if COAP_SERVER_SUPPORT
404 return &cache_lg_srcv_storage;
406 return &cache_lg_xmit_storage;
411 return &string_storage;
417 memarray_t *container = get_container(type);
421 if (size > container->size) {
422 coap_log_warn(
"coap_malloc_type: Requested memory exceeds maximum object "
423 "size (type %d, size %" PRIuS ", max %" PRIdS ")\n",
424 type, size, container->size);
428 ptr = memarray_alloc(container);
430 coap_log_warn(
"coap_malloc_type: Failure (no free blocks) for type %d\n",
432#if COAP_MEMORY_TYPE_TRACK
435 track_counts[type]++;
436 if (track_counts[type] > peak_counts[type])
437 peak_counts[type] = track_counts[type];
447#if COAP_MEMORY_TYPE_TRACK
450 track_counts[type]--;
453 memarray_free(get_container(type),
object);
458 memarray_t *container = get_container(type);
463 if (size > container->size) {
464 coap_log_warn(
"coap_realloc_type: Requested memory exceeds maximum object "
465 "size (type %d, size %" PRIuS ", max %" PRIdS ")\n",
466 type, size, container->size);
480#elif defined(__ZEPHYR__)
482#include <zephyr/kernel.h>
483#include <zephyr/version.h>
494 ptr = k_malloc(size);
495#if COAP_MEMORY_TYPE_TRACK
498 track_counts[type]++;
499 if (track_counts[type] > peak_counts[type])
500 peak_counts[type] = track_counts[type];
513#if KERNEL_VERSION_NUMBER >= 0x30700
514 ptr = k_realloc(p, size);
517 ptr = k_malloc(size);
523#if COAP_MEMORY_TYPE_TRACK
527 track_counts[type]++;
528 if (track_counts[type] > peak_counts[type])
529 peak_counts[type] = track_counts[type];
540#if COAP_MEMORY_TYPE_TRACK
543 track_counts[type]--;
549#elif defined(WITH_LWIP) && ! MEMP_USE_CUSTOM_POOLS && ! MEM_LIBC_MALLOC
558 void *ptr = mem_malloc(size +
sizeof(
size_t));
561#if COAP_MEMORY_TYPE_TRACK
564 track_counts[type]++;
565 if (track_counts[type] > peak_counts[type])
566 peak_counts[type] = track_counts[type];
572 size_t *s_ptr = (
size_t *)ptr;
573 u_char *b_ptr = (u_char *)ptr;
576 return b_ptr +
sizeof(size_t);
583 void *
new = mem_malloc(size +
sizeof(
size_t));
586#if COAP_MEMORY_TYPE_TRACK
590 track_counts[type]++;
591 if (track_counts[type] > peak_counts[type])
592 peak_counts[type] = track_counts[type];
598 size_t *s_ptr = (
size_t *)
new;
599 u_char *b_ptr = (u_char *)
new;
603 size_t *o_ptr = (
size_t *)p;
606 memcpy(b_ptr +
sizeof(
size_t), p, *o_ptr);
608 return b_ptr +
sizeof(size_t);
615 u_char *ptr = (u_char *)p;
618#if COAP_MEMORY_TYPE_TRACK
621 track_counts[type]--;
624 mem_free(ptr -
sizeof(
size_t));
629#elif defined(HAVE_MALLOC) || defined(__MINGW32__)
642#if COAP_MEMORY_TYPE_TRACK
645 track_counts[type]++;
646 if (track_counts[type] > peak_counts[type])
647 peak_counts[type] = track_counts[type];
660 ptr = realloc(p, size);
661#if COAP_MEMORY_TYPE_TRACK
665 track_counts[type]++;
666 if (track_counts[type] > peak_counts[type])
667 peak_counts[type] = track_counts[type];
678#if COAP_MEMORY_TYPE_TRACK
681 track_counts[type]--;
689#include "lib/heapmem.h"
697 void *ptr = heapmem_alloc(size);
699#if COAP_MEMORY_TYPE_TRACK
702 track_counts[type]++;
703 if (track_counts[type] > peak_counts[type])
704 peak_counts[type] = track_counts[type];
714 void *ptr = heapmem_realloc(p, size);
715#if COAP_MEMORY_TYPE_TRACK
719 track_counts[type]++;
720 if (track_counts[type] > peak_counts[type])
721 peak_counts[type] = track_counts[type];
731#if COAP_MEMORY_TYPE_TRACK
734 track_counts[type]--;
742#if ! (defined(WITH_LWIP) && MEMP_USE_CUSTOM_POOLS)
743#define MAKE_CASE(n) case n: name = #n; break
746#if COAP_MEMORY_TYPE_TRACK
749 coap_log(level,
"* Memory type counts\n");
751 const char *name =
"?";
788 coap_log(level,
"* %-20s in-use %3d peak %3d failed %2d\n",
789 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...