osd-contiki/core/net/rime/queuebuf.c

167 lines
4.7 KiB
C
Raw Normal View History

2007-03-31 20:31:27 +02:00
/**
* \addtogroup rimequeuebuf
* @{
*/
/*
* Copyright (c) 2006, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
2007-03-31 20:31:27 +02:00
* $Id: queuebuf.c,v 1.8 2007/03/31 18:31:28 adamdunkels Exp $
*/
/**
* \file
2007-03-30 01:18:47 +02:00
* Implementation of the Rime queue buffers
* \author
* Adam Dunkels <adam@sics.se>
*/
#include "net/rime/queuebuf.h"
2007-03-15 10:56:15 +01:00
#include <string.h> /* for memcpy() */
#ifdef QUEUEBUF_CONF_NUM
#define QUEUEBUF_NUM QUEUEBUF_CONF_NUM
#else
#define QUEUEBUF_NUM 2
#endif
#ifdef QUEUEBUF_CONF_REF_NUM
#define QUEUEBUF_REF_NUM QUEUEBUF_CONF_REF_NUM
#else
#define QUEUEBUF_REF_NUM 2
#endif
struct queuebuf {
u16_t len;
2007-03-13 11:28:52 +01:00
u8_t data[RIMEBUF_SIZE + RIMEBUF_HDR_SIZE];
};
struct queuebuf_ref {
u16_t len;
u8_t *ref;
2007-03-13 11:28:52 +01:00
u8_t hdr[RIMEBUF_HDR_SIZE];
u8_t hdrlen;
};
MEMB(bufmem, struct queuebuf, QUEUEBUF_NUM);
MEMB(refbufmem, struct queuebuf_ref, QUEUEBUF_REF_NUM);
2007-03-31 20:31:27 +02:00
#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif
/*---------------------------------------------------------------------------*/
void
queuebuf_init(void)
{
memb_init(&bufmem);
memb_init(&refbufmem);
}
/*---------------------------------------------------------------------------*/
struct queuebuf *
queuebuf_new_from_rimebuf(void)
{
struct queuebuf *buf;
struct queuebuf_ref *rbuf;
if(rimebuf_is_reference()) {
rbuf = memb_alloc(&refbufmem);
if(rbuf != NULL) {
2007-03-13 11:28:52 +01:00
rbuf->len = rimebuf_datalen();
rbuf->ref = rimebuf_reference_ptr();
rbuf->hdrlen = rimebuf_copyto_hdr(rbuf->hdr);
2007-03-31 20:31:27 +02:00
} else {
PRINTF("queuebuf_new_from_rimebuf: could not allocate a reference queuebuf\n");
}
return (struct queuebuf *)rbuf;
} else {
buf = memb_alloc(&bufmem);
if(buf != NULL) {
buf->len = rimebuf_copyto(buf->data);
2007-03-31 20:31:27 +02:00
} else {
PRINTF("queuebuf_new_from_rimebuf: could not allocate a queuebuf\n");
}
return buf;
}
}
/*---------------------------------------------------------------------------*/
void
queuebuf_free(struct queuebuf *buf)
{
if(memb_inmemb(&bufmem, buf)) {
memb_free(&bufmem, buf);
} else if(memb_inmemb(&refbufmem, buf)) {
memb_free(&refbufmem, buf);
}
}
/*---------------------------------------------------------------------------*/
void
queuebuf_to_rimebuf(struct queuebuf *b)
{
struct queuebuf_ref *r;
if(memb_inmemb(&bufmem, b)) {
rimebuf_copyfrom(b->data, b->len);
} else if(memb_inmemb(&refbufmem, b)) {
r = (struct queuebuf_ref *)b;
rimebuf_clear();
rimebuf_copyfrom(r->ref, r->len);
rimebuf_hdralloc(r->hdrlen);
memcpy(rimebuf_hdrptr(), r->hdr, r->hdrlen);
}
}
/*---------------------------------------------------------------------------*/
void *
queuebuf_dataptr(struct queuebuf *b)
{
struct queuebuf_ref *r;
if(memb_inmemb(&bufmem, b)) {
return b->data;
} else if(memb_inmemb(&refbufmem, b)) {
r = (struct queuebuf_ref *)b;
return r->ref;
}
return NULL;
}
/*---------------------------------------------------------------------------*/
int
queuebuf_datalen(struct queuebuf *b)
{
return b->len;
}
/*---------------------------------------------------------------------------*/
2007-03-31 20:31:27 +02:00
/** @} */