From ecda1426acd930f9b60880cd2db852bbc006be36 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Wed, 11 Mar 2015 14:59:40 +0100 Subject: [PATCH] resource.c: Increase use of coap_malloc_type() and coap_free_type() coap_malloc_type() and coap_free_type() provide an abstract API for dynamic memory management on different platforms. resource.c still is in transition and needs more cleanup. --- include/coap/mem.h | 2 +- src/mem.c | 6 ++++++ src/resource.c | 33 +++++++++------------------------ 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/include/coap/mem.h b/include/coap/mem.h index 7f72ed58..32b7f227 100644 --- a/include/coap/mem.h +++ b/include/coap/mem.h @@ -28,7 +28,7 @@ void coap_memory_init(void); * memory management on constrained devices. */ typedef enum { - COAP_STRING, COAP_ATTRIBUTE_NAME, COAP_ATTRIBUTE_VALUE, COAP_PACKET, COAP_NODE, COAP_CONTEXT, COAP_ENDPOINT, COAP_PDU, COAP_PDU_BUF + 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 } coap_memory_tag_t; #ifndef WITH_LWIP diff --git a/src/mem.c b/src/mem.c index e2617982..75d04b5f 100644 --- a/src/mem.c +++ b/src/mem.c @@ -69,6 +69,8 @@ MEMB(packet_storage, coap_packetbuf_t, COAP_MAX_PACKETS); MEMB(node_storage, coap_queue_t, COAP_PDU_MAXCNT); MEMB(pdu_storage, coap_pdu_t, COAP_PDU_MAXCNT); MEMB(pdu_buf_storage, coap_packetbuf_t, COAP_PDU_MAXCNT); +MEMB(resource_storage, coap_resource_t, COAP_MAX_RESOURCES); +MEMB(attribute_storage, coap_attr_t, COAP_MAX_ATTRIBUTES); static struct memb * get_container(coap_memory_tag_t type) { @@ -77,6 +79,8 @@ get_container(coap_memory_tag_t type) { case COAP_NODE: return &node_storage; case COAP_PDU: return &pdu_storage; case COAP_PDU_BUF: return &pdu_buf_storage; + case COAP_RESOURCE: return &resource_storage; + case COAP_RESOURCEATTR: return &attribute_storage; default: return &string_storage; } @@ -89,6 +93,8 @@ coap_memory_init(void) { memb_init(&node_storage); memb_init(&pdu_storage); memb_init(&pdu_buf_storage); + memb_init(&resource_storage); + memb_init(&attribute_storage); } void * diff --git a/src/resource.c b/src/resource.c index 53895086..520b2b93 100644 --- a/src/resource.c +++ b/src/resource.c @@ -43,14 +43,10 @@ ((coap_##Type##_t *)memb_alloc(&(Type##_storage))) #define COAP_FREE_TYPE(Type, Object) memb_free(&(Type##_storage), (Object)) -MEMB(resource_storage, coap_resource_t, COAP_MAX_RESOURCES); -MEMB(attribute_storage, coap_attr_t, COAP_MAX_ATTRIBUTES); MEMB(subscription_storage, coap_subscription_t, COAP_MAX_SUBSCRIBERS); void coap_resources_init() { - memb_init(&resource_storage); - memb_init(&attribute_storage); memb_init(&subscription_storage); } @@ -292,14 +288,11 @@ coap_resource_t * coap_resource_init(const unsigned char *uri, size_t len, int flags) { coap_resource_t *r; -#ifdef WITH_POSIX - r = (coap_resource_t *)coap_malloc(sizeof(coap_resource_t)); -#endif #ifdef WITH_LWIP r = (coap_resource_t *)memp_malloc(MEMP_COAP_RESOURCE); #endif -#ifdef WITH_CONTIKI - r = (coap_resource_t *)memb_alloc(&resource_storage); +#ifndef WITH_LWIP + r = (coap_resource_t *)coap_malloc_type(COAP_RESOURCE, sizeof(coap_resource_t)); #endif if (r) { memset(r, 0, sizeof(coap_resource_t)); @@ -327,14 +320,11 @@ coap_add_attr(coap_resource_t *resource, if (!resource || !name) return NULL; -#ifdef WITH_POSIX - attr = (coap_attr_t *)coap_malloc(sizeof(coap_attr_t)); -#endif #ifdef WITH_LWIP attr = (coap_attr_t *)memp_malloc(MEMP_COAP_RESOURCEATTR); #endif -#ifdef WITH_CONTIKI - attr = (coap_attr_t *)memb_alloc(&attribute_storage); +#ifndef WITH_LWIP + attr = (coap_attr_t *)coap_malloc_type(COAP_RESOURCEATTR, sizeof(coap_attr_t)); #endif if (attr) { @@ -380,14 +370,12 @@ coap_delete_attr(coap_attr_t *attr) { coap_free(attr->name.s); if (attr->flags & COAP_ATTR_FLAGS_RELEASE_VALUE) coap_free(attr->value.s); -#ifdef WITH_POSIX - coap_free(attr); -#endif + #ifdef WITH_LWIP memp_free(MEMP_COAP_RESOURCEATTR, attr); #endif -#ifdef WITH_CONTIKI - memb_free(&attribute_storage, attr); +#ifndef WITH_LWIP + coap_free_type(COAP_RESOURCEATTR, attr); #endif } @@ -428,14 +416,11 @@ coap_free_resource(coap_resource_t *resource) { /* free all elements from resource->subscribers */ LL_FOREACH_SAFE(resource->subscribers, obs, otmp) COAP_FREE_TYPE(subscription, obs); -#ifdef WITH_POSIX - coap_free(resource); -#endif #ifdef WITH_LWIP memp_free(MEMP_COAP_RESOURCE, resource); #endif -#ifdef WITH_CONTIKI - memb_free(&resource_storage, resource); +#ifndef WITH_LWIP + coap_free_type(COAP_RESOURCE, resource); #endif /* WITH_CONTIKI */ }