libcoap 4.3.5-develop-7370fcf
Loading...
Searching...
No Matches
coap_mem_lwip.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012,2014 Olaf Bergmann <bergmann@tzi.org>
3 * 2014 chrysn <chrysn@fsfe.org>
4 * 2022-2026 Jon Shallow <supjps-libcoap@jpshallow.com>
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 *
8 * This file is part of the CoAP library libcoap. Please see
9 * README for terms of use.
10 */
11
18
19#if defined(WITH_LWIP) && MEMP_USE_CUSTOM_POOLS
20
21#if MEM_USE_POOLS
22
23/* Need to handle COAP_STRING separately */
24
25void *
26coap_malloc_type_string(size_t size) {
27 void *ptr = mem_malloc(size + sizeof(size_t));
28
29 if (ptr) {
30 unsigned int *s_ptr = (unsigned int *)ptr;
31 u_char *b_ptr = (u_char *)ptr;
32
33 *s_ptr = size;
34 return b_ptr + sizeof(unsigned int);
35 }
36 return NULL;
37}
38
39void *
40coap_realloc_type_string(void *p, size_t size) {
41 void *new;
42 unsigned int *s_ptr = (unsigned int *)p;
43
44 if (*s_ptr <= size)
45 return p;
46
47 new = mem_malloc(size + sizeof(unsigned int));
48
49 if (new) {
50 u_char *b_ptr = (u_char *)new;
51
52 s_ptr = (unsigned int *)new;
53 *s_ptr = size;
54 if (p) {
55 unsigned int *o_ptr = (unsigned int *)p;
56
57 o_ptr--;
58 memcpy(b_ptr + sizeof(unsigned int), p, *o_ptr);
59 coap_free_type_string(o_ptr);
60 }
61 return b_ptr + sizeof(unsigned int);
62 }
63 return NULL;
64}
65
66void
67coap_free_type_string(void *p) {
68 u_char *ptr = (u_char *)p;
69
70 if (ptr) {
71 mem_free(ptr - sizeof(unsigned int));
72 }
73}
74
75#endif
76
77void
79#if MEMP_STATS && LWIP_STATS_DISPLAY && MEMP_USE_CUSTOM_POOLS
80 int i;
81
82 /* Save time if not needed */
83 if (log_level > coap_get_log_level())
84 return;
85
86 coap_log(log_level, "* LwIP custom memory pools information\n");
87 /*
88 * Make sure LwIP and libcoap have been built with the same
89 * -DCOAP_CLIENT_ONLY or -DCOAP_SERVER_ONLY options for
90 * MEMP_MAX to be correct.
91 */
92 for (i = 0; i < MEMP_MAX; i++) {
93#if MEM_USE_POOLS
94 if (!strcmp("COAP_STRING", memp_pools[i]->stats->name))
95 continue;
96#endif /* MEM_USE_POOLS */
97 coap_log(log_level, "* %-17s avail %3d in-use %3d peak %3d failed %3d\n",
98 memp_pools[i]->stats->name, memp_pools[i]->stats->avail,
99 memp_pools[i]->stats->used, memp_pools[i]->stats->max,
100 memp_pools[i]->stats->err);
101 }
102#else /* !( MEMP_STATS && LWIP_STATS_DISPLAY && MEMP_USE_CUSTOM_POOLS) */
103 (void)log_level;
104#endif /* !( MEMP_STATS && LWIP_STATS_DISPLAY && MEMP_USE_CUSTOM_POOLS) */
105}
106
107#elif defined(WITH_LWIP) && ! MEMP_USE_CUSTOM_POOLS && ! MEM_LIBC_MALLOC
108
109#include <lwip/mem.h>
110
111#if COAP_MEMORY_TYPE_TRACK
112static int track_counts[COAP_MEM_TAG_LAST];
113static int peak_counts[COAP_MEM_TAG_LAST];
114static int fail_counts[COAP_MEM_TAG_LAST];
115#endif /* COAP_MEMORY_TYPE_TRACK */
116
117void
118coap_memory_init(void) {
119}
120
121void *
122coap_malloc_type(coap_memory_tag_t type, size_t size) {
123 void *ptr = mem_malloc(size + sizeof(size_t));
124
125 (void)type;
126#if COAP_MEMORY_TYPE_TRACK
127 assert(type < COAP_MEM_TAG_LAST);
128 if (ptr) {
129 track_counts[type]++;
130 if (track_counts[type] > peak_counts[type])
131 peak_counts[type] = track_counts[type];
132 } else {
133 fail_counts[type]++;
134 }
135#endif /* COAP_MEMORY_TYPE_TRACK */
136 if (ptr) {
137 size_t *s_ptr = (size_t *)ptr;
138 u_char *b_ptr = (u_char *)ptr;
139
140 *s_ptr = size;
141 return b_ptr + sizeof(size_t);
142 }
143 return NULL;
144}
145
146void *
147coap_realloc_type(coap_memory_tag_t type, void *p, size_t size) {
148 void *new = mem_malloc(size + sizeof(size_t));
149
150 (void)type;
151#if COAP_MEMORY_TYPE_TRACK
152 if (new) {
153 assert(type < COAP_MEM_TAG_LAST);
154 if (!p)
155 track_counts[type]++;
156 if (track_counts[type] > peak_counts[type])
157 peak_counts[type] = track_counts[type];
158 } else {
159 fail_counts[type]++;
160 }
161#endif /* COAP_MEMORY_TYPE_TRACK */
162 if (new) {
163 size_t *s_ptr = (size_t *)new;
164 u_char *b_ptr = (u_char *)new;
165
166 *s_ptr = size;
167 if (p) {
168 size_t *o_ptr = (size_t *)p;
169
170 o_ptr--;
171 memcpy(b_ptr + sizeof(size_t), p, *o_ptr);
172 }
173 return b_ptr + sizeof(size_t);
174 }
175 return NULL;
176}
177
178void
179coap_free_type(coap_memory_tag_t type, void *p) {
180 u_char *ptr = (u_char *)p;
181
182 (void)type;
183#if COAP_MEMORY_TYPE_TRACK
184 assert(type < COAP_MEM_TAG_LAST);
185 if (p)
186 track_counts[type]--;
187#endif /* COAP_MEMORY_TYPE_TRACK */
188 if (ptr) {
189 mem_free(ptr - sizeof(size_t));
190 }
191}
192
193#define MAKE_CASE(n) case n: name = #n; break
194void
196#if COAP_MEMORY_TYPE_TRACK
197 int i;
198
199 coap_log(level, "* Memory type counts\n");
200 for (i = 0; i < COAP_MEM_TAG_LAST; i++) {
201 const char *name = "?";
202
203
204 switch (i) {
235 default:
236 break;
237 }
238 coap_log(level, "* %-20s in-use %3d peak %3d failed %2d\n",
239 name, track_counts[i], peak_counts[i], fail_counts[i]);
240 }
241#else /* COAP_MEMORY_TYPE_TRACK */
242 (void)level;
243#endif /* COAP_MEMORY_TYPE_TRACK */
244}
245
246#endif /* WITH_LWIP && ! MEMP_USE_CUSTOM_POOLS && | MEM_LIBC_MALLOC */
Library specific build wrapper for coap_internal.h.
#define MAKE_CASE(n)
Definition coap_mem.c:664
void coap_dump_memory_type_counts(coap_log_t level)
Dumps the current usage of malloc'd memory types.
Definition coap_mem.c:666
void coap_memory_init(void)
Initializes libcoap's memory management.
coap_memory_tag_t
Type specifiers for coap_malloc_type().
Definition coap_mem.h:32
@ COAP_DTLS_SESSION
Definition coap_mem.h:44
@ COAP_SESSION
Definition coap_mem.h:45
@ COAP_CACHE_KEY
Definition coap_mem.h:47
@ COAP_DIGEST_CTX
Definition coap_mem.h:52
@ COAP_NODE
Definition coap_mem.h:37
@ COAP_OSCORE_COM
Definition coap_mem.h:55
@ COAP_DTLS_CONTEXT
Definition coap_mem.h:54
@ COAP_OSCORE_EX
Definition coap_mem.h:58
@ COAP_CACHE_ENTRY
Definition coap_mem.h:48
@ COAP_RESOURCE
Definition coap_mem.h:42
@ COAP_RESOURCEATTR
Definition coap_mem.h:43
@ COAP_COSE
Definition coap_mem.h:61
@ COAP_LG_XMIT
Definition coap_mem.h:49
@ COAP_ATTRIBUTE_VALUE
Definition coap_mem.h:35
@ COAP_ENDPOINT
Definition coap_mem.h:39
@ COAP_CONTEXT
Definition coap_mem.h:38
@ COAP_OPTLIST
Definition coap_mem.h:46
@ COAP_PDU
Definition coap_mem.h:40
@ COAP_LG_CRCV
Definition coap_mem.h:50
@ COAP_OSCORE_BUF
Definition coap_mem.h:60
@ COAP_MEM_TAG_LAST
Definition coap_mem.h:62
@ COAP_OSCORE_SEN
Definition coap_mem.h:56
@ COAP_OSCORE_REC
Definition coap_mem.h:57
@ COAP_ATTRIBUTE_NAME
Definition coap_mem.h:34
@ COAP_OSCORE_EP
Definition coap_mem.h:59
@ COAP_LG_SRCV
Definition coap_mem.h:51
@ COAP_PACKET
Definition coap_mem.h:36
@ COAP_SUBSCRIPTION
Definition coap_mem.h:53
@ COAP_STRING
Definition coap_mem.h:33
@ COAP_PDU_BUF
Definition coap_mem.h:41
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().
#define NULL
Definition coap_option.h:30
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition coap_debug.c:103
coap_log_t
Logging type.
Definition coap_debug.h:56
#define coap_log(level,...)
Logging function.
Definition coap_debug.h:290