00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00106 #ifndef DISABLE_DATASTRUCTURE
00107
00108 #include <stdio.h>
00109 #include <stdlib.h>
00110 #include <stdbool.h>
00111 #include <string.h>
00112 #include "qDecoder.h"
00113 #include "qInternal.h"
00114
00126 size_t qQueueSize(int max, size_t objsize) {
00127 size_t memsize = objsize * max;
00128 return memsize;
00129 }
00130
00160 int qQueueInit(Q_QUEUE *queue, void* datamem, size_t datamemsize, size_t objsize) {
00161 if(queue == NULL || datamem == NULL || datamemsize <= 0 || objsize <= 0) return 0;
00162
00163
00164 int max = datamemsize / objsize;
00165 if(max < 1) return false;
00166
00167
00168 queue->max = max;
00169 queue->objsize = objsize;
00170 queue->objarr = datamem;
00171 qQueueClear(queue);
00172
00173 return max;
00174 }
00175
00187 bool qQueueClear(Q_QUEUE *queue) {
00188 queue->used = 0;
00189 queue->head = 0;
00190 queue->tail = 0;
00191
00192 return true;
00193 }
00194
00203 bool qQueuePush(Q_QUEUE *queue, const void *object) {
00204 if(queue == NULL || object == NULL) return false;
00205
00206
00207 if(queue->used == queue->max) {
00208 DEBUG("Queue full.");
00209 return false;
00210 }
00211
00212
00213 void *dp = queue->objarr + (queue->objsize * queue->tail);
00214 memcpy(dp, object, queue->objsize);
00215
00216
00217 queue->used++;
00218 queue->tail = (queue->tail + 1) % queue->max;
00219
00220 return true;
00221 }
00222
00233 bool qQueuePopFirst(Q_QUEUE *queue, void *object) {
00234 if(queue == NULL || object == NULL) return false;
00235
00236
00237 if(queue->used == 0) {
00238 DEBUG("Queue empty.");
00239 return false;
00240 }
00241
00242
00243 void *dp = queue->objarr + (queue->objsize * queue->head);
00244 memcpy(object, dp, queue->objsize);
00245
00246
00247 queue->used--;
00248 queue->head = (queue->head + 1) % queue->max;
00249
00250 return true;
00251 }
00252
00263 bool qQueuePopLast(Q_QUEUE *queue, void *object) {
00264 if(queue == NULL || object == NULL) return false;
00265
00266
00267 if(queue->used == 0) {
00268 DEBUG("Queue empty.");
00269 return false;
00270 }
00271
00272
00273 queue->tail = (queue->tail > 0) ? (queue->tail - 1) : (queue->max - 1);
00274 queue->used--;
00275
00276
00277 void *dp = queue->objarr + (queue->objsize * queue->tail);
00278 memcpy(object, dp, queue->objsize);
00279
00280 return true;
00281 }
00282
00292 bool qQueueStatus(Q_QUEUE *queue, int *used, int *max) {
00293 if(queue == NULL) return false;
00294
00295 if(used != NULL) *used = queue->used;
00296 if(max != NULL) *max = queue->max;
00297
00298 return true;
00299 }
00300
00301 #endif