libcoap  4.1.2
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 "mem.h"
12 #include "debug.h"
13 
14 #ifdef HAVE_ASSERT_H
15 #include <assert.h>
16 #else /* HAVE_ASSERT_H */
17 #define assert(...)
18 #endif /* HAVE_ASSERT_H */
19 
20 #ifdef HAVE_MALLOC
21 #include <stdlib.h>
22 
23 void
24 coap_memory_init(void) {
25 }
26 
27 #ifdef __GNUC__
28 #define UNUSED_PARAM __attribute__((unused))
29 #else
30 #define UNUSED_PARAM
31 #endif /* __GNUC__ */
32 
33 void *
35  return malloc(size);
36 }
37 
38 void
40  free(p);
41 }
42 
43 #else /* HAVE_MALLOC */
44 
45 #ifdef WITH_CONTIKI
46 
47 #define COAP_MAX_STRING_SIZE 12
48 #define COAP_MAX_STRINGS 8
49 
50 struct coap_string_t {
51  char data[COAP_MAX_STRING_SIZE];
52 };
53 
54 #include "coap_config.h"
55 #include "net.h"
56 #include "pdu.h"
57 #include "coap_io.h"
58 #include "resource.h"
59 
60 #define COAP_MAX_PACKET_SIZE (sizeof(coap_packet_t) + COAP_MAX_PDU_SIZE)
61 #define COAP_MAX_PACKETS 2
62 
63 typedef union {
64  coap_pdu_t packet; /* try to convince the compiler to word-align this structure */
65  char buf[COAP_MAX_PACKET_SIZE];
66 } coap_packetbuf_t;
67 
68 MEMB(string_storage, struct coap_string_t, COAP_MAX_STRINGS);
69 MEMB(packet_storage, coap_packetbuf_t, COAP_MAX_PACKETS);
70 MEMB(node_storage, coap_queue_t, COAP_PDU_MAXCNT);
71 MEMB(pdu_storage, coap_pdu_t, COAP_PDU_MAXCNT);
72 MEMB(pdu_buf_storage, coap_packetbuf_t, COAP_PDU_MAXCNT);
73 MEMB(resource_storage, coap_resource_t, COAP_MAX_RESOURCES);
74 MEMB(attribute_storage, coap_attr_t, COAP_MAX_ATTRIBUTES);
75 
76 static struct memb *
77 get_container(coap_memory_tag_t type) {
78  switch(type) {
79  case COAP_PACKET: return &packet_storage;
80  case COAP_NODE: return &node_storage;
81  case COAP_PDU: return &pdu_storage;
82  case COAP_PDU_BUF: return &pdu_buf_storage;
83  case COAP_RESOURCE: return &resource_storage;
84  case COAP_RESOURCEATTR: return &attribute_storage;
85  default:
86  return &string_storage;
87  }
88 }
89 
90 void
91 coap_memory_init(void) {
92  memb_init(&string_storage);
93  memb_init(&packet_storage);
94  memb_init(&node_storage);
95  memb_init(&pdu_storage);
96  memb_init(&pdu_buf_storage);
97  memb_init(&resource_storage);
98  memb_init(&attribute_storage);
99 }
100 
101 void *
102 coap_malloc_type(coap_memory_tag_t type, size_t size) {
103  struct memb *container = get_container(type);
104 
105  assert(container);
106 
107  if (size > container->size) {
108  debug("coap_malloc_type: Requested memory exceeds maximum object size\n");
109  return NULL;
110  }
111 
112  return memb_alloc(container);
113 }
114 
115 void
116 coap_free_type(coap_memory_tag_t type, void *object) {
117  memb_free(get_container(type), object);
118 }
119 #endif /* WITH_CONTIKI */
120 
121 #endif /* HAVE_MALLOC */
#define debug(...)
Definition: debug.h:66
coap_memory_tag_t
Type specifiers for coap_malloc_type().
Definition: mem.h:29
Header structure for CoAP PDUs.
Definition: pdu.h:227
#define assert(...)
Definition: mem.c:17
#define UNUSED_PARAM
Definition: coap_io.c:245
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.
void coap_memory_init(void)
Initializes libcoap&#39;s memory management.
socklen_t size
size of addr
Definition: address.h:60
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