ubuffer.c

Go to the documentation of this file.
00001 
00008 /******************************************************************************************
00009 *   include files 
00010 *******************************************************************************************/
00011 #include "uffs/ubuffer.h"
00012 #include "uffs/uffs_os.h"
00013 #include <string.h>
00014 
00015 /*
00016 
00017 usage:
00018 
00019   #define NODE_SIZE 32
00020   #define NODE_NUMS 1024
00021 
00022   static int my_data_buf[NODE_SIZE * NODE_NUMS / sizeof(int)];
00023   static struct ubufm dis;
00024 
00025   dis.node_pool = &my_data_buf;
00026   dis.node_size = NODE_SIZE;
00027   dis.node_nums = NODE_NUMS;
00028 
00029   uBufInit(&dis);
00030 
00031   void * p;
00032   p = uBufGet(&dis);
00033   ...
00034   uBufPut(p, &dis);
00035 
00036 notice:
00037     NODE_SIZE's lowest 2 bit must be 0, and NODE_SIZE must be large then or equal to 4.
00038     NODE_NUM must be larger than 1.
00039 
00040 */
00041 
00042 static int uBufLock(struct ubufm *dis)
00043 {
00044     return uffs_SemWait(dis->lock);
00045 }
00046 
00047 static int uBufUnlock(struct ubufm *dis)
00048 {
00049     return uffs_SemSignal(dis->lock);
00050 }
00051 
00052 /* init ubuffer data structure with given descriptor */
00053 int uBufInit(struct ubufm *dis)
00054 {
00055     unsigned int i;
00056     int *p1, *p2;
00057 
00058     if(dis == NULL) return -1;
00059     if(dis->node_nums < 1) return -1;
00060 
00061     uBufLock(dis);
00062     //initialize freeList. 
00063     dis->freeList = dis->node_pool;
00064     p1 = p1 = (int *)(dis->node_pool);
00065     for(i = 1; i < dis->node_nums; i++) {
00066         p2 = (int *)((char *)(dis->node_pool) + i * dis->node_size);
00067         *p1 = (int)p2;
00068         p1 = p2;
00069     }
00070     *p2 = (int)NULL;    //end of freeList
00071     uBufUnlock(dis);
00072 
00073     return 0;
00074 }
00075 
00076 
00077 /* get buffer from pool */
00078 void * uBufGet(struct ubufm *dis)
00079 {
00080     void *p;
00081 
00082     p = dis->freeList;
00083     if(p) {
00084         dis->freeList = (void *)(*((int *)(dis->freeList)));
00085     }
00086     return p;
00087 }
00088 
00089 /* get buffer from pool
00090  * this is Critical protect version,
00091  * you should use this version when multi-therad 
00092  * access the same buffer pool
00093  */
00094 void * uBufGetCt(struct ubufm *dis)
00095 {
00096     void *p;
00097 
00098     uBufLock(dis);
00099     p = dis->freeList;
00100     if(p) {
00101         dis->freeList = (void *)(*((int *)(dis->freeList)));
00102     }
00103     uBufUnlock(dis);
00104 
00105     return p;
00106 }
00107 
00108 /* put buffer to pool */
00109 int uBufPut(void *p, struct ubufm *dis)
00110 {
00111     if(p) {
00112         *((int *)p) = (int)(dis->freeList);
00113         dis->freeList = p;
00114         return 0;
00115     }
00116     return -1;
00117 }
00118 
00119 /* put buffer to pool, 
00120  * this is critical protect version,
00121  * you should use this version when multithread
00122  * access the same buffer pool
00123  */
00124 int uBufPutCt(void *p, struct ubufm *dis)
00125 {
00126     if(p) {
00127         uBufLock(dis);
00128         *((int *)p) = (int)(dis->freeList);
00129         dis->freeList = p;
00130         uBufUnlock(dis);
00131         return 0;
00132     }
00133     return -1;
00134 }
00135 
00136 /* get buffer pointer by index(offset) */
00137 void * uBufGetBufByIndex(unsigned int index, struct ubufm *dis)
00138 {
00139     return (char *)(dis->node_pool) + index * dis->node_size;
00140 }
00141 
00142 /* get index by pointer */
00143 int uBufGetIndex(void *p, struct ubufm *dis)
00144 {
00145     return ((char *)p - (char *)(dis->node_pool)) / dis->node_size;
00146 }

Generated on Sat Mar 17 15:45:44 2007 for uffs-doc by  doxygen 1.5.0