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_internal.h"
11
12 #ifdef HAVE_MALLOC
13 #include <stdlib.h>
14
15 void
coap_memory_init(void)16 coap_memory_init(void) {
17 }
18
19 #ifdef __GNUC__
20 #define UNUSED_PARAM __attribute__((unused))
21 #else
22 #define UNUSED_PARAM
23 #endif /* __GNUC__ */
24
25 void *
coap_malloc_type(coap_memory_tag_t type,size_t size)26 coap_malloc_type(coap_memory_tag_t type, size_t size) {
27 (void)type;
28 return malloc(size);
29 }
30
31 void
coap_free_type(coap_memory_tag_t type,void * p)32 coap_free_type(coap_memory_tag_t type, void *p) {
33 (void)type;
34 free(p);
35 }
36
37 #else /* HAVE_MALLOC */
38
39 #ifdef WITH_CONTIKI
40
41 /**
42 * The maximum size of a string on platforms that allocate fixed-size
43 * memory blocks.
44 */
45 #ifndef COAP_MAX_STRING_SIZE
46 #define COAP_MAX_STRING_SIZE 64
47 #endif /* COAP_MAX_STRING_SIZE */
48
49 /**
50 * The maximum number of a strings on platforms that allocate
51 * fixed-size memory blocks.
52 */
53 #ifndef COAP_MAX_STRINGS
54 #define COAP_MAX_STRINGS 10
55 #endif /* COAP_MAX_STRINGS */
56
57 struct coap_stringbuf_t {
58 char data[COAP_MAX_STRING_SIZE];
59 };
60
61
62 #define COAP_MAX_PACKET_SIZE (sizeof(coap_packet_t) + COAP_RXBUFFER_SIZE)
63 #ifndef COAP_MAX_PACKETS
64 #define COAP_MAX_PACKETS 2
65 #endif /* COAP_MAX_PACKETS */
66
67 typedef union {
68 coap_pdu_t packet; /* try to convince the compiler to word-align this structure */
69 char buf[COAP_MAX_PACKET_SIZE];
70 } coap_packetbuf_t;
71
72 MEMB(string_storage, struct coap_stringbuf_t, COAP_MAX_STRINGS);
73 MEMB(packet_storage, coap_packetbuf_t, COAP_MAX_PACKETS);
74 MEMB(session_storage, coap_session_t, COAP_MAX_SESSIONS);
75 MEMB(node_storage, coap_queue_t, COAP_PDU_MAXCNT);
76 MEMB(pdu_storage, coap_pdu_t, COAP_PDU_MAXCNT);
77 MEMB(pdu_buf_storage, coap_packetbuf_t, COAP_PDU_MAXCNT);
78 MEMB(resource_storage, coap_resource_t, COAP_MAX_RESOURCES);
79 MEMB(attribute_storage, coap_attr_t, COAP_MAX_ATTRIBUTES);
80
81 static struct memb *
get_container(coap_memory_tag_t type)82 get_container(coap_memory_tag_t type) {
83 switch(type) {
84 case COAP_PACKET: return &packet_storage;
85 case COAP_NODE: return &node_storage;
86 case COAP_SESSION: return &session_storage;
87 case COAP_PDU: return &pdu_storage;
88 case COAP_PDU_BUF: return &pdu_buf_storage;
89 case COAP_RESOURCE: return &resource_storage;
90 case COAP_RESOURCEATTR: return &attribute_storage;
91 default:
92 return &string_storage;
93 }
94 }
95
96 void
coap_memory_init(void)97 coap_memory_init(void) {
98 memb_init(&string_storage);
99 memb_init(&packet_storage);
100 memb_init(&node_storage);
101 memb_init(&session_storage);
102 memb_init(&pdu_storage);
103 memb_init(&pdu_buf_storage);
104 memb_init(&resource_storage);
105 memb_init(&attribute_storage);
106 }
107
108 void *
coap_malloc_type(coap_memory_tag_t type,size_t size)109 coap_malloc_type(coap_memory_tag_t type, size_t size) {
110 struct memb *container = get_container(type);
111 void *ptr;
112
113 assert(container);
114
115 if (size > container->size) {
116 coap_log(LOG_WARNING,
117 "coap_malloc_type: Requested memory exceeds maximum object "
118 "size (type %d, size %d, max %d)\n",
119 type, (int)size, container->size);
120 return NULL;
121 }
122
123 ptr = memb_alloc(container);
124 if (!ptr)
125 coap_log(LOG_WARNING,
126 "coap_malloc_type: Failure (no free blocks) for type %d\n",
127 type);
128 return ptr;
129 }
130
131 void
coap_free_type(coap_memory_tag_t type,void * object)132 coap_free_type(coap_memory_tag_t type, void *object) {
133 memb_free(get_container(type), object);
134 }
135 #endif /* WITH_CONTIKI */
136
137 #endif /* HAVE_MALLOC */
138