[Tinyos-2-commits] CVS: tinyos-2.x/tos/lib/net/collection Cache.nc, NONE, 1.1.2.1 CacheC.nc, NONE, 1.1.2.1 CacheP.nc, NONE, 1.1.2.1

Rodrigo Fonseca rfonseca76 at users.sourceforge.net
Thu Jun 22 06:45:41 PDT 2006


Update of /cvsroot/tinyos/tinyos-2.x/tos/lib/net/collection
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv22295

Added Files:
      Tag: tinyos-2_0_devel-BRANCH
	Cache.nc CacheC.nc CacheP.nc 
Log Message:
Initial untested checkin of a cache module


--- NEW FILE: Cache.nc ---
/* Very simple cache of a single type */
interface Cache<t> {
    /* Inserts an item in the cache, evicting a previous one if 
     * necessary.
     * An atomic lookup after insert should return true.
     */
    command void insert(t item);

    /* Returns true if item is in the cache, false otherwise */
    command bool lookup(t item);
    command errot_t remove(t item);
}


--- NEW FILE: CacheC.nc ---
generic configuration CacheC(typedef key_t, uint8_t CACHE_SIZE) {
    provides interface Cache<key_t>;
}
implementation {
    components MainC, new CacheP(key_t, CACHE_SIZE);

    MainC.SoftwareInit -> CacheP;
}

--- NEW FILE: CacheP.nc ---
/* An LRU implementation of a simple cache.
 * Insert on an element not in the cache will replace the oldest.
 * Insert on an element already in the cache will refresh its age.
 */
generic module CacheP(typedef key_t, uint8_t size) {
    provides {
        interface Init;
        interface Cache<key_t>
    }
}
implementation {
    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(key_t key) {
        uint8_t i;
        for (i = 0; i < count; i++) {
           if (cache[(i + first) % size] == 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(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(key_t key) {
        return (lookup(key) < count);
    }

    /* Removes the item if in the cache.
     * Returns SUCCESS if item was in cache,
     *         FAIL if item was not in cache */
    command error_t Cache.remove(key_t key) {
    }

}



More information about the Tinyos-2-commits mailing list