• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "libcoap.h"
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 #ifdef HAVE_LIBTINYDTLS
42   COAP_DTLS_SESSION,
43 #endif
44   COAP_SESSION,
45   COAP_OPTLIST,
46 } coap_memory_tag_t;
47 
48 #ifndef WITH_LWIP
49 
50 /**
51  * Allocates a chunk of @p size bytes and returns a pointer to the newly
52  * allocated memory. The @p type is used to select the appropriate storage
53  * container on constrained devices. The storage allocated by coap_malloc_type()
54  * must be released with coap_free_type().
55  *
56  * @param type The type of object to be stored.
57  * @param size The number of bytes requested.
58  * @return     A pointer to the allocated storage or @c NULL on error.
59  */
60 void *coap_malloc_type(coap_memory_tag_t type, size_t size);
61 
62 /**
63  * Releases the memory that was allocated by coap_malloc_type(). The type tag @p
64  * type must be the same that was used for allocating the object pointed to by
65  * @p .
66  *
67  * @param type The type of the object to release.
68  * @param p    A pointer to memory that was allocated by coap_malloc_type().
69  */
70 void coap_free_type(coap_memory_tag_t type, void *p);
71 
72 /**
73  * Wrapper function to coap_malloc_type() for backwards compatibility.
74  */
coap_malloc(size_t size)75 COAP_STATIC_INLINE void *coap_malloc(size_t size) {
76   return coap_malloc_type(COAP_STRING, size);
77 }
78 
79 /**
80  * Wrapper function to coap_free_type() for backwards compatibility.
81  */
coap_free(void * object)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 #include <lwip/mem.h>
92 
93 /* no initialization needed with lwip (or, more precisely: lwip must be
94  * completely initialized anyway by the time coap gets active)  */
coap_memory_init(void)95 COAP_STATIC_INLINE void coap_memory_init(void) {}
96 
97 /* It would be nice to check that size equals the size given at the memp
98  * declaration, but i currently don't see a standard way to check that without
99  * sourcing the custom memp pools and becoming dependent of its syntax
100  */
101 #define coap_malloc_type(type, size) memp_malloc(MEMP_ ## type)
102 #define coap_free_type(type, p) memp_free(MEMP_ ## type, p)
103 
104 /* Those are just here to make uri.c happy where string allocation has not been
105  * made conditional.
106  */
coap_malloc(size_t size)107 COAP_STATIC_INLINE void *coap_malloc(size_t size) {
108   LWIP_ASSERT("coap_malloc must not be used in lwIP", 0);
109   return mem_malloc(size);
110 }
111 
coap_free(void * pointer)112 COAP_STATIC_INLINE void coap_free(void *pointer) {
113   LWIP_ASSERT("coap_free must not be used in lwIP", 0);
114   mem_free(pointer);
115 }
116 
117 #endif /* WITH_LWIP */
118 
119 #endif /* COAP_MEM_H_ */
120