#include <stdlib.h>
#include
#include <lwip/memp.h>
#include
#define COAP_MEM_H_
#define coap_malloc_type(type, size) memp_malloc(MEMP_ ## type)
/* 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_free_type(type, p) memp_free(MEMP_ ## type, p)
void coap_memory_init(void);
/** * Initializes libcoap's memory management. * This function must be called once before coap_malloc() can be used on * constrained devices. */
void *coap_malloc_type(coap_memory_tag_t type, size_t size);
/** * Allocates a chunk of @p size bytes and returns a pointer to the newly * allocated memory. The @p type is used to select the appropriate storage * container on constrained devices. The storage allocated by coap_malloc_type() * must be released with coap_free_type(). * * @param type The type of object to be stored. * @param size The number of bytes requested. * @return A pointer to the allocated storage or @c NULL on error. */
void coap_free_type(coap_memory_tag_t type, void *p);
/** * Releases the memory that was allocated by coap_malloc_type(). The type tag @p * type must be the same that was used for allocating the object pointed to by * @p . * * @param type The type of the object to release. * @param p A pointer to memory that was allocated by coap_malloc_type(). */
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;
/** * Type specifiers for coap_malloc_type(). Memory objects can be typed to * facilitate arrays of type objects to be used instead of dynamic memory * management on constrained devices. */