libcoap  4.1.2
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 {
42 
43 #ifndef WITH_LWIP
44 
55 void *coap_malloc_type(coap_memory_tag_t type, size_t size);
56 
65 void coap_free_type(coap_memory_tag_t type, void *p);
66 
70 static inline void *coap_malloc(size_t size) {
71  return coap_malloc_type(COAP_STRING, size);
72 }
73 
77 static inline void coap_free(void *object) {
78  coap_free_type(COAP_STRING, object);
79 }
80 
81 #endif /* not WITH_LWIP */
82 
83 #ifdef WITH_LWIP
84 
85 #include <lwip/memp.h>
86 
87 /* no initialization needed with lwip (or, more precisely: lwip must be
88  * completely initialized anyway by the time coap gets active) */
89 static inline void coap_memory_init(void) {}
90 
91 /* It would be nice to check that size equals the size given at the memp
92  * declaration, but i currently don't see a standard way to check that without
93  * sourcing the custom memp pools and becoming dependent of its syntax
94  */
95 #define coap_malloc_type(type, size) memp_malloc(MEMP_ ## type)
96 #define coap_free_type(type, p) memp_free(MEMP_ ## type, p)
97 
98 /* Those are just here to make uri.c happy where string allocation has not been
99  * made conditional.
100  */
101 static inline void *coap_malloc(size_t size) {
102  LWIP_ASSERT("coap_malloc must not be used in lwIP", 0);
103 }
104 
105 static inline void coap_free(void *pointer) {
106  LWIP_ASSERT("coap_free must not be used in lwIP", 0);
107 }
108 
109 #endif /* WITH_LWIP */
110 
111 #endif /* _COAP_MEM_H_ */
static void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition: mem.h:70
coap_memory_tag_t
Type specifiers for coap_malloc_type().
Definition: mem.h:29
static void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:77
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