From bafa1ca05ea458c647f8521aff9e6900e2ced1ac Mon Sep 17 00:00:00 2001 From: chrysn Date: Thu, 15 Jan 2015 11:40:19 +0100 Subject: [PATCH] coap_malloc_type implementation for lwip based on memp those things need to reside in mem.h (instead of living implementation dependently inside mem.c) because coap_malloc_type(MY_TYPE, ...) is converted to memp_malloc(MEMP_MY_TYPE) and thus needs to be done in the preprocessor. (the alternative would be to switch on the MY_TYPE enum, which would be harder to maintain). --- mem.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/mem.h b/mem.h index 798ac691..c99f9fc9 100644 --- a/mem.h +++ b/mem.h @@ -13,12 +13,14 @@ #include #endif +#ifndef WITH_LWIP /** * Initializes libcoap's memory management. This function must be * called once before coap_malloc() can be used on constrained * devices. */ void coap_memory_init(void); +#endif /* WITH_LWIP */ /** * Type specifiers for coap_malloc_type(). Memory objects can be typed @@ -29,6 +31,8 @@ typedef enum { COAP_STRING, COAP_ATTRIBUTE_NAME, COAP_ATTRIBUTE_VALUE, COAP_NODE, COAP_CONTEXT, COAP_ENDPOINT } coap_memory_tag_t; +#ifndef WITH_LWIP + /** * Allocates a chunk of @p size bytes and returns a pointer to the * newly allocated memory. The @p type is used to select the @@ -67,4 +71,26 @@ static inline void coap_free(void *object) { coap_free_type(COAP_STRING, object); } +#endif /* not WITH_LWIP */ + +#ifdef WITH_LWIP + +#include + +/* no initialization needed with lwip (or, more precisely: lwip must be + * completely initialized anyway by the time coap gets active) */ +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 */ +static inline void *coap_malloc(size_t size) { LWIP_ASSERT("coap_malloc must not be used in lwIP", 0); } +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_ */