1268966Spfg/*	$NetBSD: queue.c,v 1.5 2011/08/31 16:24:57 plunky Exp $	*/
2220422Sgabor/*	$FreeBSD$	*/
3220422Sgabor
4210389Sgabor/*-
5211496Sdes * Copyright (c) 1999 James Howard and Dag-Erling Co��dan Sm��rgrav
6210389Sgabor * All rights reserved.
7210389Sgabor *
8210389Sgabor * Redistribution and use in source and binary forms, with or without
9210389Sgabor * modification, are permitted provided that the following conditions
10210389Sgabor * are met:
11210389Sgabor * 1. Redistributions of source code must retain the above copyright
12210389Sgabor *    notice, this list of conditions and the following disclaimer.
13210389Sgabor * 2. Redistributions in binary form must reproduce the above copyright
14210389Sgabor *    notice, this list of conditions and the following disclaimer in the
15210389Sgabor *    documentation and/or other materials provided with the distribution.
16210389Sgabor *
17210389Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18210389Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19210389Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20210389Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21210389Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22210389Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23210389Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24210389Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25210389Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26210389Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27210389Sgabor * SUCH DAMAGE.
28210389Sgabor */
29210389Sgabor
30210389Sgabor/*
31210389Sgabor * A really poor man's queue.  It does only what it has to and gets out of
32210389Sgabor * Dodge.  It is used in place of <sys/queue.h> to get a better performance.
33210389Sgabor */
34210389Sgabor
35210389Sgabor#include <sys/cdefs.h>
36210389Sgabor__FBSDID("$FreeBSD$");
37210389Sgabor
38210389Sgabor#include <sys/param.h>
39210389Sgabor#include <sys/queue.h>
40210389Sgabor
41210389Sgabor#include <stdlib.h>
42210389Sgabor#include <string.h>
43210389Sgabor
44210389Sgabor#include "grep.h"
45210389Sgabor
46210389Sgaborstruct qentry {
47210389Sgabor	STAILQ_ENTRY(qentry)	list;
48210389Sgabor	struct str	 	data;
49210389Sgabor};
50210389Sgabor
51210389Sgaborstatic STAILQ_HEAD(, qentry)	queue = STAILQ_HEAD_INITIALIZER(queue);
52210389Sgaborstatic unsigned long long	count;
53210389Sgabor
54210389Sgaborstatic struct qentry	*dequeue(void);
55210389Sgabor
56210389Sgaborvoid
57210389Sgaborenqueue(struct str *x)
58210389Sgabor{
59210389Sgabor	struct qentry *item;
60210389Sgabor
61210389Sgabor	item = grep_malloc(sizeof(struct qentry));
62210389Sgabor	item->data.dat = grep_malloc(sizeof(char) * x->len);
63210389Sgabor	item->data.len = x->len;
64210389Sgabor	item->data.line_no = x->line_no;
65210389Sgabor	item->data.off = x->off;
66211364Sgabor	memcpy(item->data.dat, x->dat, x->len);
67210389Sgabor	item->data.file = x->file;
68210389Sgabor
69210389Sgabor	STAILQ_INSERT_TAIL(&queue, item, list);
70210389Sgabor
71268966Spfg	if (++count > Bflag) {
72268966Spfg		item = dequeue();
73268966Spfg		free(item->data.dat);
74268966Spfg		free(item);
75268966Spfg	}
76210389Sgabor}
77210389Sgabor
78210389Sgaborstatic struct qentry *
79210389Sgabordequeue(void)
80210389Sgabor{
81210389Sgabor	struct qentry *item;
82210389Sgabor
83210389Sgabor	item = STAILQ_FIRST(&queue);
84210389Sgabor	if (item == NULL)
85210389Sgabor		return (NULL);
86210389Sgabor
87210389Sgabor	STAILQ_REMOVE_HEAD(&queue, list);
88210389Sgabor	--count;
89210389Sgabor	return (item);
90210389Sgabor}
91210389Sgabor
92210389Sgaborvoid
93210389Sgaborprintqueue(void)
94210389Sgabor{
95210389Sgabor	struct qentry *item;
96210389Sgabor
97210389Sgabor	while ((item = dequeue()) != NULL) {
98268966Spfg		printline(&item->data, '-', NULL, 0);
99268966Spfg		free(item->data.dat);
100210389Sgabor		free(item);
101210389Sgabor	}
102210389Sgabor}
103210389Sgabor
104210389Sgaborvoid
105210389Sgaborclearqueue(void)
106210389Sgabor{
107210389Sgabor	struct qentry *item;
108210389Sgabor
109268966Spfg	while ((item = dequeue()) != NULL) {
110268966Spfg		free(item->data.dat);
111210389Sgabor		free(item);
112268966Spfg	}
113210389Sgabor}
114