xref: /freebsd/usr.bin/grep/queue.c (revision b66a823be88d3f3dab30b6beb083a3ef994cbe15)
1*b66a823bSGabor Kovesdan /*	$NetBSD: queue.c,v 1.2 2011/02/16 01:31:33 joerg Exp $	*/
2*b66a823bSGabor Kovesdan /*	$FreeBSD$	*/
3*b66a823bSGabor 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 
714dc88ebeSGabor Kovesdan 	if (++count > Bflag)
724dc88ebeSGabor Kovesdan 		free(dequeue());
734dc88ebeSGabor Kovesdan }
744dc88ebeSGabor Kovesdan 
754dc88ebeSGabor Kovesdan static struct qentry *
764dc88ebeSGabor Kovesdan dequeue(void)
774dc88ebeSGabor Kovesdan {
784dc88ebeSGabor Kovesdan 	struct qentry *item;
794dc88ebeSGabor Kovesdan 
804dc88ebeSGabor Kovesdan 	item = STAILQ_FIRST(&queue);
814dc88ebeSGabor Kovesdan 	if (item == NULL)
824dc88ebeSGabor Kovesdan 		return (NULL);
834dc88ebeSGabor Kovesdan 
844dc88ebeSGabor Kovesdan 	STAILQ_REMOVE_HEAD(&queue, list);
854dc88ebeSGabor Kovesdan 	--count;
864dc88ebeSGabor Kovesdan 	return (item);
874dc88ebeSGabor Kovesdan }
884dc88ebeSGabor Kovesdan 
894dc88ebeSGabor Kovesdan void
904dc88ebeSGabor Kovesdan printqueue(void)
914dc88ebeSGabor Kovesdan {
924dc88ebeSGabor Kovesdan 	struct qentry *item;
934dc88ebeSGabor Kovesdan 
944dc88ebeSGabor Kovesdan 	while ((item = dequeue()) != NULL) {
954dc88ebeSGabor Kovesdan 		printline(&item->data, '-', (regmatch_t *)NULL, 0);
964dc88ebeSGabor Kovesdan 		free(item);
974dc88ebeSGabor Kovesdan 	}
984dc88ebeSGabor Kovesdan }
994dc88ebeSGabor Kovesdan 
1004dc88ebeSGabor Kovesdan void
1014dc88ebeSGabor Kovesdan clearqueue(void)
1024dc88ebeSGabor Kovesdan {
1034dc88ebeSGabor Kovesdan 	struct qentry *item;
1044dc88ebeSGabor Kovesdan 
1054dc88ebeSGabor Kovesdan 	while ((item = dequeue()) != NULL)
1064dc88ebeSGabor Kovesdan 		free(item);
1074dc88ebeSGabor Kovesdan }
108