mem.h
Go to the documentation of this file.
/*
* mem.h -- CoAP memory handling
*
* Copyright (C) 2010-2011,2014-2015 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_MEM_H_
#define COAP_MEM_H_
#include <stdlib.h>
#ifndef WITH_LWIP
void coap_memory_init(void);
#endif /* WITH_LWIP */
typedef enum {
COAP_STRING,
COAP_ATTRIBUTE_NAME,
COAP_ATTRIBUTE_VALUE,
COAP_PACKET,
COAP_NODE,
COAP_CONTEXT,
COAP_ENDPOINT,
COAP_PDU,
COAP_PDU_BUF,
COAP_RESOURCE,
COAP_RESOURCEATTR,
#ifdef HAVE_LIBTINYDTLS
COAP_DTLS_SESSION,
#endif
COAP_SESSION,
COAP_OPTLIST,
} coap_memory_tag_t;
#ifndef WITH_LWIP
void *coap_malloc_type(coap_memory_tag_t type, size_t size);
void coap_free_type(coap_memory_tag_t type, void *p);
COAP_STATIC_INLINE void *coap_malloc(size_t size) {
return coap_malloc_type(COAP_STRING, size);
}
COAP_STATIC_INLINE void coap_free(void *object) {
coap_free_type(COAP_STRING, object);
}
#endif /* not WITH_LWIP */
#ifdef WITH_LWIP
#include <lwip/memp.h>
/* no initialization needed with lwip (or, more precisely: lwip must be
* completely initialized anyway by the time coap gets active) */
COAP_STATIC_INLINE void coap_memory_init(void) {}
/* It would be nice to check that size equals the size given at the memp
* declaration, but i currently don't see a standard way to check that without
* sourcing the custom memp pools and becoming dependent of its syntax
*/
#define coap_malloc_type(type, size) memp_malloc(MEMP_ ## type)
#define coap_free_type(type, p) memp_free(MEMP_ ## type, p)
/* Those are just here to make uri.c happy where string allocation has not been
* made conditional.
*/
COAP_STATIC_INLINE void *coap_malloc(size_t size) {
LWIP_ASSERT("coap_malloc must not be used in lwIP", 0);
}
COAP_STATIC_INLINE void coap_free(void *pointer) {
LWIP_ASSERT("coap_free must not be used in lwIP", 0);
}
#endif /* WITH_LWIP */
#endif /* COAP_MEM_H_ */
COAP_STATIC_INLINE
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
coap_malloc_type
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.
coap_malloc
COAP_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition: mem.h:75
coap_free
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:82
coap_memory_init
void coap_memory_init(void)
Initializes libcoap's memory management.
coap_memory_tag_t
coap_memory_tag_t
Type specifiers for coap_malloc_type().
Definition: mem.h:29
COAP_SESSION
@ COAP_SESSION
Definition: mem.h:44
COAP_NODE
@ COAP_NODE
Definition: mem.h:34
COAP_RESOURCE
@ COAP_RESOURCE
Definition: mem.h:39
COAP_RESOURCEATTR
@ COAP_RESOURCEATTR
Definition: mem.h:40
COAP_ATTRIBUTE_VALUE
@ COAP_ATTRIBUTE_VALUE
Definition: mem.h:32
COAP_ENDPOINT
@ COAP_ENDPOINT
Definition: mem.h:36
COAP_CONTEXT
@ COAP_CONTEXT
Definition: mem.h:35
COAP_OPTLIST
@ COAP_OPTLIST
Definition: mem.h:45
COAP_PDU
@ COAP_PDU
Definition: mem.h:37
COAP_ATTRIBUTE_NAME
@ COAP_ATTRIBUTE_NAME
Definition: mem.h:31
COAP_PACKET
@ COAP_PACKET
Definition: mem.h:33
COAP_STRING
@ COAP_STRING
Definition: mem.h:30
COAP_PDU_BUF
@ COAP_PDU_BUF
Definition: mem.h:38
coap_free_type
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().