From a9dc65315b8fbe5e39b07ca46e833a1072a6577e Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Mon, 2 Jan 2012 21:39:52 +0100 Subject: [PATCH] use static buffers for CoAP PDUs --- pdu.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/pdu.c b/pdu.c index 5170ff19..b40d5bfb 100644 --- a/pdu.c +++ b/pdu.c @@ -16,22 +16,41 @@ #endif #include "debug.h" -#include "mem.h" #include "pdu.h" #include "option.h" #include "encode.h" +#ifdef WITH_CONTIKI +#include "memb.h" + +typedef unsigned char _pdu[COAP_MAX_PDU_SIZE]; + +MEMB(pdu_storage, _pdu, COAP_PDU_MAXCNT); + +void +coap_pdu_resources_init() { + memb_init(&pdu_storage); +} +#else /* WITH_CONTIKI */ +#include "mem.h" +#endif /* WITH_CONTIKI */ + coap_pdu_t * coap_pdu_init(unsigned char type, unsigned char code, unsigned short id, size_t size) { coap_pdu_t *pdu; + assert(size <= COAP_MAX_PDU_SIZE); /* Size must be large enough to fit the header. */ - if (size < sizeof(coap_hdr_t)) + if (size < sizeof(coap_hdr_t) || size > COAP_MAX_PDU_SIZE) return NULL; - //size must be large enough for hdr + /* size must be large enough for hdr */ +#ifndef WITH_CONTIKI pdu = coap_malloc(sizeof(coap_pdu_t) + size); +#else /* WITH_CONTIKI */ + pdu = (coap_pdu_t *)memb_alloc(&pdu_storage); +#endif /* WITH_CONTIKI */ if (pdu) { memset(pdu, 0, sizeof(coap_pdu_t) + size); pdu->max_size = size; @@ -45,7 +64,7 @@ coap_pdu_init(unsigned char type, unsigned char code, pointer is moved to the back */ pdu->length = sizeof(coap_hdr_t); pdu->data = (unsigned char *)pdu->hdr + pdu->length; - } + } return pdu; } @@ -64,7 +83,11 @@ coap_new_pdu() { void coap_delete_pdu(coap_pdu_t *pdu) { +#ifndef WITH_CONTIKI coap_free( pdu ); +#else /* WITH_CONTIKI */ + memb_free(&pdu_storage, pdu); +#endif /* WITH_CONTIKI */ } int