.\" .\" Bindle Binaries Tools .\" Copyright (C) 2022 David M. Syzdek . .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are .\" met: .\" .\" * Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" * Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" * Neither the name of David M. Syzdek nor the .\" names of its contributors may be used to endorse or promote products .\" derived from this software without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS .\" IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, .\" THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID M. SYZDEK BE LIABLE FOR .\" ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER .\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .TH "BINDLE_ARRAY" "3" "@RELEASE_MONTH@" "@PACKAGE_NAME@" "Library Functions Manual" .SH NAME bindle_array_dequeue, bindle_array_enqueue, bindle_array_peek - Bindle Binaries Tools queue routines .SH LIBRARY Bindle Binaries Tools (libbindle, -lbindle) .SH SYNOPSIS .nf \fB#include\fR \fB\fR .sp .BI "ssize_t bindle_enqueue( void * " obj ", void ** " basep ", size_t * " nelp "," .BI " size_t " width ", void * (*" reallocbase ")(void *, size_t) );" .sp .BI "void * bindle_dequeue( void * " base ", size_t * " nelp ", size_t width );" .SH DESCRIPTION The \fBbindle_enqueue\fR() and \fBbindle_dequeue\fR() functions implement an array based queue. The elements in the array are ordered from newest to oldest with the most recently added element being the first element in the array and oldest element in the array being the last element. When adding an element to the queue, all existing elements are shifted by one position within the array. If \fIreallocbase\fR is NULL, \fBbindle_enqueue\fR() has a cost of O(n). \fBbindle_dequeue\fR() has a cost of O(1). .sp The size (in bytes) of each element of the array is specified by \fIwidth\fR. .sp \fBbindle_enqueue\fR() adds \fIobj\fR to a queue of *\fInelp\fR elements referenced by \fIbasep\fR. If \fIreallocbase\fR is not NULL, \fBbindle_enqueue\fR() will attempt to increase the size of the array by calling \fIreallocbase\fR. If \fIreallocbase\fR is NULL, it is up to the developer to not queue more elements than can be held by the array. If successful, the value referenced by \fInelp\fR will be incremented. .sp \fBbindle_dequeue\fR() removes and returns the next element in a queue of *\fInelp\fR elements pointed to by \fIbase\fR. If successful, the value referenced by \fInelp\fR will be decremented. .SH RETURN VALUES \fBbindle_dequeue\fR() return \fINULL\fR if the queue does not contain any elements, otherwise a pointer to the next object is returned. .sp Upon success, \fBbindle_enqueue\fR() returns 0. If an error ocurred, \fBbindle_enqueue\fR() returns -1. If \fIreallocbase\fR is NULL, \fBbindle_enqueue\fR() will always return 0. .SH EXAMPLE .nf #include #include #include #include struct wc { unsigned value; char * name; } wcs[] = { { 0xFFFFFF, "white" }, { 0xC0C0C0, "silver" }, { 0x808080, "gray" }, { 0x000000, "black" }, { 0xFF0000, "red" }, { 0x800000, "maroon" } }; #define nr_wcs (sizeof(wcs) / sizeof(wcs[0])) int main(void) { size_t pos, nel, width; ssize_t rc; struct wc **list, *obj, **res; nel = 0; list = NULL; width = sizeof(struct wc *); for(pos = 0; (pos < nr_wcs); pos++) { obj = &wcs[pos]; printf("enqueuing: #%06x %s\\n", obj->value, obj->name); rc = bindle_enqueue(&obj, (void **)&list, &nel, width, realloc); if (rc == -1) { fprintf(stderr, "error adding %s to queue\\n", wcs[pos].name); free(list); exit(EXIT_FAILURE); }; }; while((res = bindle_dequeue(list, &nel, width)) != NULL) { printf("dequeued: #%06x %s\\n", (*res)->value, (*res)->name); }; free(list); exit(EXIT_SUCCESS); } .fi .SH SEE ALSO .BR bindle_array (3), .BR bindle_peek (3) .SH AUTHOR David M. Syzdek .SH ACKNOWLEDGEMENTS \fB@PACKAGE_NAME@\fR is developed and maintained by David M. Syzdek . .\" end of man page