libcoap 4.3.3
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 <stdlib.h>
22
23#ifndef WITH_LWIP
30#endif /* WITH_LWIP */
31
37typedef enum {
68
69#ifndef WITH_LWIP
70
81void *coap_malloc_type(coap_memory_tag_t type, size_t size);
82
97void *coap_realloc_type(coap_memory_tag_t type, void *p, size_t size);
98
108
113coap_malloc(size_t size) {
114 return coap_malloc_type(COAP_STRING, size);
115}
116
121coap_free(void *object) {
123}
124
125#endif /* not WITH_LWIP */
126
127#ifdef WITH_LWIP
128
129#include <lwip/memp.h>
130
131/* no initialization needed with lwip (or, more precisely: lwip must be
132 * completely initialized anyway by the time coap gets active) */
134coap_memory_init(void) {}
135
136/* It would be nice to check that size equals the size given at the memp
137 * declaration, but i currently don't see a standard way to check that without
138 * sourcing the custom memp pools and becoming dependent of its syntax
139 */
140#define coap_malloc_type(type, asize) \
141 (((asize) <= memp_pools[MEMP_ ## type]->size) ? \
142 memp_malloc(MEMP_ ## type) : NULL)
143#define coap_free_type(type, p) memp_free(MEMP_ ## type, p)
144
145/* As these are fixed size, return value if already defined */
146#define coap_realloc_type(type, p, asize) \
147 ((p) ? ((asize) <= memp_pools[MEMP_ ## type]->size) ? (p) : NULL : coap_malloc_type(type, asize))
148
149/* Those are just here to make uri.c happy where string allocation has not been
150 * made conditional.
151 */
153coap_malloc(size_t size) {
154 (void)size;
155 LWIP_ASSERT("coap_malloc must not be used in lwIP", 0);
156}
157
159coap_free(void *pointer) {
160 (void)pointer;
161 LWIP_ASSERT("coap_free must not be used in lwIP", 0);
162}
163
164#endif /* WITH_LWIP */
165
166#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:121
COAP_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition: coap_mem.h:113
void coap_memory_init(void)
Initializes libcoap's memory management.
coap_memory_tag_t
Type specifiers for coap_malloc_type().
Definition: coap_mem.h:37
@ COAP_DTLS_SESSION
Definition: coap_mem.h:49
@ COAP_SESSION
Definition: coap_mem.h:50
@ COAP_CACHE_KEY
Definition: coap_mem.h:52
@ COAP_DIGEST_CTX
Definition: coap_mem.h:57
@ COAP_NODE
Definition: coap_mem.h:42
@ COAP_OSCORE_COM
Definition: coap_mem.h:60
@ COAP_DTLS_CONTEXT
Definition: coap_mem.h:59
@ COAP_OSCORE_EX
Definition: coap_mem.h:63
@ COAP_CACHE_ENTRY
Definition: coap_mem.h:53
@ COAP_RESOURCE
Definition: coap_mem.h:47
@ COAP_RESOURCEATTR
Definition: coap_mem.h:48
@ COAP_COSE
Definition: coap_mem.h:66
@ COAP_LG_XMIT
Definition: coap_mem.h:54
@ COAP_ATTRIBUTE_VALUE
Definition: coap_mem.h:40
@ COAP_ENDPOINT
Definition: coap_mem.h:44
@ COAP_CONTEXT
Definition: coap_mem.h:43
@ COAP_OPTLIST
Definition: coap_mem.h:51
@ COAP_PDU
Definition: coap_mem.h:45
@ COAP_LG_CRCV
Definition: coap_mem.h:55
@ COAP_OSCORE_BUF
Definition: coap_mem.h:65
@ COAP_OSCORE_SEN
Definition: coap_mem.h:61
@ COAP_OSCORE_REC
Definition: coap_mem.h:62
@ COAP_ATTRIBUTE_NAME
Definition: coap_mem.h:39
@ COAP_OSCORE_EP
Definition: coap_mem.h:64
@ COAP_LG_SRCV
Definition: coap_mem.h:56
@ COAP_PACKET
Definition: coap_mem.h:41
@ COAP_SUBSCRIPTION
Definition: coap_mem.h:58
@ COAP_STRING
Definition: coap_mem.h:38
@ COAP_PDU_BUF
Definition: coap_mem.h:46
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.
#define COAP_STATIC_INLINE
Definition: libcoap.h:53