queue.c revision 1.2 1 /* $NetBSD: queue.c,v 1.2 2011/02/16 01:31:33 joerg Exp $ */
2 /* $FreeBSD: head/usr.bin/grep/queue.c 211496 2010-08-19 09:28:59Z des $ */
3 /*-
4 * Copyright (c) 1999 James Howard and Dag-Erling Codan Smrgrav
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * A really poor man's queue. It does only what it has to and gets out of
31 * Dodge. It is used in place of <sys/queue.h> to get a better performance.
32 */
33
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: queue.c,v 1.2 2011/02/16 01:31:33 joerg Exp $");
36
37 #include <sys/param.h>
38 #include <sys/queue.h>
39
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include "grep.h"
44
45 struct qentry {
46 STAILQ_ENTRY(qentry) list;
47 struct str data;
48 };
49
50 static STAILQ_HEAD(, qentry) queue = STAILQ_HEAD_INITIALIZER(queue);
51 static unsigned long long count;
52
53 static struct qentry *dequeue(void);
54
55 void
56 enqueue(struct str *x)
57 {
58 struct qentry *item;
59
60 item = grep_malloc(sizeof(struct qentry));
61 item->data.dat = grep_malloc(sizeof(char) * x->len);
62 item->data.len = x->len;
63 item->data.line_no = x->line_no;
64 item->data.off = x->off;
65 memcpy(item->data.dat, x->dat, x->len);
66 item->data.file = x->file;
67
68 STAILQ_INSERT_TAIL(&queue, item, list);
69
70 if (++count > Bflag)
71 free(dequeue());
72 }
73
74 static struct qentry *
75 dequeue(void)
76 {
77 struct qentry *item;
78
79 item = STAILQ_FIRST(&queue);
80 if (item == NULL)
81 return (NULL);
82
83 STAILQ_REMOVE_HEAD(&queue, list);
84 --count;
85 return (item);
86 }
87
88 void
89 printqueue(void)
90 {
91 struct qentry *item;
92
93 while ((item = dequeue()) != NULL) {
94 printline(&item->data, '-', (regmatch_t *)NULL, 0);
95 free(item);
96 }
97 }
98
99 void
100 clearqueue(void)
101 {
102 struct qentry *item;
103
104 while ((item = dequeue()) != NULL)
105 free(item);
106 }
107