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