libcoap 4.3.1
mem.h
Go to the documentation of this file.
1/*
2 * 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 <stdlib.h>
21
22#ifndef WITH_LWIP
29#endif /* WITH_LWIP */
30
36typedef enum {
60
61#ifndef WITH_LWIP
62
73void *coap_malloc_type(coap_memory_tag_t type, size_t size);
74
89void *coap_realloc_type(coap_memory_tag_t type, void *p, size_t size);
90
100
104COAP_STATIC_INLINE void *coap_malloc(size_t size) {
105 return coap_malloc_type(COAP_STRING, size);
106}
107
111COAP_STATIC_INLINE void coap_free(void *object) {
113}
114
115#endif /* not WITH_LWIP */
116
117#ifdef WITH_LWIP
118
119#include <lwip/memp.h>
120
121/* no initialization needed with lwip (or, more precisely: lwip must be
122 * completely initialized anyway by the time coap gets active) */
124
125/* It would be nice to check that size equals the size given at the memp
126 * declaration, but i currently don't see a standard way to check that without
127 * sourcing the custom memp pools and becoming dependent of its syntax
128 */
129#define coap_malloc_type(type, asize) \
130 (((asize) <= memp_pools[MEMP_ ## type]->size) ? \
131 memp_malloc(MEMP_ ## type) : NULL)
132#define coap_free_type(type, p) memp_free(MEMP_ ## type, p)
133
134/* As these are fixed size, return value if already defined */
135#define coap_realloc_type(type, p, asize) \
136 ((p) ? ((asize) <= memp_pools[MEMP_ ## type]->size) ? (p) : NULL : coap_malloc_type(type, asize))
137
138/* Those are just here to make uri.c happy where string allocation has not been
139 * made conditional.
140 */
141COAP_STATIC_INLINE void *coap_malloc(size_t size) {
142 (void)size;
143 LWIP_ASSERT("coap_malloc must not be used in lwIP", 0);
144}
145
146COAP_STATIC_INLINE void coap_free(void *pointer) {
147 (void)pointer;
148 LWIP_ASSERT("coap_free must not be used in lwIP", 0);
149}
150
151#endif /* WITH_LWIP */
152
153#endif /* COAP_MEM_H_ */
#define COAP_STATIC_INLINE
Definition: libcoap.h:45
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:111
COAP_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition: mem.h:104
void coap_memory_init(void)
Initializes libcoap's memory management.
coap_memory_tag_t
Type specifiers for coap_malloc_type().
Definition: mem.h:36
@ COAP_DTLS_SESSION
Definition: mem.h:48
@ COAP_SESSION
Definition: mem.h:49
@ COAP_CACHE_KEY
Definition: mem.h:51
@ COAP_DIGEST_CTX
Definition: mem.h:56
@ COAP_NODE
Definition: mem.h:41
@ COAP_DTLS_CONTEXT
Definition: mem.h:58
@ COAP_CACHE_ENTRY
Definition: mem.h:52
@ COAP_RESOURCE
Definition: mem.h:46
@ COAP_RESOURCEATTR
Definition: mem.h:47
@ COAP_LG_XMIT
Definition: mem.h:53
@ COAP_ATTRIBUTE_VALUE
Definition: mem.h:39
@ COAP_ENDPOINT
Definition: mem.h:43
@ COAP_CONTEXT
Definition: mem.h:42
@ COAP_OPTLIST
Definition: mem.h:50
@ COAP_PDU
Definition: mem.h:44
@ COAP_LG_CRCV
Definition: mem.h:54
@ COAP_ATTRIBUTE_NAME
Definition: mem.h:38
@ COAP_LG_SRCV
Definition: mem.h:55
@ COAP_PACKET
Definition: mem.h:40
@ COAP_SUBSCRIPTION
Definition: mem.h:57
@ COAP_STRING
Definition: mem.h:37
@ COAP_PDU_BUF
Definition: mem.h:45
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().