queue.c revision 1.3 1 1.3 joerg /* $NetBSD: queue.c,v 1.3 2011/04/18 03:27:40 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.3 joerg #if HAVE_NBTOOL_CONFIG_H
35 1.3 joerg #include "nbtool_config.h"
36 1.3 joerg #endif
37 1.3 joerg
38 1.2 joerg #include <sys/cdefs.h>
39 1.3 joerg __RCSID("$NetBSD: queue.c,v 1.3 2011/04/18 03:27:40 joerg Exp $");
40 1.2 joerg
41 1.1 cjep #include <sys/param.h>
42 1.2 joerg #include <sys/queue.h>
43 1.1 cjep
44 1.1 cjep #include <stdlib.h>
45 1.1 cjep #include <string.h>
46 1.1 cjep
47 1.1 cjep #include "grep.h"
48 1.1 cjep
49 1.2 joerg struct qentry {
50 1.2 joerg STAILQ_ENTRY(qentry) list;
51 1.2 joerg struct str data;
52 1.2 joerg };
53 1.1 cjep
54 1.2 joerg static STAILQ_HEAD(, qentry) queue = STAILQ_HEAD_INITIALIZER(queue);
55 1.2 joerg static unsigned long long count;
56 1.1 cjep
57 1.2 joerg static struct qentry *dequeue(void);
58 1.1 cjep
59 1.1 cjep void
60 1.2 joerg enqueue(struct str *x)
61 1.1 cjep {
62 1.2 joerg struct qentry *item;
63 1.1 cjep
64 1.2 joerg item = grep_malloc(sizeof(struct qentry));
65 1.2 joerg item->data.dat = grep_malloc(sizeof(char) * x->len);
66 1.1 cjep item->data.len = x->len;
67 1.1 cjep item->data.line_no = x->line_no;
68 1.1 cjep item->data.off = x->off;
69 1.1 cjep memcpy(item->data.dat, x->dat, x->len);
70 1.1 cjep item->data.file = x->file;
71 1.1 cjep
72 1.2 joerg STAILQ_INSERT_TAIL(&queue, item, list);
73 1.1 cjep
74 1.1 cjep if (++count > Bflag)
75 1.2 joerg free(dequeue());
76 1.1 cjep }
77 1.1 cjep
78 1.2 joerg static struct qentry *
79 1.1 cjep dequeue(void)
80 1.1 cjep {
81 1.2 joerg struct qentry *item;
82 1.1 cjep
83 1.2 joerg item = STAILQ_FIRST(&queue);
84 1.2 joerg if (item == NULL)
85 1.2 joerg return (NULL);
86 1.1 cjep
87 1.2 joerg STAILQ_REMOVE_HEAD(&queue, list);
88 1.1 cjep --count;
89 1.2 joerg return (item);
90 1.1 cjep }
91 1.1 cjep
92 1.1 cjep void
93 1.1 cjep printqueue(void)
94 1.1 cjep {
95 1.2 joerg struct qentry *item;
96 1.1 cjep
97 1.1 cjep while ((item = dequeue()) != NULL) {
98 1.2 joerg printline(&item->data, '-', (regmatch_t *)NULL, 0);
99 1.2 joerg free(item);
100 1.1 cjep }
101 1.1 cjep }
102 1.1 cjep
103 1.1 cjep void
104 1.1 cjep clearqueue(void)
105 1.1 cjep {
106 1.2 joerg struct qentry *item;
107 1.1 cjep
108 1.1 cjep while ((item = dequeue()) != NULL)
109 1.2 joerg free(item);
110 1.1 cjep }
111