1*f3f50de6SPedro F. Giffuni /* $NetBSD: queue.c,v 1.5 2011/08/31 16:24:57 plunky Exp $ */ 2b66a823bSGabor Kovesdan /* $FreeBSD$ */ 3b66a823bSGabor Kovesdan 44dc88ebeSGabor Kovesdan /*- 5a0ef9ad6SDag-Erling Smørgrav * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav 64dc88ebeSGabor Kovesdan * All rights reserved. 74dc88ebeSGabor Kovesdan * 84dc88ebeSGabor Kovesdan * Redistribution and use in source and binary forms, with or without 94dc88ebeSGabor Kovesdan * modification, are permitted provided that the following conditions 104dc88ebeSGabor Kovesdan * are met: 114dc88ebeSGabor Kovesdan * 1. Redistributions of source code must retain the above copyright 124dc88ebeSGabor Kovesdan * notice, this list of conditions and the following disclaimer. 134dc88ebeSGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright 144dc88ebeSGabor Kovesdan * notice, this list of conditions and the following disclaimer in the 154dc88ebeSGabor Kovesdan * documentation and/or other materials provided with the distribution. 164dc88ebeSGabor Kovesdan * 174dc88ebeSGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 184dc88ebeSGabor Kovesdan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 194dc88ebeSGabor Kovesdan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 204dc88ebeSGabor Kovesdan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 214dc88ebeSGabor Kovesdan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 224dc88ebeSGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 234dc88ebeSGabor Kovesdan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 244dc88ebeSGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 254dc88ebeSGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 264dc88ebeSGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 274dc88ebeSGabor Kovesdan * SUCH DAMAGE. 284dc88ebeSGabor Kovesdan */ 294dc88ebeSGabor Kovesdan 304dc88ebeSGabor Kovesdan /* 314dc88ebeSGabor Kovesdan * A really poor man's queue. It does only what it has to and gets out of 324dc88ebeSGabor Kovesdan * Dodge. It is used in place of <sys/queue.h> to get a better performance. 334dc88ebeSGabor Kovesdan */ 344dc88ebeSGabor Kovesdan 354dc88ebeSGabor Kovesdan #include <sys/cdefs.h> 364dc88ebeSGabor Kovesdan __FBSDID("$FreeBSD$"); 374dc88ebeSGabor Kovesdan 384dc88ebeSGabor Kovesdan #include <sys/param.h> 394dc88ebeSGabor Kovesdan #include <sys/queue.h> 404dc88ebeSGabor Kovesdan 414dc88ebeSGabor Kovesdan #include <stdlib.h> 424dc88ebeSGabor Kovesdan #include <string.h> 434dc88ebeSGabor Kovesdan 444dc88ebeSGabor Kovesdan #include "grep.h" 454dc88ebeSGabor Kovesdan 464dc88ebeSGabor Kovesdan struct qentry { 474dc88ebeSGabor Kovesdan STAILQ_ENTRY(qentry) list; 484dc88ebeSGabor Kovesdan struct str data; 494dc88ebeSGabor Kovesdan }; 504dc88ebeSGabor Kovesdan 514dc88ebeSGabor Kovesdan static STAILQ_HEAD(, qentry) queue = STAILQ_HEAD_INITIALIZER(queue); 524dc88ebeSGabor Kovesdan static unsigned long long count; 534dc88ebeSGabor Kovesdan 544dc88ebeSGabor Kovesdan static struct qentry *dequeue(void); 554dc88ebeSGabor Kovesdan 564dc88ebeSGabor Kovesdan void 574dc88ebeSGabor Kovesdan enqueue(struct str *x) 584dc88ebeSGabor Kovesdan { 594dc88ebeSGabor Kovesdan struct qentry *item; 604dc88ebeSGabor Kovesdan 614dc88ebeSGabor Kovesdan item = grep_malloc(sizeof(struct qentry)); 624dc88ebeSGabor Kovesdan item->data.dat = grep_malloc(sizeof(char) * x->len); 634dc88ebeSGabor Kovesdan item->data.len = x->len; 644dc88ebeSGabor Kovesdan item->data.line_no = x->line_no; 654dc88ebeSGabor Kovesdan item->data.off = x->off; 6659218eb7SGabor Kovesdan memcpy(item->data.dat, x->dat, x->len); 674dc88ebeSGabor Kovesdan item->data.file = x->file; 684dc88ebeSGabor Kovesdan 694dc88ebeSGabor Kovesdan STAILQ_INSERT_TAIL(&queue, item, list); 704dc88ebeSGabor Kovesdan 71*f3f50de6SPedro F. Giffuni if (++count > Bflag) { 72*f3f50de6SPedro F. Giffuni item = dequeue(); 73*f3f50de6SPedro F. Giffuni free(item->data.dat); 74*f3f50de6SPedro F. Giffuni free(item); 75*f3f50de6SPedro F. Giffuni } 764dc88ebeSGabor Kovesdan } 774dc88ebeSGabor Kovesdan 784dc88ebeSGabor Kovesdan static struct qentry * 794dc88ebeSGabor Kovesdan dequeue(void) 804dc88ebeSGabor Kovesdan { 814dc88ebeSGabor Kovesdan struct qentry *item; 824dc88ebeSGabor Kovesdan 834dc88ebeSGabor Kovesdan item = STAILQ_FIRST(&queue); 844dc88ebeSGabor Kovesdan if (item == NULL) 854dc88ebeSGabor Kovesdan return (NULL); 864dc88ebeSGabor Kovesdan 874dc88ebeSGabor Kovesdan STAILQ_REMOVE_HEAD(&queue, list); 884dc88ebeSGabor Kovesdan --count; 894dc88ebeSGabor Kovesdan return (item); 904dc88ebeSGabor Kovesdan } 914dc88ebeSGabor Kovesdan 924dc88ebeSGabor Kovesdan void 934dc88ebeSGabor Kovesdan printqueue(void) 944dc88ebeSGabor Kovesdan { 954dc88ebeSGabor Kovesdan struct qentry *item; 964dc88ebeSGabor Kovesdan 974dc88ebeSGabor Kovesdan while ((item = dequeue()) != NULL) { 98*f3f50de6SPedro F. Giffuni printline(&item->data, '-', NULL, 0); 994dc88ebeSGabor Kovesdan free(item); 1004dc88ebeSGabor Kovesdan } 1014dc88ebeSGabor Kovesdan } 1024dc88ebeSGabor Kovesdan 1034dc88ebeSGabor Kovesdan void 1044dc88ebeSGabor Kovesdan clearqueue(void) 1054dc88ebeSGabor Kovesdan { 1064dc88ebeSGabor Kovesdan struct qentry *item; 1074dc88ebeSGabor Kovesdan 108*f3f50de6SPedro F. Giffuni while ((item = dequeue()) != NULL) { 109*f3f50de6SPedro F. Giffuni free(item->data.dat); 1104dc88ebeSGabor Kovesdan free(item); 1114dc88ebeSGabor Kovesdan } 112*f3f50de6SPedro F. Giffuni } 113