queue.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* $NetBSD: queue.c,v 1.5 2011/08/31 16:24:57 plunky 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 Coïdan Smørgrav
  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. * A really poor man's queue. It does only what it has to and gets out of
  30. * Dodge. It is used in place of <sys/queue.h> to get a better performance.
  31. */
  32. #if HAVE_NBTOOL_CONFIG_H
  33. #include "nbtool_config.h"
  34. #endif
  35. #include <sys/cdefs.h>
  36. __RCSID("$NetBSD: queue.c,v 1.5 2011/08/31 16:24:57 plunky Exp $");
  37. #include <sys/param.h>
  38. #include <sys/queue.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include "grep.h"
  42. struct qentry {
  43. STAILQ_ENTRY(qentry) list;
  44. struct str data;
  45. };
  46. static STAILQ_HEAD(, qentry) queue = STAILQ_HEAD_INITIALIZER(queue);
  47. static unsigned long long count;
  48. static struct qentry *dequeue(void);
  49. void
  50. enqueue(struct str *x)
  51. {
  52. struct qentry *item;
  53. item = grep_malloc(sizeof(struct qentry));
  54. item->data.dat = grep_malloc(sizeof(char) * x->len);
  55. item->data.len = x->len;
  56. item->data.line_no = x->line_no;
  57. item->data.off = x->off;
  58. memcpy(item->data.dat, x->dat, x->len);
  59. item->data.file = x->file;
  60. STAILQ_INSERT_TAIL(&queue, item, list);
  61. if (++count > Bflag) {
  62. item = dequeue();
  63. free(item->data.dat);
  64. free(item);
  65. }
  66. }
  67. static struct qentry *
  68. dequeue(void)
  69. {
  70. struct qentry *item;
  71. item = STAILQ_FIRST(&queue);
  72. if (item == NULL)
  73. return (NULL);
  74. STAILQ_REMOVE_HEAD(&queue, list);
  75. --count;
  76. return (item);
  77. }
  78. void
  79. printqueue(void)
  80. {
  81. struct qentry *item;
  82. while ((item = dequeue()) != NULL) {
  83. printline(&item->data, '-', NULL, 0);
  84. free(item->data.dat);
  85. free(item);
  86. }
  87. }
  88. void
  89. clearqueue(void)
  90. {
  91. struct qentry *item;
  92. while ((item = dequeue()) != NULL) {
  93. free(item->data.dat);
  94. free(item);
  95. }
  96. }