aboutsummaryrefslogtreecommitdiff
path: root/util/queue.h
blob: 31236f6f30c3dedcf43b74f7268201b4320a4ae2 (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
#ifndef QUEUE_H
#define QUEUE_H

#define QUEUE_GENERATE(id, type, cap)                                  \
    static struct                                                      \
    { type buf[cap]; size_t start; size_t end; } _##id##_queue;        \
                                                                       \
    static int id##_enqueue(type *m)                                   \
    {                                                                  \
        if(_##id##_queue.end >= _##id##_queue.start + cap) {           \
            fprintf(stderr,                                            \
                    "ERROR: Queue capacity of %d reached\n", cap);     \
            return 1;                                                  \
        }                                                              \
                                                                       \
        _##id##_queue.buf[_##id##_queue.end++ % cap] = *m;             \
        return 0;                                                      \
    }                                                                  \
                                                                       \
    static int id##_dequeue(type *m)                                   \
    {                                                                  \
        if(_##id##_queue.start >= _##id##_queue.end) {                 \
            fprintf(stderr, "ERROR: Trying to dequeue empty queue\n"); \
            return 1;                                                  \
        }                                                              \
                                                                       \
        *m = _##id##_queue.buf[_##id##_queue.start++ % cap];           \
        return 0;                                                      \
    }                                                                  \
                                                                       \
    static int id##_empty()                                            \
    { return _##id##_queue.start == _##id##_queue.end; }               \
                                                                       \
    static int id##_peek(type *m)                                      \
    {                                                                  \
        if(id##_empty()) {                                             \
            fprintf(stderr,                                            \
                    "ERROR: Trying to peek into empty queue\n");       \
            return 1;                                                  \
        }                                                              \
                                                                       \
        *m = _##id##_queue.buf[_##id##_queue.start % cap];             \
        return 0;                                                      \
    }

#endif