suricata
util-mpm-ac-queue.h
Go to the documentation of this file.
1 /* Copyright (C) 2025 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
22  *
23  */
24 
25 #ifndef SURICATA_UTIL_MPM_AC_QUEUE_H
26 #define SURICATA_UTIL_MPM_AC_QUEUE_H
27 
28 #define STATE_QUEUE_CONTAINER_SIZE 65536
29 
30 /**
31  * \brief Helper structure used by AC during state table creation
32  */
33 typedef struct StateQueue_ {
34  uint32_t top;
35  uint32_t bot;
36  uint32_t size;
37  int32_t *store;
39 
42 
43 static inline int SCACStateQueueIsEmpty(StateQueue *q)
44 {
45  if (q->top == q->bot)
46  return 1;
47  else
48  return 0;
49 }
50 
51 static inline void SCACEnqueue(StateQueue *q, int32_t state)
52 {
53  /*if we already have this */
54  for (uint32_t i = q->bot; i < q->top; i++) {
55  if (q->store[i] == state)
56  return;
57  }
58 
59  q->store[q->top++] = state;
60 
61  if (q->top == q->size)
62  q->top = 0;
63 
64  if (q->top == q->bot) {
65  // allocate a new store and copy + realign
66  int32_t *tmp = SCCalloc(q->size + STATE_QUEUE_CONTAINER_SIZE, sizeof(int32_t));
67  if (tmp == NULL) {
68  FatalError("Error reallocating memory");
69  }
70  memcpy(tmp, q->store + q->bot, (q->size - q->bot) * sizeof(int32_t));
71  memcpy(tmp + (q->size - q->bot), q->store, q->top * sizeof(int32_t));
72  SCFree(q->store);
73  q->store = tmp;
74  q->bot = 0;
75  q->top = q->size;
77  }
78 }
79 
80 static inline int32_t SCACDequeue(StateQueue *q)
81 {
82  if (q->bot == q->size)
83  q->bot = 0;
84 
85  if (q->bot == q->top) {
86  FatalError("StateQueue behaving weirdly. "
87  "Fatal Error. Exiting. Please file a bug report on this");
88  }
89 
90  return q->store[q->bot++];
91 }
92 
93 #endif /* SURICATA_UTIL_MPM_AC_QUEUE_H */
SCACStateQueueFree
void SCACStateQueueFree(StateQueue *q)
Definition: util-mpm-ac-queue.c:36
StateQueue_::top
uint32_t top
Definition: util-mpm-ac-queue.h:34
StateQueue_
Helper structure used by AC during state table creation.
Definition: util-mpm-ac-queue.h:33
SCACStateQueueAlloc
StateQueue * SCACStateQueueAlloc(void)
Definition: util-mpm-ac-queue.c:22
StateQueue
struct StateQueue_ StateQueue
Helper structure used by AC during state table creation.
StateQueue_::store
int32_t * store
Definition: util-mpm-ac-queue.h:37
FatalError
#define FatalError(...)
Definition: util-debug.h:510
StateQueue_::size
uint32_t size
Definition: util-mpm-ac-queue.h:36
SCFree
#define SCFree(p)
Definition: util-mem.h:61
StateQueue_::bot
uint32_t bot
Definition: util-mpm-ac-queue.h:35
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
STATE_QUEUE_CONTAINER_SIZE
#define STATE_QUEUE_CONTAINER_SIZE
Definition: util-mpm-ac-queue.h:28