libcoap  4.3.0beta
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
49 
50 #ifndef WITH_LWIP
51 
62 void *coap_malloc_type(coap_memory_tag_t type, size_t size);
63 
78 void *coap_realloc_type(coap_memory_tag_t type, void *p, size_t size);
79 
88 void coap_free_type(coap_memory_tag_t type, void *p);
89 
93 COAP_STATIC_INLINE void *coap_malloc(size_t size) {
94  return coap_malloc_type(COAP_STRING, size);
95 }
96 
100 COAP_STATIC_INLINE void coap_free(void *object) {
101  coap_free_type(COAP_STRING, object);
102 }
103 
104 #endif /* not WITH_LWIP */
105 
106 #ifdef WITH_LWIP
107 
108 #include <lwip/memp.h>
109 
110 /* no initialization needed with lwip (or, more precisely: lwip must be
111  * completely initialized anyway by the time coap gets active) */
113 
114 /* It would be nice to check that size equals the size given at the memp
115  * declaration, but i currently don't see a standard way to check that without
116  * sourcing the custom memp pools and becoming dependent of its syntax
117  */
118 #define coap_malloc_type(type, size) memp_malloc(MEMP_ ## type)
119 #define coap_free_type(type, p) memp_free(MEMP_ ## type, p)
120 
121 /* Those are just here to make uri.c happy where string allocation has not been
122  * made conditional.
123  */
124 COAP_STATIC_INLINE void *coap_malloc(size_t size) {
125  LWIP_ASSERT("coap_malloc must not be used in lwIP", 0);
126 }
127 
128 COAP_STATIC_INLINE void coap_free(void *pointer) {
129  LWIP_ASSERT("coap_free must not be used in lwIP", 0);
130 }
131 
132 #endif /* WITH_LWIP */
133 
134 #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:93
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:100
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_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_ATTRIBUTE_NAME
Definition: mem.h:31
@ 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().