libcoap  4.2.0
mem.c
Go to the documentation of this file.
1 /* mem.c -- CoAP memory handling
2  *
3  * Copyright (C) 2014--2015 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 
10 #include "coap_config.h"
11 #include "libcoap.h"
12 #include "mem.h"
13 #include "coap_debug.h"
14 
15 #ifdef HAVE_ASSERT_H
16 #include <assert.h>
17 #else /* HAVE_ASSERT_H */
18 #define assert(...)
19 #endif /* HAVE_ASSERT_H */
20 
21 #ifdef HAVE_MALLOC
22 #include <stdlib.h>
23 
24 void
25 coap_memory_init(void) {
26 }
27 
28 #ifdef __GNUC__
29 #define UNUSED_PARAM __attribute__((unused))
30 #else
31 #define UNUSED_PARAM
32 #endif /* __GNUC__ */
33 
34 void *
35 coap_malloc_type(coap_memory_tag_t type, size_t size) {
36  (void)type;
37  return malloc(size);
38 }
39 
40 void
41 coap_free_type(coap_memory_tag_t type, void *p) {
42  (void)type;
43  free(p);
44 }
45 
46 #else /* HAVE_MALLOC */
47 
48 #ifdef WITH_CONTIKI
49 
54 #ifndef COAP_MAX_STRING_SIZE
55 #define COAP_MAX_STRING_SIZE 64
56 #endif /* COAP_MAX_STRING_SIZE */
57 
62 #ifndef COAP_MAX_STRINGS
63 #define COAP_MAX_STRINGS 10
64 #endif /* COAP_MAX_STRINGS */
65 
66 struct coap_stringbuf_t {
67  char data[COAP_MAX_STRING_SIZE];
68 };
69 
70 #include "coap_config.h"
71 #include "net.h"
72 #include "pdu.h"
73 #include "coap_io.h"
74 #include "resource.h"
75 #include "coap_session.h"
76 
77 #define COAP_MAX_PACKET_SIZE (sizeof(coap_packet_t) + COAP_RXBUFFER_SIZE)
78 #ifndef COAP_MAX_PACKETS
79 #define COAP_MAX_PACKETS 2
80 #endif /* COAP_MAX_PACKETS */
81 
82 typedef union {
83  coap_pdu_t packet; /* try to convince the compiler to word-align this structure */
84  char buf[COAP_MAX_PACKET_SIZE];
85 } coap_packetbuf_t;
86 
87 MEMB(string_storage, struct coap_stringbuf_t, COAP_MAX_STRINGS);
88 MEMB(packet_storage, coap_packetbuf_t, COAP_MAX_PACKETS);
89 MEMB(session_storage, coap_session_t, COAP_MAX_SESSIONS);
90 MEMB(node_storage, coap_queue_t, COAP_PDU_MAXCNT);
91 MEMB(pdu_storage, coap_pdu_t, COAP_PDU_MAXCNT);
92 MEMB(pdu_buf_storage, coap_packetbuf_t, COAP_PDU_MAXCNT);
93 MEMB(resource_storage, coap_resource_t, COAP_MAX_RESOURCES);
94 MEMB(attribute_storage, coap_attr_t, COAP_MAX_ATTRIBUTES);
95 
96 static struct memb *
97 get_container(coap_memory_tag_t type) {
98  switch(type) {
99  case COAP_PACKET: return &packet_storage;
100  case COAP_NODE: return &node_storage;
101  case COAP_SESSION: return &session_storage;
102  case COAP_PDU: return &pdu_storage;
103  case COAP_PDU_BUF: return &pdu_buf_storage;
104  case COAP_RESOURCE: return &resource_storage;
105  case COAP_RESOURCEATTR: return &attribute_storage;
106  default:
107  return &string_storage;
108  }
109 }
110 
111 void
112 coap_memory_init(void) {
113  memb_init(&string_storage);
114  memb_init(&packet_storage);
115  memb_init(&node_storage);
116  memb_init(&session_storage);
117  memb_init(&pdu_storage);
118  memb_init(&pdu_buf_storage);
119  memb_init(&resource_storage);
120  memb_init(&attribute_storage);
121 }
122 
123 void *
124 coap_malloc_type(coap_memory_tag_t type, size_t size) {
125  struct memb *container = get_container(type);
126  void *ptr;
127 
128  assert(container);
129 
130  if (size > container->size) {
132  "coap_malloc_type: Requested memory exceeds maximum object "
133  "size (type %d, size %d, max %d)\n",
134  type, (int)size, container->size);
135  return NULL;
136  }
137 
138  ptr = memb_alloc(container);
139  if (!ptr)
141  "coap_malloc_type: Failure (no free blocks) for type %d\n",
142  type);
143  return ptr;
144 }
145 
146 void
147 coap_free_type(coap_memory_tag_t type, void *object) {
148  memb_free(get_container(type), object);
149 }
150 #endif /* WITH_CONTIKI */
151 
152 #endif /* HAVE_MALLOC */
coap_memory_tag_t
Type specifiers for coap_malloc_type().
Definition: mem.h:29
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
Warning.
Definition: coap_debug.h:46
#define assert(...)
Definition: mem.c:18
Generic resource handling.
Pre-defined constants that reflect defaults for CoAP.
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.
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:122
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
Queue entry.
Definition: net.h:39