From 1961caeebccd611ed01c68ecc1c4ca7d8b6f73a2 Mon Sep 17 00:00:00 2001 From: chrysn Date: Sun, 2 Jun 2013 16:10:43 +0200 Subject: [PATCH] use lwip pbufs for pdu allocation so far, this only replaces the allocator; it is not used yet for zero-copy operations. --- pdu.c | 31 +++++++++++++++++++++++++------ pdu.h | 9 +++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/pdu.c b/pdu.c index ea8ab87a..5b36395a 100644 --- a/pdu.c +++ b/pdu.c @@ -56,6 +56,9 @@ coap_pdu_t * coap_pdu_init(unsigned char type, unsigned char code, unsigned short id, size_t size) { coap_pdu_t *pdu; +#ifdef WITH_LWIP + struct pbuf *p; +#endif assert(size <= COAP_MAX_PDU_SIZE); /* Size must be large enough to fit the header. */ @@ -63,16 +66,27 @@ coap_pdu_init(unsigned char type, unsigned char code, return NULL; /* size must be large enough for hdr */ -#ifndef WITH_CONTIKI +#ifdef WITH_POSIX pdu = coap_malloc(sizeof(coap_pdu_t) + size); -#else /* WITH_CONTIKI */ +#endif +#ifdef WITH_CONTIKI pdu = (coap_pdu_t *)memb_alloc(&pdu_storage); -#endif /* WITH_CONTIKI */ +#endif +#ifdef WITH_LWIP + p = pbuf_alloc(PBUF_TRANSPORT, sizeof(coap_pdu_t) + size, PBUF_RAM); + if (p != NULL) + pdu = p->payload; + else + pdu = NULL; +#endif if (pdu) { coap_pdu_clear(pdu, size); pdu->hdr->id = id; pdu->hdr->type = type; pdu->hdr->code = code; +#ifdef WITH_LWIP + pdu->pbuf = p; +#endif } return pdu; } @@ -96,11 +110,16 @@ coap_new_pdu() { void coap_delete_pdu(coap_pdu_t *pdu) { -#ifndef WITH_CONTIKI +#ifdef WITH_POSIX coap_free( pdu ); -#else /* WITH_CONTIKI */ +#endif +#ifdef WITH_LWIP + if (pdu != NULL) /* accepting double free as the other implementation accept that too */ + pbuf_free(pdu->pbuf); +#endif +#ifdef WITH_CONTIKI memb_free(&pdu_storage, pdu); -#endif /* WITH_CONTIKI */ +#endif } int diff --git a/pdu.h b/pdu.h index 2cf04f88..638a12d2 100644 --- a/pdu.h +++ b/pdu.h @@ -13,6 +13,10 @@ #include "coap_list.h" #include "uri.h" +#ifdef WITH_LWIP +#include +#endif + /* pre-defined constants that reflect defaults for CoAP */ #define COAP_DEFAULT_RESPONSE_TIMEOUT 2 /* response timeout in seconds */ @@ -206,6 +210,11 @@ typedef struct { unsigned short max_delta; /**< highest option number */ unsigned short length; /**< PDU length (including header, options, data) */ unsigned char *data; /**< payload */ + +#ifdef WITH_LWIP + struct pbuf *pbuf; /**< lwIP PBUF. The allocated coap_pdu_t will always reside inside the pbuf's payload, but the pointer has to be kept because no exact offset can be given. */ +#endif + } coap_pdu_t; /** Options in coap_pdu_t are accessed with the macro COAP_OPTION. */