libcoap  4.3.1
mem.h
Go to the documentation of this file.
1 /*
2  * 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 
17 #ifndef COAP_MEM_H_
18 #define COAP_MEM_H_
19 
20 #include <stdlib.h>
21 
22 #ifndef WITH_LWIP
28 void coap_memory_init(void);
29 #endif /* WITH_LWIP */
30 
36 typedef enum {
48 #ifdef HAVE_LIBTINYDTLS
49  COAP_DTLS_SESSION,
50 #endif
59 
60 #ifndef WITH_LWIP
61 
72 void *coap_malloc_type(coap_memory_tag_t type, size_t size);
73 
88 void *coap_realloc_type(coap_memory_tag_t type, void *p, size_t size);
89 
98 void coap_free_type(coap_memory_tag_t type, void *p);
99 
103 COAP_STATIC_INLINE void *coap_malloc(size_t size) {
104  return coap_malloc_type(COAP_STRING, size);
105 }
106 
110 COAP_STATIC_INLINE void coap_free(void *object) {
111  coap_free_type(COAP_STRING, object);
112 }
113 
114 #endif /* not WITH_LWIP */
115 
116 #ifdef WITH_LWIP
117 
118 #include <lwip/memp.h>
119 
120 /* no initialization needed with lwip (or, more precisely: lwip must be
121  * completely initialized anyway by the time coap gets active) */
123 
124 /* It would be nice to check that size equals the size given at the memp
125  * declaration, but i currently don't see a standard way to check that without
126  * sourcing the custom memp pools and becoming dependent of its syntax
127  */
128 #define coap_malloc_type(type, size) memp_malloc(MEMP_ ## type)
129 #define coap_free_type(type, p) memp_free(MEMP_ ## type, p)
130 
131 /* Those are just here to make uri.c happy where string allocation has not been
132  * made conditional.
133  */
134 COAP_STATIC_INLINE void *coap_malloc(size_t size) {
135  LWIP_ASSERT("coap_malloc must not be used in lwIP", 0);
136 }
137 
138 COAP_STATIC_INLINE void coap_free(void *pointer) {
139  LWIP_ASSERT("coap_free must not be used in lwIP", 0);
140 }
141 
142 #endif /* WITH_LWIP */
143 
144 #endif /* COAP_MEM_H_ */
#define COAP_STATIC_INLINE
Definition: libcoap.h:45
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.
COAP_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition: mem.h:103
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:110
void coap_memory_init(void)
Initializes libcoap's memory management.
coap_memory_tag_t
Type specifiers for coap_malloc_type().
Definition: mem.h:36
@ COAP_SESSION
Definition: mem.h:51
@ COAP_CACHE_KEY
Definition: mem.h:53
@ COAP_NODE
Definition: mem.h:41
@ COAP_CACHE_ENTRY
Definition: mem.h:54
@ COAP_RESOURCE
Definition: mem.h:46
@ COAP_RESOURCEATTR
Definition: mem.h:47
@ COAP_LG_XMIT
Definition: mem.h:55
@ COAP_ATTRIBUTE_VALUE
Definition: mem.h:39
@ COAP_ENDPOINT
Definition: mem.h:43
@ COAP_CONTEXT
Definition: mem.h:42
@ COAP_OPTLIST
Definition: mem.h:52
@ COAP_PDU
Definition: mem.h:44
@ COAP_LG_CRCV
Definition: mem.h:56
@ COAP_ATTRIBUTE_NAME
Definition: mem.h:38
@ COAP_LG_SRCV
Definition: mem.h:57
@ COAP_PACKET
Definition: mem.h:40
@ COAP_STRING
Definition: mem.h:37
@ COAP_PDU_BUF
Definition: mem.h:45
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_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().