00001
00007 #define DKUTIL_C_QUEUE_C
00008 #include "dkcQueue.h"
00009 #include "dkcStdio.h"
00010
00011
00012 DKC_QUEUE* WINAPI dkcAllocQueue(size_t numof__,size_t offsetof__){
00013 DKC_QUEUE *p;
00014 size_t size = (numof__) * offsetof__;
00015 if(0==size) return NULL;
00016 p = (DKC_QUEUE *)dkcAllocate(sizeof(DKC_QUEUE));
00017 if(NULL==p) return NULL;
00018 p->mBuffer = (BYTE *)dkcAllocate(size);
00019
00020 if(NULL==p->mBuffer) goto Error;
00021
00022 p->mExit = 0;
00023 p->mEntrance = 0;
00024 p->mSize = size;
00025 p->mOffsetOf = offsetof__;
00026 p->mCounter = 0;
00027
00028 return p;
00029 Error:
00030 dkcFree((void **)&p);
00031 return NULL;
00032 }
00033 int WINAPI dkcFreeQueue(DKC_QUEUE **ptr){
00034 if(NULL==ptr || *ptr==NULL || NULL==(*ptr)->mBuffer)
00035 return edk_ArgumentException;
00036
00037 dkcFree((void **)&((*ptr)->mBuffer));
00038 return dkcFree((void **)ptr);
00039 }
00040
00041
00042 int WINAPI dkcQueuePush(DKC_QUEUE *ptr,const void *data){
00043 BYTE *tp;
00044 dkcmNOT_ASSERT(NULL==ptr);
00045
00046 tp = ptr->mBuffer;
00047 if(
00048 ptr->mCounter >= ptr->mSize / ptr->mOffsetOf)
00049 {
00050 return edk_FAILED;
00051 }
00052 else if(ptr->mEntrance >= ptr->mSize)
00053 {
00054 ptr->mEntrance = 0;
00055 }
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 memcpy(&tp[ptr->mEntrance],data,ptr->mOffsetOf);
00084
00085 ptr->mEntrance += ptr->mOffsetOf;
00086 ptr->mCounter++;
00087
00088 return edk_SUCCEEDED;
00089 }
00090
00091
00092 int WINAPI dkcQueueDynamicPush(DKC_QUEUE *ptr,const void *data)
00093 {
00094 int result;
00095 void *NewPtr;
00096 size_t want_size;
00097
00098 result = dkcQueuePush(ptr,data);
00099
00100 if(DKUTIL_SUCCEEDED(result)) return result;
00101
00102
00103 {
00104 want_size = dkcReallocateSizeFunction(ptr->mSize,ptr->mOffsetOf);
00105 if(DKUTIL_FAILED(
00106 dkcReallocate(&NewPtr,want_size,(void **)&ptr->mBuffer)
00107 )){
00108 return edk_FAILED;
00109 }
00110 ptr->mBuffer =(BYTE *)NewPtr;
00111 ptr->mSize = want_size;
00112 }
00113
00114 return dkcQueuePush(ptr,data);
00115
00116 }
00117 void WINAPI dkcQueuePop(DKC_QUEUE *ptr)
00118 {
00119 dkcmNOT_ASSERT(NULL==ptr);
00120 ptr->mExit += ptr->mOffsetOf;
00121 if(ptr->mCounter <= 0)
00122 {
00123 return;
00124 }
00125 ptr->mCounter--;
00126
00127 if(ptr->mExit >= ptr->mSize)
00128 {
00129 ptr->mExit = 0;
00130 }
00131 return;
00132 }
00133 int WINAPI dkcQueueTop(DKC_QUEUE *ptr,void *get_data){
00134 BYTE *tp;
00135
00136 dkcmNOT_ASSERT(NULL==ptr || NULL==get_data);
00137
00138
00139
00140 if(0==ptr->mCounter){
00141 return edk_FAILED;
00142 }
00143 tp = ptr->mBuffer;
00144
00145 memcpy(get_data,&tp[ptr->mExit],ptr->mOffsetOf);
00146
00147 return edk_SUCCEEDED;
00148
00149
00150 }
00151
00152 void WINAPI dkcQueueClear(DKC_QUEUE *ptr){
00153 if(NULL==ptr) return;
00154
00155 ptr->mExit = 0;
00156 ptr->mEntrance = 0;
00157 ptr->mCounter = 0;
00158 }
00159
00160 size_t WINAPI dkcQueueSize(DKC_QUEUE *ptr){
00161 return ptr->mCounter;
00162 }
00163
00164 BOOL WINAPI dkcQueueIsEmpty(DKC_QUEUE *ptr){
00165 return (dkcQueueSize(ptr)==0);
00166 }
00167
00168 int WINAPI dkcQueueSerialize(const DKC_QUEUE *ptr,DKC_SERIALIZE *se)
00169 {
00170 int id = edkcSerializeIDQueue;
00171 dkcmNOT_ASSERT(NULL==ptr);
00172 dkcSerializeWrite(se,&id,sizeof(id));
00173 dkcSerializeWrite(se,ptr,sizeof(DKC_QUEUE));
00174 return dkcSerializeWrite(se,ptr->mBuffer,ptr->mSize);
00175 }
00176
00177
00178 DKC_QUEUE* WINAPI dkcAllocQueueDeserialize(DKC_DESERIALIZE *se){
00179 DKC_QUEUE *p;
00180 DKC_QUEUE t;
00181 size_t read;
00182 int id;
00183
00184 dkcDeserializeRead(se,&id,sizeof(id),&read);
00185 if(id != edkcSerializeIDQueue)
00186 {
00187 return NULL;
00188 }
00189 dkcDeserializeRead(se,&t,sizeof(t),&read);
00190
00191 p = dkcAllocQueue(t.mSize,t.mOffsetOf);
00192 if(NULL==p) return NULL;
00193
00194 p->mEntrance = t.mEntrance;
00195 p->mExit = t.mExit;
00196 dkcDeserializeRead(se,p->mBuffer,p->mSize,&read);
00197
00198 dkcmNOT_ASSERT(read != p->mSize);
00199
00200 return p;
00201 }