use static buffers for CoAP PDUs

This commit is contained in:
Olaf Bergmann committed 2012-01-02 21:39:52 +01:00
1 parent f7c41a85f5
commit a9dc65315b
1 file changed
+27 -4
+27 -4
View File
@@ -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