libcoap  4.3.0
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 
12 #ifndef COAP_MEM_H_
13 #define COAP_MEM_H_
14 
15 #include <stdlib.h>
16 
17 #ifndef WITH_LWIP
23 void coap_memory_init(void);
24 #endif /* WITH_LWIP */
25 
31 typedef enum {
43 #ifdef HAVE_LIBTINYDTLS
44  COAP_DTLS_SESSION,
45 #endif
54 
55 #ifndef WITH_LWIP
56 
67 void *coap_malloc_type(coap_memory_tag_t type, size_t size);
68 
83 void *coap_realloc_type(coap_memory_tag_t type, void *p, size_t size);
84 
93 void coap_free_type(coap_memory_tag_t type, void *p);
94 
98 COAP_STATIC_INLINE void *coap_malloc(size_t size) {
99  return coap_malloc_type(COAP_STRING, size);
100 }
101 
105 COAP_STATIC_INLINE void coap_free(void *object) {
106  coap_free_type(COAP_STRING, object);
107 }
108 
109 #endif /* not WITH_LWIP */
110 
111 #ifdef WITH_LWIP
112 
113 #include <lwip/memp.h>
114 
115 /* no initialization needed with lwip (or, more precisely: lwip must be
116  * completely initialized anyway by the time coap gets active) */
118 
119 /* It would be nice to check that size equals the size given at the memp
120  * declaration, but i currently don't see a standard way to check that without
121  * sourcing the custom memp pools and becoming dependent of its syntax
122  */
123 #define coap_malloc_type(type, size) memp_malloc(MEMP_ ## type)
124 #define coap_free_type(type, p) memp_free(MEMP_ ## type, p)
125 
126 /* Those are just here to make uri.c happy where string allocation has not been
127  * made conditional.
128  */
129 COAP_STATIC_INLINE void *coap_malloc(size_t size) {
130  LWIP_ASSERT("coap_malloc must not be used in lwIP", 0);
131 }
132 
133 COAP_STATIC_INLINE void coap_free(void *pointer) {
134  LWIP_ASSERT("coap_free must not be used in lwIP", 0);
135 }
136 
137 #endif /* WITH_LWIP */
138 
139 #endif /* COAP_MEM_H_ */
#define COAP_STATIC_INLINE
Definition: libcoap.h:40
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:98
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:105
void coap_memory_init(void)
Initializes libcoap's memory management.
coap_memory_tag_t
Type specifiers for coap_malloc_type().
Definition: mem.h:31
@ COAP_SESSION
Definition: mem.h:46
@ COAP_CACHE_KEY
Definition: mem.h:48
@ COAP_NODE
Definition: mem.h:36
@ COAP_CACHE_ENTRY
Definition: mem.h:49
@ COAP_RESOURCE
Definition: mem.h:41
@ COAP_RESOURCEATTR
Definition: mem.h:42
@ COAP_LG_XMIT
Definition: mem.h:50
@ COAP_ATTRIBUTE_VALUE
Definition: mem.h:34
@ COAP_ENDPOINT
Definition: mem.h:38
@ COAP_CONTEXT
Definition: mem.h:37
@ COAP_OPTLIST
Definition: mem.h:47
@ COAP_PDU
Definition: mem.h:39
@ COAP_LG_CRCV
Definition: mem.h:51
@ COAP_ATTRIBUTE_NAME
Definition: mem.h:33
@ COAP_LG_SRCV
Definition: mem.h:52
@ COAP_PACKET
Definition: mem.h:35
@ COAP_STRING
Definition: mem.h:32
@ COAP_PDU_BUF
Definition: mem.h:40
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().