00001
00008
00009
00010
00011 #include "uffs/ubuffer.h"
00012 #include "uffs/uffs_os.h"
00013 #include <string.h>
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 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
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
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;
00071 uBufUnlock(dis);
00072
00073 return 0;
00074 }
00075
00076
00077
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
00090
00091
00092
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
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
00120
00121
00122
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
00137 void * uBufGetBufByIndex(unsigned int index, struct ubufm *dis)
00138 {
00139 return (char *)(dis->node_pool) + index * dis->node_size;
00140 }
00141
00142
00143 int uBufGetIndex(void *p, struct ubufm *dis)
00144 {
00145 return ((char *)p - (char *)(dis->node_pool)) / dis->node_size;
00146 }