queuebuf.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include "contiki-net.h"
00047
00048 #include <string.h>
00049
00050 #ifdef QUEUEBUF_CONF_REF_NUM
00051 #define QUEUEBUF_REF_NUM QUEUEBUF_CONF_REF_NUM
00052 #else
00053 #define QUEUEBUF_REF_NUM 2
00054 #endif
00055
00056 struct queuebuf {
00057 #if QUEUEBUF_DEBUG
00058 struct queuebuf *next;
00059 const char *file;
00060 int line;
00061 clock_time_t time;
00062 #endif
00063 uint16_t len;
00064 uint8_t data[PACKETBUF_SIZE];
00065 struct packetbuf_attr attrs[PACKETBUF_NUM_ATTRS];
00066 struct packetbuf_addr addrs[PACKETBUF_NUM_ADDRS];
00067 };
00068
00069 struct queuebuf_ref {
00070 uint16_t len;
00071 uint8_t *ref;
00072 uint8_t hdr[PACKETBUF_HDR_SIZE];
00073 uint8_t hdrlen;
00074 };
00075
00076 MEMB(bufmem, struct queuebuf, QUEUEBUF_NUM);
00077 MEMB(refbufmem, struct queuebuf_ref, QUEUEBUF_REF_NUM);
00078
00079 #if QUEUEBUF_DEBUG
00080 #include "lib/list.h"
00081 LIST(queuebuf_list);
00082 #endif
00083
00084 #define DEBUG 0
00085 #if DEBUG
00086 #include <stdio.h>
00087 #define PRINTF(...) printf(__VA_ARGS__)
00088 #else
00089 #define PRINTF(...)
00090 #endif
00091
00092 #ifdef QUEUEBUF_CONF_STATS
00093 #define QUEUEBUF_STATS QUEUEBUF_CONF_STATS
00094 #else
00095 #define QUEUEBUF_STATS 0
00096 #endif
00097
00098 #if QUEUEBUF_STATS
00099 uint8_t queuebuf_len, queuebuf_ref_len, queuebuf_max_len;
00100 #endif
00101
00102
00103 void
00104 queuebuf_init(void)
00105 {
00106 memb_init(&bufmem);
00107 memb_init(&refbufmem);
00108 #if QUEUEBUF_STATS
00109 queuebuf_max_len = QUEUEBUF_NUM;
00110 #endif
00111 }
00112
00113 #if QUEUEBUF_DEBUG
00114 struct queuebuf *
00115 queuebuf_new_from_packetbuf_debug(const char *file, int line)
00116 #else
00117 struct queuebuf *
00118 queuebuf_new_from_packetbuf(void)
00119 #endif
00120 {
00121 struct queuebuf *buf;
00122 struct queuebuf_ref *rbuf;
00123
00124 if(packetbuf_is_reference()) {
00125 rbuf = memb_alloc(&refbufmem);
00126 if(rbuf != NULL) {
00127 #if QUEUEBUF_STATS
00128 ++queuebuf_ref_len;
00129 #endif
00130 rbuf->len = packetbuf_datalen();
00131 rbuf->ref = packetbuf_reference_ptr();
00132 rbuf->hdrlen = packetbuf_copyto_hdr(rbuf->hdr);
00133 } else {
00134 PRINTF("queuebuf_new_from_packetbuf: could not allocate a reference queuebuf\n");
00135 }
00136 return (struct queuebuf *)rbuf;
00137 } else {
00138 buf = memb_alloc(&bufmem);
00139 if(buf != NULL) {
00140 #if QUEUEBUF_DEBUG
00141 list_add(queuebuf_list, buf);
00142 buf->file = file;
00143 buf->line = line;
00144 buf->time = clock_time();
00145 #endif
00146 #if QUEUEBUF_STATS
00147 ++queuebuf_len;
00148 PRINTF("queuebuf len %d\n", queuebuf_len);
00149 printf("#A q=%d\n", queuebuf_len);
00150 if(queuebuf_len == queuebuf_max_len + 1) {
00151 memb_free(&bufmem, buf);
00152 queuebuf_len--;
00153 return NULL;
00154 }
00155 #endif
00156 buf->len = packetbuf_copyto(buf->data);
00157 packetbuf_attr_copyto(buf->attrs, buf->addrs);
00158 } else {
00159 PRINTF("queuebuf_new_from_packetbuf: could not allocate a queuebuf\n");
00160 }
00161 return buf;
00162 }
00163 }
00164
00165 void
00166 queuebuf_free(struct queuebuf *buf)
00167 {
00168 if(memb_inmemb(&bufmem, buf)) {
00169 memb_free(&bufmem, buf);
00170 #if QUEUEBUF_STATS
00171 --queuebuf_len;
00172 printf("#A q=%d\n", queuebuf_len);
00173 #endif
00174 #if QUEUEBUF_DEBUG
00175 list_remove(queuebuf_list, buf);
00176 #endif
00177 } else if(memb_inmemb(&refbufmem, buf)) {
00178 memb_free(&refbufmem, buf);
00179 #if QUEUEBUF_STATS
00180 --queuebuf_ref_len;
00181 #endif
00182 }
00183 }
00184
00185 void
00186 queuebuf_to_packetbuf(struct queuebuf *b)
00187 {
00188 struct queuebuf_ref *r;
00189
00190 if(memb_inmemb(&bufmem, b)) {
00191 packetbuf_copyfrom(b->data, b->len);
00192 packetbuf_attr_copyfrom(b->attrs, b->addrs);
00193 } else if(memb_inmemb(&refbufmem, b)) {
00194 r = (struct queuebuf_ref *)b;
00195 packetbuf_clear();
00196 packetbuf_copyfrom(r->ref, r->len);
00197 packetbuf_hdralloc(r->hdrlen);
00198 memcpy(packetbuf_hdrptr(), r->hdr, r->hdrlen);
00199 }
00200 }
00201
00202 void *
00203 queuebuf_dataptr(struct queuebuf *b)
00204 {
00205 struct queuebuf_ref *r;
00206
00207 if(memb_inmemb(&bufmem, b)) {
00208 return b->data;
00209 } else if(memb_inmemb(&refbufmem, b)) {
00210 r = (struct queuebuf_ref *)b;
00211 return r->ref;
00212 }
00213 return NULL;
00214 }
00215
00216 int
00217 queuebuf_datalen(struct queuebuf *b)
00218 {
00219 return b->len;
00220 }
00221
00222 rimeaddr_t *
00223 queuebuf_addr(struct queuebuf *b, uint8_t type)
00224 {
00225 return &b->addrs[type - PACKETBUF_ADDR_FIRST].addr;
00226 }
00227
00228 packetbuf_attr_t
00229 queuebuf_attr(struct queuebuf *b, uint8_t type)
00230 {
00231 return b->attrs[type].val;
00232 }
00233
00234 void
00235 queuebuf_debug_print(void)
00236 {
00237 #if QUEUEBUF_DEBUG
00238 struct queuebuf *q;
00239 printf("queuebuf_list: ");
00240 for(q = list_head(queuebuf_list); q != NULL;
00241 q = list_item_next(q)) {
00242 printf("%s,%d,%lu ", q->file, q->line, q->time);
00243 }
00244 printf("\n");
00245 #endif
00246 }
00247
00248