forked from cetic/tinydtls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
netq.c
123 lines (95 loc) · 1.93 KB
/
netq.c
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* netq.h -- Simple packet queue
*
* Copyright (C) 2010--2012 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the library tinyDTLS. Please see the file
* LICENSE for terms of use.
*/
#include "debug.h"
#include "netq.h"
#ifdef HAVE_ASSERT_H
#include <assert.h>
#else
#ifndef assert
#warning "assertions are disabled"
# define assert(x)
#endif
#endif
#include "t_list.h"
#ifndef WITH_CONTIKI
#include <stdlib.h>
static inline netq_t *
netq_malloc_node() {
return (netq_t *)malloc(sizeof(netq_t));
}
static inline void
netq_free_node(netq_t *node) {
free(node);
}
/* FIXME: implement Contiki's list functions using utlist.h */
#else /* WITH_CONTIKI */
#include "memb.h"
MEMB(netq_storage, netq_t, NETQ_MAXCNT);
static inline netq_t *
netq_malloc_node() {
return (netq_t *)memb_alloc(&netq_storage);
}
static inline void
netq_free_node(netq_t *node) {
memb_free(&netq_storage, node);
}
void
netq_init() {
memb_init(&netq_storage);
}
#endif /* WITH_CONTIKI */
int
netq_insert_node(netq_t **queue, netq_t *node) {
netq_t *p;
assert(queue);
assert(node);
p = (netq_t *)list_head((list_t)queue);
while(p && p->t <= node->t)
p = list_item_next(p);
if (p)
list_insert((list_t)queue, p, node);
else
list_push((list_t)queue, node);
return 1;
}
netq_t *
netq_head(netq_t **queue) {
if (!queue)
return NULL;
return list_head((list_t)queue);
}
netq_t *netq_pop_first(netq_t **queue) {
if (!queue)
return NULL;
return list_pop((list_t)queue);
}
netq_t *
netq_node_new() {
netq_t *node;
node = netq_malloc_node();
#ifndef NDEBUG
if (!node)
dsrv_log(LOG_WARN, "netq_node_new: malloc\n");
#endif
if (node)
memset(node, 0, sizeof(netq_t));
return node;
}
void
netq_node_free(netq_t *node) {
if (node)
netq_free_node(node);
}
void
netq_delete_all(netq_t *queue) {
netq_t *p;
if (queue) {
while((p = list_pop((list_t)&queue)))
netq_free_node(p);
}
}