-
Notifications
You must be signed in to change notification settings - Fork 26
/
netq.c
172 lines (144 loc) · 2.98 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*******************************************************************************
*
* Copyright (c) 2011, 2012, 2013, 2014, 2015 Olaf Bergmann (TZI) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Olaf Bergmann - initial API and implementation
* Hauke Mehrtens - memory optimization, ECC integration
*
*******************************************************************************/
#include "tinydtls.h"
#include "netq.h"
#ifdef HAVE_ASSERT_H
#include <assert.h>
#else
#ifndef assert
#warning "assertions are disabled"
# define assert(x)
#endif
#endif
#include "lib/memb.h"
/* Log configuration */
#define LOG_MODULE "dtls-netq"
#define LOG_LEVEL LOG_LEVEL_DTLS
#include "dtls-log.h"
MEMB(netq_storage, netq_t, NETQ_MAXCNT);
static inline netq_t *
netq_malloc_node(size_t size) {
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);
}
int
netq_insert_node(netq_t **queue, netq_t *node) {
netq_t *p, *op;
assert(queue);
assert(node);
p = *queue;
op = NULL;
while(p && p->t <= node->t) {
op = p;
p = p->next;
}
node->next = p;
if(op) {
op->next = node;
} else {
*queue = node;
}
return 1;
}
netq_t *
netq_head(netq_t **queue) {
return queue ? *queue : NULL;
}
netq_t *
netq_next(netq_t *p) {
if (!p)
return NULL;
return p->next;
}
void
netq_remove(netq_t **queue, netq_t *item) {
{
struct netq_t *curr, *prev;
if (!queue || !item) {
return;
}
if(*queue == NULL) {
return;
}
prev = NULL;
for(curr = *queue; curr != NULL; curr = curr->next) {
if(curr == item) {
if(prev == NULL) {
/* First on list */
*queue = curr->next;
} else {
/* Not first on list */
prev->next = curr->next;
}
curr->next = NULL;
return;
}
prev = curr;
}
}
}
netq_t
*netq_pop_first(netq_t **queue) {
netq_t *p = netq_head(queue);
if(p) {
/* remove the element from the list and update head */
*queue = p->next;
p->next = NULL;
}
return p;
}
netq_t *
netq_node_new(size_t size) {
netq_t *node;
node = netq_malloc_node(size);
if (!node) {
LOG_WARN("netq_node_new: malloc\n");
}
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, *tmp;
if (queue) {
p = *queue;
while(p) {
tmp = p->next;
p->next = NULL;
netq_free_node(p);
p = tmp;
}
*queue = NULL;
}
}