#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "qDecoder.h"
#include "qInternal.h"
Go to the source code of this file.
FunctionsTitleTestFour | |
bool | qQueueClear (Q_QUEUE *queue) |
int | qQueueInit (Q_QUEUE *queue, void *datamem, size_t datamemsize, size_t objsize) |
bool | qQueuePopFirst (Q_QUEUE *queue, void *object) |
bool | qQueuePopLast (Q_QUEUE *queue, void *object) |
bool | qQueuePush (Q_QUEUE *queue, const void *object) |
size_t | qQueueSize (int max, size_t objsize) |
bool | qQueueStatus (Q_QUEUE *queue, int *used, int *max) |
This implementation is designed to use statically allocated(fixed-size) memory for flexibilities. So you can use this queue with in shared-memory architecture to communicate with other processors.
----[Sample codes]---- struct myobj { int integer; char string[255+1]; }; int main(void) { // allocate objects data memory size_t memsize = qQueueSize(10, sizeof(struct myobj)); void *datamem = malloc(memsize); // for static data memory use can use this way // char datamem[NNN]; // initialize queue Q_QUEUE queue; if(qQueueInit(&queue, datamem, memsize, sizeof(struct myobj)) == 0) { printf("Can't initialize queue.\n"); return -1; } // push object int i; for(i = 1; ; i++) { // set sample object struct myobj obj; obj.integer = i; sprintf(obj.string, "hello world %d", i); // push object if(qQueuePush(&queue, &obj) == false) break; // print debug info printf("Push : %d, %s\n", obj.integer, obj.string); } // pop object from head & tail while(true) { struct myobj pop; if(qQueuePopFirst(&queue, &pop) == false) break; printf("PopFirst : %d, %s\n", pop.integer, pop.string); if(qQueuePopLast(&queue, &pop) == false) break; printf("PopLast : %d, %s\n", pop.integer, pop.string); } return 0; } ----[Results]---- Push : 1, hello world 1 Push : 2, hello world 2 Push : 3, hello world 3 Push : 4, hello world 4 Push : 5, hello world 5 Push : 6, hello world 6 Push : 7, hello world 7 Push : 8, hello world 8 Push : 9, hello world 9 Push : 10, hello world 10 PopFirst : 1, hello world 1 PopLast : 10, hello world 10 PopFirst : 2, hello world 2 PopLast : 9, hello world 9 PopFirst : 3, hello world 3 PopLast : 8, hello world 8 PopFirst : 4, hello world 4 PopLast : 7, hello world 7 PopFirst : 5, hello world 5 PopLast : 6, hello world 6
Definition in file qQueue.c.