mirror of
https://github.com/obgm/libcoap.git
synced 2026-11-08 02:22:54 +03:00
use lwip pbufs for pdu allocation
so far, this only replaces the allocator; it is not used yet for zero-copy operations.
This commit is contained in:
1 parent
2a6f04dd16
commit
1961caeebc
2 files changed
+34
-6
No files matched your search
@@ -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
|
||||
|
||||
@@ -13,6 +13,10 @@
|
||||
#include "coap_list.h"
|
||||
#include "uri.h"
|
||||
|
||||
#ifdef WITH_LWIP
|
||||
#include <lwip/pbuf.h>
|
||||
#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. */
|
||||
|
||||
Reference in new issue
Block a user