aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/suricata/src/data-queue.c
blob: a3afd4acc92337dc113ed81ac88dd1be5eb679b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
 * Copyright (c) 2009, 2010 Open Information Security Foundation.
 *
 * \author Anoop Saldanha <anoopsaldanha@gmail.com>
 */

#include "suricata-common.h"
#include "data-queue.h"
#include "threads.h"

/**
 * \brief Enqueues data on the queue.
 *
 * \param q    Pointer to the data queue.
 * \param data Pointer to the data to be queued.  It should be a pointer to a
 *             structure instance that implements the template structure
 *             struct SCDQGenericQData_ defined in data-queue.h.
 */
void SCDQDataEnqueue(SCDQDataQueue *q, SCDQGenericQData *data)
{
    /* we already have some data in queue */
    if (q->top != NULL) {
        data->next = q->top;
        q->top->prev = data;
        q->top = data;

    /* the queue is empty */
    } else {
        q->top = data;
        q->bot = data;
    }

    q->len++;

#ifdef DBG_PERF
    if (q->len > q->dbg_maxlen)
        q->dbg_maxlen = q->len;
#endif /* DBG_PERF */

    return;
}

/**
 * \brief Dequeues and returns an entry from the queue.
 *
 * \param q      Pointer to the data queue.
 * \param retval Pointer to the data that has been enqueued.  The instance
 *               returned is/should be a pointer to a structure instance that
 *               implements the template structure struct SCDQGenericQData_
 *               defined in data-queue.h.
 */
SCDQGenericQData *SCDQDataDequeue(SCDQDataQueue *q)
{
    SCDQGenericQData *data = NULL;

    /* if the queue is empty there are is no data left and we return NULL */
    if (q->len == 0) {
        return NULL;
    }

    /* If we are going to get the last packet, set len to 0
     * before doing anything else (to make the threads to follow
     * the SCondWait as soon as possible) */
    q->len--;

    /* pull the bottom packet from the queue */
    data = q->bot;

#ifdef OS_DARWIN
    /* Weird issue in OS_DARWIN
     * Sometimes it looks that two thread arrive here at the same time
     * so the bot ptr is NULL */
    if (data == NULL) {
        printf("No data to dequeue!\n");
        return NULL;
    }
#endif /* OS_DARWIN */

    /* more data in queue */
    if (q->bot->prev != NULL) {
        q->bot = q->bot->prev;
        q->bot->next = NULL;
    /* just the one we remove, so now empty */
    } else {
        q->top = NULL;
        q->bot = NULL;
    }

    data->next = NULL;
    data->prev = NULL;

    return data;
}