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