libcoap  4.2.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  * 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
16 
21 void coap_memory_init(void);
22 #endif /* WITH_LWIP */
23 
29 typedef enum {
41 #ifdef HAVE_LIBTINYDTLS
42  COAP_DTLS_SESSION,
43 #endif
47 
48 #ifndef WITH_LWIP
49 
60 void *coap_malloc_type(coap_memory_tag_t type, size_t size);
61 
70 void coap_free_type(coap_memory_tag_t type, void *p);
71 
75 COAP_STATIC_INLINE void *coap_malloc(size_t size) {
76  return coap_malloc_type(COAP_STRING, size);
77 }
78 
82 COAP_STATIC_INLINE void coap_free(void *object) {
83  coap_free_type(COAP_STRING, object);
84 }
85 
86 #endif /* not WITH_LWIP */
87 
88 #ifdef WITH_LWIP
89 
90 #include <lwip/memp.h>
91 
92 /* no initialization needed with lwip (or, more precisely: lwip must be
93  * completely initialized anyway by the time coap gets active) */
95 
96 /* It would be nice to check that size equals the size given at the memp
97  * declaration, but i currently don't see a standard way to check that without
98  * sourcing the custom memp pools and becoming dependent of its syntax
99  */
100 #define coap_malloc_type(type, size) memp_malloc(MEMP_ ## type)
101 #define coap_free_type(type, p) memp_free(MEMP_ ## type, p)
102 
103 /* Those are just here to make uri.c happy where string allocation has not been
104  * made conditional.
105  */
106 COAP_STATIC_INLINE void *coap_malloc(size_t size) {
107  LWIP_ASSERT("coap_malloc must not be used in lwIP", 0);
108 }
109 
110 COAP_STATIC_INLINE void coap_free(void *pointer) {
111  LWIP_ASSERT("coap_free must not be used in lwIP", 0);
112 }
113 
114 #endif /* WITH_LWIP */
115 
116 #endif /* COAP_MEM_H_ */
COAP_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition: mem.h:75
coap_memory_tag_t
Type specifiers for coap_malloc_type().
Definition: mem.h:29
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:82
#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.
void coap_memory_init(void)
Initializes libcoap&#39;s memory management.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
Definition: mem.h:34
Definition: mem.h:37