libcoap 4.3.5-develop-ea01661
Loading...
Searching...
No Matches
coap_mem.h
Go to the documentation of this file.
1/*
2 * coap_mem.h -- CoAP memory handling
3 *
4 * Copyright (C) 2010-2011,2014-2015 Olaf Bergmann <bergmann@tzi.org>
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 *
8 * This file is part of the CoAP library libcoap. Please see README for terms
9 * of use.
10 */
11
17#ifndef COAP_MEM_H_
18#define COAP_MEM_H_
19
20#include "coap3/coap_oscore.h"
21#include "coap3/coap_proxy.h"
22#include <stdlib.h>
23
24#ifdef WITH_LWIP
25#include <lwip/memp.h>
26#endif /* WITH_LWIP */
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#ifndef WITH_LWIP
39
40#endif /* WITH_LWIP */
41
79
80#ifndef WITH_LWIP
81
92void *coap_malloc_type(coap_memory_tag_t type, size_t size);
93
108void *coap_realloc_type(coap_memory_tag_t type, void *p, size_t size);
109
119
128
133coap_malloc(size_t size) {
134 return coap_malloc_type(COAP_STRING, size);
135}
136
141coap_free(void *object) {
143}
144
145#endif /* not WITH_LWIP */
146
147#ifdef WITH_LWIP
148
149/* no initialization needed with lwip (or, more precisely: lwip must be
150 * completely initialized anyway by the time coap gets active) */
152coap_memory_init(void) {}
153
154#if MEMP_STATS
156coap_malloc_error(uint16_t *err) {
157 (*err)++;
158 return NULL;
159}
160#endif /* MEMP_STATS */
161/* It would be nice to check that size equals the size given at the memp
162 * declaration, but i currently don't see a standard way to check that without
163 * sourcing the custom memp pools and becoming dependent of its syntax
164 */
165#if MEMP_STATS
166#define coap_malloc_type(type, asize) \
167 (((asize) <= memp_pools[MEMP_ ## type]->size) ? \
168 memp_malloc(MEMP_ ## type) : coap_malloc_error(&memp_pools[MEMP_ ## type]->stats->err))
169#else /* ! MEMP_STATS */
170#define coap_malloc_type(type, asize) \
171 (((asize) <= memp_pools[MEMP_ ## type]->size) ? \
172 memp_malloc(MEMP_ ## type) : NULL)
173#endif /* ! MEMP_STATS */
174#define coap_free_type(type, p) memp_free(MEMP_ ## type, p)
175
176/* As these are fixed size, return value if already defined */
177#define coap_realloc_type(type, p, asize) \
178 ((p) ? ((asize) <= memp_pools[MEMP_ ## type]->size) ? (p) : NULL : coap_malloc_type(type, asize))
179
180/* Those are just here to make uri.c happy where string allocation has not been
181 * made conditional.
182 */
184coap_malloc(size_t size) {
185 (void)size;
186 LWIP_ASSERT("coap_malloc must not be used in lwIP", 0);
187}
188
190coap_free(void *pointer) {
191 (void)pointer;
192 LWIP_ASSERT("coap_free must not be used in lwIP", 0);
193}
194
195#define coap_dump_memory_type_counts(l) coap_lwip_dump_memory_pools(l)
196
197#endif /* WITH_LWIP */
198
199#ifdef __cplusplus
200}
201#endif
202
203#endif /* COAP_MEM_H_ */
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition coap_mem.h:141
COAP_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition coap_mem.h:133
void coap_dump_memory_type_counts(coap_log_t log_level)
Dumps the current usage of malloc'd memory types.
Definition coap_mem.c:670
void coap_memory_init(void)
Initializes libcoap's memory management.
coap_memory_tag_t
Type specifiers for coap_malloc_type().
Definition coap_mem.h:47
@ COAP_DTLS_SESSION
Definition coap_mem.h:59
@ COAP_SESSION
Definition coap_mem.h:60
@ COAP_CACHE_KEY
Definition coap_mem.h:62
@ COAP_DIGEST_CTX
Definition coap_mem.h:67
@ COAP_NODE
Definition coap_mem.h:52
@ COAP_OSCORE_COM
Definition coap_mem.h:70
@ COAP_DTLS_CONTEXT
Definition coap_mem.h:69
@ COAP_OSCORE_EX
Definition coap_mem.h:73
@ COAP_CACHE_ENTRY
Definition coap_mem.h:63
@ COAP_RESOURCE
Definition coap_mem.h:57
@ COAP_RESOURCEATTR
Definition coap_mem.h:58
@ COAP_COSE
Definition coap_mem.h:76
@ COAP_LG_XMIT
Definition coap_mem.h:64
@ COAP_ATTRIBUTE_VALUE
Definition coap_mem.h:50
@ COAP_ENDPOINT
Definition coap_mem.h:54
@ COAP_CONTEXT
Definition coap_mem.h:53
@ COAP_OPTLIST
Definition coap_mem.h:61
@ COAP_PDU
Definition coap_mem.h:55
@ COAP_LG_CRCV
Definition coap_mem.h:65
@ COAP_OSCORE_BUF
Definition coap_mem.h:75
@ COAP_MEM_TAG_LAST
Definition coap_mem.h:77
@ COAP_OSCORE_SEN
Definition coap_mem.h:71
@ COAP_OSCORE_REC
Definition coap_mem.h:72
@ COAP_ATTRIBUTE_NAME
Definition coap_mem.h:49
@ COAP_OSCORE_EP
Definition coap_mem.h:74
@ COAP_LG_SRCV
Definition coap_mem.h:66
@ COAP_PACKET
Definition coap_mem.h:51
@ COAP_SUBSCRIPTION
Definition coap_mem.h:68
@ COAP_STRING
Definition coap_mem.h:48
@ COAP_PDU_BUF
Definition coap_mem.h:56
void * coap_realloc_type(coap_memory_tag_t type, void *p, size_t size)
Reallocates a chunk p of bytes created by coap_malloc_type() or coap_realloc_type() and returns a poi...
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_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
CoAP OSCORE support.
Helper functions for proxy handling.
coap_log_t
Logging type.
Definition coap_debug.h:56
#define COAP_STATIC_INLINE
Definition libcoap.h:57