/*---------------------------------------------------------------------------*/ struct queuebuf * queuebuf_new_from_packetbuf(void) { struct queuebuf *buf; struct queuebuf_ref *rbuf; if(packetbuf_is_reference()) { rbuf = memb_alloc(&refbufmem); if(rbuf != NULL) { #if QUEUEBUF_STATS ++queuebuf_ref_len; #if CONTIKI_TARGET_NETSIM /* node_log("%d %d\n", queuebuf_len, queuebuf_ref_len);*/ #endif /* CONTIKI_TARGET_NETSIM */ #endif /* QUEUEBUF_STATS */ rbuf->len = packetbuf_datalen(); rbuf->ref = packetbuf_reference_ptr(); rbuf->hdrlen = packetbuf_copyto_hdr(rbuf->hdr); } else { PRINTF("queuebuf_new_from_packetbuf: could not allocate a reference queuebuf\n"); } return (struct queuebuf *)rbuf; } else { buf = memb_alloc(&bufmem); if(buf != NULL) { #if QUEUEBUF_STATS ++queuebuf_len; if(queuebuf_len == queuebuf_max_len + 1) { memb_free(&bufmem, buf); queuebuf_len--; return NULL; } #if CONTIKI_TARGET_NETSIM /* node_log("%d %d\n", queuebuf_len, queuebuf_ref_len);*/ #endif /* CONTIKI_TARGET_NETSIM */ #endif /* QUEUEBUF_STATS */ buf->len = packetbuf_copyto(buf->data); packetbuf_attr_copyto(buf->attrs, buf->addrs); } else { PRINTF("queuebuf_new_from_packetbuf: could not allocate a queuebuf\n"); } return buf; } }
/*---------------------------------------------------------------------------*/ #if QUEUEBUF_DEBUG struct queuebuf * queuebuf_new_from_packetbuf_debug(const char *file, int line) #else /* QUEUEBUF_DEBUG */ struct queuebuf * queuebuf_new_from_packetbuf(void) #endif /* QUEUEBUF_DEBUG */ { struct queuebuf *buf; struct queuebuf_ref *rbuf; if(packetbuf_is_reference()) { rbuf = memb_alloc(&refbufmem); if(rbuf != NULL) { #if QUEUEBUF_STATS ++queuebuf_ref_len; #endif /* QUEUEBUF_STATS */ rbuf->len = packetbuf_datalen(); rbuf->ref = packetbuf_reference_ptr(); rbuf->hdrlen = packetbuf_copyto_hdr(rbuf->hdr); } else { PRINTF("queuebuf_new_from_packetbuf: could not allocate a reference queuebuf\n"); } return (struct queuebuf *)rbuf; } else { buf = memb_alloc(&bufmem); if(buf != NULL) { #if QUEUEBUF_DEBUG list_add(queuebuf_list, buf); buf->file = file; buf->line = line; buf->time = clock_time(); #endif /* QUEUEBUF_DEBUG */ #if QUEUEBUF_STATS ++queuebuf_len; PRINTF("queuebuf len %d\n", queuebuf_len); printf("#A q=%d\n", queuebuf_len); if(queuebuf_len == queuebuf_max_len + 1) { memb_free(&bufmem, buf); queuebuf_len--; return NULL; } #endif /* QUEUEBUF_STATS */ buf->len = packetbuf_copyto(buf->data); packetbuf_attr_copyto(buf->attrs, buf->addrs); } else { PRINTF("queuebuf_new_from_packetbuf: could not allocate a queuebuf\n"); } return buf; } }
/*---------------------------------------------------------------------------*/ #if QUEUEBUF_DEBUG struct queuebuf * queuebuf_new_from_packetbuf_debug(const char *file, int line) #else /* QUEUEBUF_DEBUG */ struct queuebuf * queuebuf_new_from_packetbuf(void) #endif /* QUEUEBUF_DEBUG */ { struct queuebuf *buf; struct queuebuf_ref *rbuf; if(packetbuf_is_reference()) { rbuf = memb_alloc(&refbufmem); if(rbuf != NULL) { #if QUEUEBUF_STATS ++queuebuf_ref_len; #endif /* QUEUEBUF_STATS */ rbuf->len = packetbuf_datalen(); rbuf->ref = packetbuf_reference_ptr(); rbuf->hdrlen = packetbuf_copyto_hdr(rbuf->hdr); } else { PRINTF("queuebuf_new_from_packetbuf: could not allocate a reference queuebuf\n"); } return (struct queuebuf *)rbuf; } else { struct queuebuf_data *buframptr; buf = memb_alloc(&bufmem); if(buf != NULL) { #if QUEUEBUF_DEBUG list_add(queuebuf_list, buf); buf->file = file; buf->line = line; buf->time = clock_time(); #endif /* QUEUEBUF_DEBUG */ buf->ram_ptr = memb_alloc(&buframmem); #if WITH_SWAP /* If the allocation failed, store the qbuf in swap files */ if(buf->ram_ptr != NULL) { buf->location = IN_RAM; buframptr = buf->ram_ptr; } else { buf->location = IN_CFS; buf->swap_id = -1; tmpdata_qbuf = buf; buframptr = &tmpdata; } #else if(buf->ram_ptr == NULL) { PRINTF("queuebuf_new_from_packetbuf: could not queuebuf data\n"); memb_free(&bufmem, buf); return NULL; } buframptr = buf->ram_ptr; #endif buframptr->len = packetbuf_copyto(buframptr->data); packetbuf_attr_copyto(buframptr->attrs, buframptr->addrs); #if WITH_SWAP if(buf->location == IN_CFS) { if(queuebuf_flush_tmpdata() == -1) { /* We were unable to write the data in the swap */ memb_free(&bufmem, buf); return NULL; } } #endif #if QUEUEBUF_STATS ++queuebuf_len; PRINTF("queuebuf len %d\n", queuebuf_len); printf("#A q=%d\n", queuebuf_len); if(queuebuf_len == queuebuf_max_len + 1) { queuebuf_free(buf); queuebuf_len--; return NULL; } #endif /* QUEUEBUF_STATS */ } else { PRINTF("queuebuf_new_from_packetbuf: could not allocate a queuebuf\n"); } return buf; } }