[Tinyos-2-commits]
CVS: tinyos-2.x/tos/system LruIntCacheC.nc, NONE,
1.1.2.1 LruIntCacheP.nc, NONE, 1.1.2.1 PoolC.nc, NONE,
1.1.2.1 PoolP.nc, NONE, 1.1.2.1 QueueC.nc, NONE, 1.1.2.1
Phil Levis
scipio at users.sourceforge.net
Mon Jun 26 09:52:19 PDT 2006
Update of /cvsroot/tinyos/tinyos-2.x/tos/system
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv15102/system
Added Files:
Tag: tinyos-2_0_devel-BRANCH
LruIntCacheC.nc LruIntCacheP.nc PoolC.nc PoolP.nc QueueC.nc
Log Message:
Basic data structures.
--- NEW FILE: LruIntCacheC.nc ---
/*
* "Copyright (c) 2006 The Regents of the University of California.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice, the following
* two paragraphs and the author appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
*/
/**
* An LRU cache of integers, where insertion represents use.
*
* @author Rodrigo Fonseca
*/
generic configuration LruIntCacheC(typedef cache_key_t at integer(), uint8_t CACHE_SIZE) {
provides interface Cache<cache_key_t>;
}
implementation {
components MainC, new LruIntCacheP(cache_key_t, CACHE_SIZE) as CacheP;
Cache = CacheP;
MainC.SoftwareInit -> CacheP;
}
--- NEW FILE: LruIntCacheP.nc ---
/*
* "Copyright (c) 2006 The Regents of the University of California.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice, the following
* two paragraphs and the author appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
*/
/**
* An LRU cache that stores integers. Inserting an element not in
* the cache will replace the oldest.
* Inserting an element already in the cache will refresh its age.
*
* @author Rodrigo Fonseca
* @author Philip Levis
*/
generic module LruIntCacheP(typedef cache_key_t @integer(), uint8_t size) {
provides {
interface Init;
interface Cache<cache_key_t>;
}
}
implementation {
cache_key_t cache[size];
uint8_t first;
uint8_t count;
command error_t Init.init() {
first = 0;
count = 0;
return SUCCESS;
}
void printCache() {
#ifdef TOSSIM
int i;
dbg("Cache","Cache:");
for (i = 0; i < count; i++) {
dbg_clear("Cache", " %08x", cache[i]);
if (i == first)
dbg_clear("Cache","*");
}
dbg_clear("Cache","\n");
#endif
}
/* if key is in cache returns the index (offset by first), otherwise returns count */
uint8_t lookup(cache_key_t key) {
uint8_t i;
cache_key_t k;
for (i = 0; i < count; i++) {
k = cache[(i + first) % size];
if (k == key)
break;
}
return i;
}
/* remove the entry with index i (relative to first) */
void remove(uint8_t i) {
uint8_t j;
if (i >= count)
return;
if (i == 0) {
//shift all by moving first
first = (first + 1) % size;
} else {
//shift everyone down
for (j = i; j < count; j++) {
cache[(j + first) % size] = cache[(j + first + 1) % size];
}
}
count--;
}
command void Cache.insert(cache_key_t key) {
uint8_t i;
if (count == size ) {
//remove someone. If item not in
//cache, remove the first item.
//otherwise remove the item temporarily for
//reinsertion. This moves the item up in the
//LRU stack.
i = lookup(key);
remove(i % count);
}
//now count < size
cache[(first + count) % size] = key;
count++;
}
command bool Cache.lookup(cache_key_t key) {
return (lookup(key) < count);
}
command void Cache.flush() {
call Init.init();
}
}
--- NEW FILE: PoolC.nc ---
/* $Id: PoolC.nc,v 1.1.2.1 2006/06/26 16:52:17 scipio Exp $ */
/*
* Copyright (c) 2006 Stanford University.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - 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.
* - Neither the name of the Stanford University 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 COPYRIGHT HOLDERS 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 STANFORD
* UNIVERSITY OR ITS 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.
*/
/**
* A general dynamic memory pool component.
*
* @author Philip Levis
* @date $Date: 2006/06/26 16:52:17 $
*/
generic configuration PoolC(typedef pool_t, uint8_t POOL_SIZE) {
provides interface Pool<pool_t>;
}
implementation {
components MainC, new PoolP(pool_t, POOL_SIZE);
MainC.SoftwareInit -> PoolP;
Pool = PoolP;
}
--- NEW FILE: PoolP.nc ---
/* $Id: PoolP.nc,v 1.1.2.1 2006/06/26 16:52:17 scipio Exp $ */
/*
* Copyright (c) 2006 Stanford University.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - 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.
* - Neither the name of the Stanford University 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 COPYRIGHT HOLDERS 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 STANFORD
* UNIVERSITY OR ITS 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.
*/
/**
* Implementation of a general dynamic memory pool component.
*
* @author Philip Levis
* @author Kyle Jamieson
* @date $Date: 2006/06/26 16:52:17 $
*/
generic module PoolP(typedef pool_t, uint8_t size) {
provides {
interface Init;
interface Pool<pool_t>;
}
}
implementation {
uint8_t free;
uint8_t index;
pool_t* queue[size];
pool_t pool[size];
command error_t Init.init() {
int i;
for (i = 0; i < size; i++) {
queue[i] = &pool[i];
}
free = size;
index = 0;
return SUCCESS;
}
command bool Pool.empty() {
return free == 0;
}
command uint8_t Pool.size() {
return free;
}
command uint8_t Pool.maxSize() {
return size;
}
command pool_t* Pool.get() {
if (free) {
pool_t* rval = queue[index];
queue[index] = NULL;
free--;
index = (index + 1) % size;
return rval;
}
return NULL;
}
command error_t Pool.put(pool_t* newVal) {
if (free >= size) {
return FAIL;
}
else {
uint8_t emptyIndex = (index + free) % size;
queue[emptyIndex] = newVal;
free++;
return SUCCESS;
}
}
}
--- NEW FILE: QueueC.nc ---
/* $Id: QueueC.nc,v 1.1.2.1 2006/06/26 16:52:17 scipio Exp $ */
/*
* Copyright (c) 2006 Stanford University.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - 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.
* - Neither the name of the Stanford University 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 COPYRIGHT HOLDERS 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 STANFORD
* UNIVERSITY OR ITS 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.
*/
/**
* A general FIFO queue component, whose queue has a bounded size.
*
* @author Philip Levis
* @date $Date: 2006/06/26 16:52:17 $
*/
generic module QueueC(typedef queue_t, uint8_t QUEUE_SIZE) {
provides interface Queue<queue_t>;
}
implementation {
queue_t queue[QUEUE_SIZE];
uint8_t head = 0;
uint8_t tail = 0;
uint8_t size = 0;
command bool Queue.empty() {
return size == 0;
}
command uint8_t Queue.size() {
return size;
}
command uint8_t Queue.maxSize() {
return QUEUE_SIZE;
}
command queue_t Queue.head() {
return queue[head];
}
void printQueue() {
#ifdef TOSSIM
int i, j;
dbg("QueueC", "head <-");
for (i = head; i < head + size; i++) {
dbg_clear("QueueC", "[");
for (j = 0; j < sizeof(queue_t); j++) {
uint8_t v = ((uint8_t*)&queue[i % QUEUE_SIZE])[j];
dbg_clear("QueueC", "%0.2hhx", v);
}
dbg_clear("QueueC", "] ");
}
dbg_clear("QueueC", "<- tail\n");
#endif
}
command queue_t Queue.dequeue() {
queue_t t = call Queue.head();
dbg("QueueC", "%s: size is %hhu\n", __FUNCTION__, size);
if (!call Queue.empty()) {
head++;
head %= QUEUE_SIZE;
size--;
printQueue();
}
return t;
}
command error_t Queue.enqueue(queue_t newVal) {
if (call Queue.size() < call Queue.maxSize()) {
dbg("QueueC", "%s: size is %hhu\n", __FUNCTION__, size);
queue[tail] = newVal;
tail++;
tail %= QUEUE_SIZE;
size++;
printQueue();
return SUCCESS;
}
else {
return FAIL;
}
}
command queue_t Queue.element(uint8_t index) {
index += head;
index %= QUEUE_SIZE;
return queue[index];
}
}
More information about the Tinyos-2-commits
mailing list