From 77183cdc483ae1e33bed33ff6cc15bbeee8f1da5 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Wed, 18 Jun 2014 23:35:19 +0200 Subject: [PATCH] Use type tag in coap_free_type(). --- mem.c | 30 +++++------------------------- mem.h | 7 +++++-- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/mem.c b/mem.c index fc5d46d1..44508ca4 100644 --- a/mem.c +++ b/mem.c @@ -36,7 +36,7 @@ coap_malloc_type(coap_memory_tag_t type UNUSED_PARAM, size_t size) { } void -coap_free_type(void *p) { +coap_free_type(coap_memory_tag_t type UNUSED_PARAM, void *p) { free(p); } @@ -46,13 +46,7 @@ coap_free_type(void *p) { #define COAP_MAX_STRING_SIZE 12 #define COAP_MAX_STRINGS 8 -typedef struct { - coap_memory_tag_t type; - char data[]; -} coap_memory_item_t; - struct coap_string_t { - coap_memory_tag_t type; char data[COAP_MAX_STRING_SIZE]; }; @@ -74,34 +68,20 @@ coap_memory_init() { void * coap_malloc_type(coap_memory_tag_t type, size_t size) { struct memb *container = get_container(type); - coap_memory_item_t *item; assert(container); - if (size + sizeof(coap_memory_item_t) > container->size) { + if (size > container->size) { debug("coap_malloc_type: Requested memory exceeds maximum object size\n"); return NULL; } - item = (coap_memory_item_t *)memb_alloc(container); - if (item != NULL) { - item->type = type; - return item->data; - } else { - debug("coap_malloc_type: insufficient memory\n"); - return NULL; - } + return memb_alloc(container); } void -coap_free_type(void *object) { - coap_memory_item_t *item; - - if (object) { - item = (char *)object - sizeof(coap_memory_item_t); - - memb_free(get_container(item->type), item); - } +coap_free_type(coap_memory_tag_t type, void *object) { + memb_free(get_container(type), object); } #endif /* WITH_CONTIKI */ diff --git a/mem.h b/mem.h index cbecf73c..9e02f2be 100644 --- a/mem.h +++ b/mem.h @@ -44,11 +44,14 @@ void *coap_malloc_type(coap_memory_tag_t type, size_t size); /** * 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(). */ -void coap_free_type(void *p); +void coap_free_type(coap_memory_tag_t type, void *p); /** * Wrapper function to coap_malloc_type() for backwards compatibility. @@ -61,7 +64,7 @@ static inline void *coap_malloc(size_t size) { * Wrapper function to coap_free_type() for backwards compatibility. */ static inline void coap_free(void *object) { - coap_free_type(object); + coap_free_type(COAP_STRING, object); } #endif /* _COAP_MEM_H_ */