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 /**
17  * Initializes libcoap's memory management.
18  * This function must be called once before coap_malloc() can be used on
19  * constrained devices.
20  */
21 void coap_memory_init(void);
22 #endif /* WITH_LWIP */
23 
24 /**
25  * Type specifiers for coap_malloc_type(). Memory objects can be typed to
26  * facilitate arrays of type objects to be used instead of dynamic memory
27  * management on constrained devices.
28  */
29 typedef enum {
30   COAP_STRING,
31   COAP_ATTRIBUTE_NAME,
32   COAP_ATTRIBUTE_VALUE,
33   COAP_PACKET,
34   COAP_NODE,
35   COAP_CONTEXT,
36   COAP_ENDPOINT,
37   COAP_PDU,
38   COAP_PDU_BUF,
39   COAP_RESOURCE,
40   COAP_RESOURCEATTR
41 } coap_memory_tag_t;
42 
43 #ifndef WITH_LWIP
44 
45 /**
46  * Allocates a chunk of @p size bytes and returns a pointer to the newly
47  * allocated memory. The @p type is used to select the appropriate storage
48  * container on constrained devices. The storage allocated by coap_malloc_type()
49  * must be released with coap_free_type().
50  *
51  * @param type The type of object to be stored.
52  * @param size The number of bytes requested.
53  * @return     A pointer to the allocated storage or @c NULL on error.
54  */
55 void *coap_malloc_type(coap_memory_tag_t type, size_t size);
56 
57 /**
58  * Releases the memory that was allocated by coap_malloc_type(). The type tag @p
59  * type must be the same that was used for allocating the object pointed to by
60  * @p .
61  *
62  * @param type The type of the object to release.
63  * @param p    A pointer to memory that was allocated by coap_malloc_type().
64  */
65 void coap_free_type(coap_memory_tag_t type, void *p);
66 
67 /**
68  * Wrapper function to coap_malloc_type() for backwards compatibility.
69  */
coap_malloc(size_t size)70 static inline void *coap_malloc(size_t size) {
71   return coap_malloc_type(COAP_STRING, size);
72 }
73 
74 /**
75  * Wrapper function to coap_free_type() for backwards compatibility.
76  */
coap_free(void * object)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)  */
coap_memory_init(void)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  */
coap_malloc(size_t size)101 static inline void *coap_malloc(size_t size) {
102   LWIP_ASSERT("coap_malloc must not be used in lwIP", 0);
103 }
104 
coap_free(void * pointer)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_ */
112