/src/libcoap/include/coap3/coap_mem.h
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * coap_mem.h -- CoAP memory handling  | 
3  |  |  *  | 
4  |  |  * Copyright (C) 2010-2011,2014-2015 Olaf Bergmann <bergmann@tzi.org>  | 
5  |  |  *  | 
6  |  |  * SPDX-License-Identifier: BSD-2-Clause  | 
7  |  |  *  | 
8  |  |  * This file is part of the CoAP library libcoap. Please see README for terms  | 
9  |  |  * of use.  | 
10  |  |  */  | 
11  |  |  | 
12  |  | /**  | 
13  |  |  * @file coap_mem.h  | 
14  |  |  * @brief CoAP memory handling  | 
15  |  |  */  | 
16  |  |  | 
17  |  | #ifndef COAP_MEM_H_  | 
18  |  | #define COAP_MEM_H_  | 
19  |  |  | 
20  |  | #include "coap3/coap_oscore.h"  | 
21  |  | #include "coap3/coap_proxy.h"  | 
22  |  | #include <stdlib.h>  | 
23  |  |  | 
24  |  | #ifdef __cplusplus  | 
25  |  | extern "C" { | 
26  |  | #endif  | 
27  |  |  | 
28  |  | /**  | 
29  |  |  * Type specifiers for coap_malloc_type(). Memory objects can be typed to  | 
30  |  |  * facilitate arrays of type objects to be used instead of dynamic memory  | 
31  |  |  * management on constrained devices.  | 
32  |  |  */  | 
33  |  | typedef enum { | 
34  |  |   COAP_STRING,  | 
35  |  |   COAP_ATTRIBUTE_NAME,  | 
36  |  |   COAP_ATTRIBUTE_VALUE,  | 
37  |  |   COAP_PACKET,  | 
38  |  |   COAP_NODE,  | 
39  |  |   COAP_CONTEXT,  | 
40  |  |   COAP_ENDPOINT,  | 
41  |  |   COAP_PDU,  | 
42  |  |   COAP_PDU_BUF,  | 
43  |  |   COAP_RESOURCE,  | 
44  |  |   COAP_RESOURCEATTR,  | 
45  |  |   COAP_DTLS_SESSION,  | 
46  |  |   COAP_SESSION,  | 
47  |  |   COAP_OPTLIST,  | 
48  |  |   COAP_CACHE_KEY,  | 
49  |  |   COAP_CACHE_ENTRY,  | 
50  |  |   COAP_LG_XMIT,  | 
51  |  |   COAP_LG_CRCV,  | 
52  |  |   COAP_LG_SRCV,  | 
53  |  |   COAP_DIGEST_CTX,  | 
54  |  |   COAP_SUBSCRIPTION,  | 
55  |  |   COAP_DTLS_CONTEXT,  | 
56  |  |   COAP_OSCORE_COM,  | 
57  |  |   COAP_OSCORE_SEN,  | 
58  |  |   COAP_OSCORE_REC,  | 
59  |  |   COAP_OSCORE_EX,  | 
60  |  |   COAP_OSCORE_EP,  | 
61  |  |   COAP_OSCORE_BUF,  | 
62  |  |   COAP_COSE,  | 
63  |  |   COAP_MEM_TAG_LAST  | 
64  |  | } coap_memory_tag_t;  | 
65  |  |  | 
66  |  | #if ! defined(WITH_LWIP) || (defined(WITH_LWIP) && ! MEMP_USE_CUSTOM_POOLS)  | 
67  |  |  | 
68  |  | /**  | 
69  |  |  * Initializes libcoap's memory management.  | 
70  |  |  * This function must be called once before coap_malloc() can be used on  | 
71  |  |  * constrained devices.  | 
72  |  |  */  | 
73  |  | void coap_memory_init(void);  | 
74  |  |  | 
75  |  | /**  | 
76  |  |  * Allocates a chunk of @p size bytes and returns a pointer to the newly  | 
77  |  |  * allocated memory. The @p type is used to select the appropriate storage  | 
78  |  |  * container on constrained devices. The storage allocated by coap_malloc_type()  | 
79  |  |  * must be released with coap_free_type().  | 
80  |  |  *  | 
81  |  |  * @param type The type of object to be stored.  | 
82  |  |  * @param size The number of bytes requested.  | 
83  |  |  * @return     A pointer to the allocated storage or @c NULL on error.  | 
84  |  |  */  | 
85  |  | void *coap_malloc_type(coap_memory_tag_t type, size_t size);  | 
86  |  |  | 
87  |  | /**  | 
88  |  |  * Reallocates a chunk @p p of bytes created by coap_malloc_type() or  | 
89  |  |  * coap_realloc_type() and returns a pointer to the newly allocated memory of  | 
90  |  |  * @p size.  | 
91  |  |  * Only COAP_STRING type is supported.  | 
92  |  |  *  | 
93  |  |  * Note: If there is an error, @p p will separately need to be released by  | 
94  |  |  * coap_free_type().  | 
95  |  |  *  | 
96  |  |  * @param type The type of object to be stored.  | 
97  |  |  * @param p    A pointer to memory that was allocated by coap_malloc_type().  | 
98  |  |  * @param size The number of bytes requested.  | 
99  |  |  * @return     A pointer to the allocated storage or @c NULL on error.  | 
100  |  |  */  | 
101  |  | void *coap_realloc_type(coap_memory_tag_t type, void *p, size_t size);  | 
102  |  |  | 
103  |  | /**  | 
104  |  |  * Releases the memory that was allocated by coap_malloc_type(). The type tag @p  | 
105  |  |  * type must be the same that was used for allocating the object pointed to by  | 
106  |  |  * @p .  | 
107  |  |  *  | 
108  |  |  * @param type The type of the object to release.  | 
109  |  |  * @param p    A pointer to memory that was allocated by coap_malloc_type().  | 
110  |  |  */  | 
111  |  | void coap_free_type(coap_memory_tag_t type, void *p);  | 
112  |  |  | 
113  |  | /**  | 
114  |  |  * Dumps the current usage of malloc'd memory types.  | 
115  |  |  *  | 
116  |  |  * Requires COAP_MEMORY_TYPE_TRACK to be defined to 1.  | 
117  |  |  *  | 
118  |  |  * @param log_level The logging level to use.  | 
119  |  |  */  | 
120  |  | void coap_dump_memory_type_counts(coap_log_t log_level);  | 
121  |  |  | 
122  |  | /**  | 
123  |  |  * Wrapper function to coap_malloc_type() for backwards compatibility.  | 
124  |  |  */  | 
125  |  | COAP_STATIC_INLINE void *  | 
126  | 0  | coap_malloc(size_t size) { | 
127  | 0  |   return coap_malloc_type(COAP_STRING, size);  | 
128  | 0  | } Unexecuted instantiation: coap_debug.c:coap_malloc Unexecuted instantiation: coap_encode.c:coap_malloc Unexecuted instantiation: coap_net.c:coap_malloc Unexecuted instantiation: coap_netif.c:coap_malloc Unexecuted instantiation: coap_notls.c:coap_malloc Unexecuted instantiation: coap_option.c:coap_malloc Unexecuted instantiation: coap_oscore.c:coap_malloc Unexecuted instantiation: coap_pdu.c:coap_malloc Unexecuted instantiation: coap_proxy.c:coap_malloc Unexecuted instantiation: coap_prng.c:coap_malloc Unexecuted instantiation: coap_resource.c:coap_malloc Unexecuted instantiation: coap_session.c:coap_malloc Unexecuted instantiation: coap_sha1.c:coap_malloc Unexecuted instantiation: coap_str.c:coap_malloc Unexecuted instantiation: coap_strm_posix.c:coap_malloc Unexecuted instantiation: coap_subscribe.c:coap_malloc Unexecuted instantiation: coap_threadsafe.c:coap_malloc Unexecuted instantiation: coap_time.c:coap_malloc Unexecuted instantiation: coap_uri.c:coap_malloc Unexecuted instantiation: coap_ws.c:coap_malloc Unexecuted instantiation: oscore.c:coap_malloc Unexecuted instantiation: oscore_cbor.c:coap_malloc Unexecuted instantiation: oscore_context.c:coap_malloc Unexecuted instantiation: oscore_cose.c:coap_malloc Unexecuted instantiation: oscore_crypto.c:coap_malloc Unexecuted instantiation: coap_address.c:coap_malloc Unexecuted instantiation: coap_async.c:coap_malloc Unexecuted instantiation: coap_block.c:coap_malloc Unexecuted instantiation: coap_cache.c:coap_malloc Unexecuted instantiation: coap_dgrm_posix.c:coap_malloc Unexecuted instantiation: coap_dtls.c:coap_malloc Unexecuted instantiation: coap_hashkey.c:coap_malloc Unexecuted instantiation: coap_io.c:coap_malloc Unexecuted instantiation: coap_io_posix.c:coap_malloc Unexecuted instantiation: coap_layers.c:coap_malloc Unexecuted instantiation: coap_mem.c:coap_malloc  | 
129  |  |  | 
130  |  | /**  | 
131  |  |  * Wrapper function to coap_free_type() for backwards compatibility.  | 
132  |  |  */  | 
133  |  | COAP_STATIC_INLINE void  | 
134  | 0  | coap_free(void *object) { | 
135  | 0  |   coap_free_type(COAP_STRING, object);  | 
136  | 0  | } Unexecuted instantiation: coap_debug.c:coap_free Unexecuted instantiation: coap_encode.c:coap_free Unexecuted instantiation: coap_net.c:coap_free Unexecuted instantiation: coap_netif.c:coap_free Unexecuted instantiation: coap_notls.c:coap_free Unexecuted instantiation: coap_option.c:coap_free Unexecuted instantiation: coap_oscore.c:coap_free Unexecuted instantiation: coap_pdu.c:coap_free Unexecuted instantiation: coap_proxy.c:coap_free Unexecuted instantiation: coap_prng.c:coap_free Unexecuted instantiation: coap_resource.c:coap_free Unexecuted instantiation: coap_session.c:coap_free Unexecuted instantiation: coap_sha1.c:coap_free Unexecuted instantiation: coap_str.c:coap_free Unexecuted instantiation: coap_strm_posix.c:coap_free Unexecuted instantiation: coap_subscribe.c:coap_free Unexecuted instantiation: coap_threadsafe.c:coap_free Unexecuted instantiation: coap_time.c:coap_free Unexecuted instantiation: coap_uri.c:coap_free Unexecuted instantiation: coap_ws.c:coap_free Unexecuted instantiation: oscore.c:coap_free Unexecuted instantiation: oscore_cbor.c:coap_free Unexecuted instantiation: oscore_context.c:coap_free Unexecuted instantiation: oscore_cose.c:coap_free Unexecuted instantiation: oscore_crypto.c:coap_free Unexecuted instantiation: coap_address.c:coap_free Unexecuted instantiation: coap_async.c:coap_free Unexecuted instantiation: coap_block.c:coap_free Unexecuted instantiation: coap_cache.c:coap_free Unexecuted instantiation: coap_dgrm_posix.c:coap_free Unexecuted instantiation: coap_dtls.c:coap_free Unexecuted instantiation: coap_hashkey.c:coap_free Unexecuted instantiation: coap_io.c:coap_free Unexecuted instantiation: coap_io_posix.c:coap_free Unexecuted instantiation: coap_layers.c:coap_free Unexecuted instantiation: coap_mem.c:coap_free  | 
137  |  |  | 
138  |  | #else /* WITH_LWIP && MEMP_USE_CUSTOM_POOLS */  | 
139  |  |  | 
140  |  | #include <lwip/memp.h>  | 
141  |  |  | 
142  |  | /* no initialization needed with lwip (or, more precisely: lwip must be  | 
143  |  |  * completely initialized anyway by the time coap gets active)  */  | 
144  |  | COAP_STATIC_INLINE void  | 
145  |  | coap_memory_init(void) {} | 
146  |  |  | 
147  |  | #if MEMP_STATS  | 
148  |  | COAP_STATIC_INLINE void *  | 
149  |  | coap_malloc_error(uint16_t *err) { | 
150  |  |   (*err)++;  | 
151  |  |   return NULL;  | 
152  |  | }  | 
153  |  | #endif /* MEMP_STATS */  | 
154  |  | /* It would be nice to check that size equals the size given at the memp  | 
155  |  |  * declaration, but i currently don't see a standard way to check that without  | 
156  |  |  * sourcing the custom memp pools and becoming dependent of its syntax  | 
157  |  |  */  | 
158  |  | #if MEMP_STATS  | 
159  |  | #define coap_malloc_type(type, asize) \  | 
160  |  |   (((asize) <= memp_pools[MEMP_ ## type]->size) ? \  | 
161  |  |    memp_malloc(MEMP_ ## type) : coap_malloc_error(&memp_pools[MEMP_ ## type]->stats->err))  | 
162  |  | #else /* ! MEMP_STATS */  | 
163  |  | #define coap_malloc_type(type, asize) \  | 
164  |  |   (((asize) <= memp_pools[MEMP_ ## type]->size) ? \  | 
165  |  |    memp_malloc(MEMP_ ## type) : NULL)  | 
166  |  | #endif /* ! MEMP_STATS */  | 
167  |  | #define coap_free_type(type, p) memp_free(MEMP_ ## type, p)  | 
168  |  |  | 
169  |  | /* As these are fixed size, return value if already defined */  | 
170  |  | #define coap_realloc_type(type, p, asize) \  | 
171  |  |   ((p) ? ((asize) <= memp_pools[MEMP_ ## type]->size) ? (p) : NULL : coap_malloc_type(type, asize))  | 
172  |  |  | 
173  |  | /* Those are just here to make uri.c happy where string allocation has not been  | 
174  |  |  * made conditional.  | 
175  |  |  */  | 
176  |  | COAP_STATIC_INLINE void *  | 
177  |  | coap_malloc(size_t size) { | 
178  |  |   (void)size;  | 
179  |  |   LWIP_ASSERT("coap_malloc must not be used in lwIP", 0); | 
180  |  | }  | 
181  |  |  | 
182  |  | COAP_STATIC_INLINE void  | 
183  |  | coap_free(void *pointer) { | 
184  |  |   (void)pointer;  | 
185  |  |   LWIP_ASSERT("coap_free must not be used in lwIP", 0); | 
186  |  | }  | 
187  |  |  | 
188  |  | #define coap_dump_memory_type_counts(l) coap_lwip_dump_memory_pools(l)  | 
189  |  |  | 
190  |  | #endif /* WITH_LWIP && MEMP_USE_CUSTOM_POOLS */  | 
191  |  |  | 
192  |  | #ifdef __cplusplus  | 
193  |  | }  | 
194  |  | #endif  | 
195  |  |  | 
196  |  | #endif /* COAP_MEM_H_ */  |