summaryrefslogtreecommitdiffstats
path: root/src/output/dnssim/ll.h
blob: 8e0b07af184ba1c8dcc021a95b7b4c7c89d668ff (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
/*
 * Copyright (c) 2019-2020, CZ.NIC, z.s.p.o.
 * All rights reserved.
 *
 * This file is part of dnsjit.
 *
 * dnsjit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * dnsjit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with dnsjit.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __dnsjit_output_dnssim_ll_h
#define __dnsjit_output_dnssim_ll_h

#include "core/assert.h"

/* Utility macros for linked list structures.
 *
 * - "list" is the pointer to the first node of the linked list
 * - "list" can be NULL if there are no nodes
 * - every node has "next", which points to the next node (can be NULL)
 */

/* Append a node to the list.
 *
 * Only a single node can be appended - node->next must be NULL.
 */
#define _ll_append(list, node)                                                    \
    {                                                                             \
        glassert((node)->next == NULL, "node->next must be null when appending"); \
        if ((list) == NULL)                                                       \
            (list) = (node);                                                      \
        else if ((node) != NULL) {                                                \
            typeof(list) _current = (list);                                       \
            while (_current->next != NULL)                                        \
                _current = _current->next;                                        \
            _current->next = node;                                                \
        }                                                                         \
    }

/* Remove a node from the list.
 *
 * In strict mode, the node must be present in the list.
 */
#define _ll_remove_template(list, node, strict)                                                    \
    {                                                                                              \
        if (strict)                                                                                \
            glassert((list), "list can't be null when removing nodes");                            \
        if ((list) != NULL && (node) != NULL) {                                                    \
            if ((list) == (node)) {                                                                \
                (list)       = (node)->next;                                                       \
                (node)->next = NULL;                                                               \
            } else {                                                                               \
                typeof(list) _current = (list);                                                    \
                while (_current != NULL && _current->next != (node)) {                             \
                    if (strict)                                                                    \
                        glassert((_current->next), "list doesn't contain the node to be removed"); \
                    _current = _current->next;                                                     \
                }                                                                                  \
                if (_current != NULL) {                                                            \
                    _current->next = (node)->next;                                                 \
                    (node)->next   = NULL;                                                         \
                }                                                                                  \
            }                                                                                      \
        }                                                                                          \
    }

/* Remove a node from the list. */
#define _ll_remove(list, node) _ll_remove_template((list), (node), true)

/* Remove a node from the list if it's present. */
#define _ll_try_remove(list, node) _ll_remove_template((list), (node), false)

#endif