Use type tag in coap_free_type().

This commit is contained in:
Olaf Bergmann committed 2014-06-18 23:35:19 +02:00
1 parent 6dcf3c7128
commit 77183cdc48
2 files changed
+10 -27

No files matched your search

+5 -25
View File
@@ -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 */
+5 -2
View File
@@ -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_ */