summaryrefslogtreecommitdiffstats
path: root/nsock/src/gh_list.h
blob: 32f383edf4cfe78e07f1eae44ff17813a9cd7595 (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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/***************************************************************************
 * gh_list.h -- a simple doubly-linked list implementation.                *
 *                                                                         *
 ***********************IMPORTANT NSOCK LICENSE TERMS***********************
 *
 * The nsock parallel socket event library is (C) 1999-2023 Nmap Software LLC
 * This library is free software; you may redistribute and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; Version 2. This guarantees your right to use, modify, and
 * redistribute this software under certain conditions. If this license is
 * unacceptable to you, Nmap Software LLC may be willing to sell alternative
 * licenses (contact sales@nmap.com ).
 *
 * As a special exception to the GPL terms, Nmap Software LLC grants permission
 * to link the code of this program with any version of the OpenSSL library
 * which is distributed under a license identical to that listed in the included
 * docs/licenses/OpenSSL.txt file, and distribute linked combinations including
 * the two. You must obey the GNU GPL in all respects for all of the code used
 * other than OpenSSL. If you modify this file, you may extend this exception to
 * your version of the file, but you are not obligated to do so.
 *
 * If you received these files with a written license agreement stating terms
 * other than the (GPL) terms above, then that alternative license agreement
 * takes precedence over this comment.
 *
 * Source is provided to this software because we believe users have a right to
 * know exactly what a program is going to do before they run it. This also
 * allows you to audit the software for security holes.
 *
 * Source code also allows you to port Nmap to new platforms, fix bugs, and add
 * new features. You are highly encouraged to send your changes to the
 * dev@nmap.org mailing list for possible incorporation into the main
 * distribution. By sending these changes to Fyodor or one of the Insecure.Org
 * development mailing lists, or checking them into the Nmap source code
 * repository, it is understood (unless you specify otherwise) that you are
 * offering the Nmap Project (Nmap Software LLC) the unlimited, non-exclusive
 * right to reuse, modify, and relicense the code. Nmap will always be available
 * Open Source, but this is important because the inability to relicense code
 * has caused devastating problems for other Free Software projects (such as KDE
 * and NASM). We also occasionally relicense the code to third parties as
 * discussed above. If you wish to specify special license conditions of your
 * contributions, just say so when you send them.
 *
 * This program 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 v2.0 for more
 * details (http://www.gnu.org/licenses/gpl-2.0.html).
 *
 ***************************************************************************/

/* $Id$ */

#ifndef GH_LIST_H
#define GH_LIST_H

#ifdef HAVE_CONFIG_H
#include "nsock_config.h"
#include "nbase_config.h"
#endif

#ifdef WIN32
#include "nbase_winconfig.h"
#endif

#include "error.h"
#include <assert.h>

#define GH_LIST_MAGIC       0xBADFACE
#ifndef GH_LIST_PARANOID
#define GH_LIST_PARANOID    0
#endif


typedef struct gh_list_node {
  struct gh_list_node *next;
  struct gh_list_node *prev;
} gh_lnode_t;

typedef struct gh_list {
  /* Number of elements in the list */
  unsigned int count;
  gh_lnode_t *first;
  gh_lnode_t *last;
} gh_list_t;


/* That one's an efficiency killer but it should reveal
 * any inconsistency in nsock's lists management. To be
 * called on every list we get and return. */
static inline void paranoid_list_check(gh_list_t *list) {
#if GH_LIST_PARANOID
  switch (list->count) {
    case 0:
      assert(list->first == NULL);
      assert(list->last == NULL);
      break;

    case 1:
      assert(list->first);
      assert(list->last);
      assert(list->first == list->last);
      break;

    default:
      assert(list->first);
      assert(list->last);
      assert(list->first != list->last);
      break;
  }
#endif
}

static inline int gh_list_init(gh_list_t *newlist) {
  newlist->count = 0;
  newlist->first = NULL;
  newlist->last  = NULL;
  return 0;
}

static inline int gh_list_append(gh_list_t *list, gh_lnode_t *lnode) {
  gh_lnode_t *oldlast;

  paranoid_list_check(list);

  oldlast = list->last;
  if (oldlast)
    oldlast->next = lnode;

  lnode->prev = oldlast;
  lnode->next = NULL;

  list->count++;
  list->last = lnode;

  if (list->count == 1)
    list->first = lnode;

  paranoid_list_check(list);
  return 0;
}

static inline int gh_list_prepend(gh_list_t *list, gh_lnode_t *lnode) {
  gh_lnode_t *oldfirst;

  paranoid_list_check(list);

  oldfirst = list->first;
  if (oldfirst)
    oldfirst->prev = lnode;

  lnode->next = oldfirst;
  lnode->prev = NULL;

  list->count++;
  list->first = lnode;

  if (list->count == 1)
    list->last = lnode;

  paranoid_list_check(list);
  return 0;
}

static inline int gh_list_insert_before(gh_list_t *list, gh_lnode_t *before,
                                        gh_lnode_t *lnode) {
  paranoid_list_check(list);

  lnode->prev = before->prev;
  lnode->next = before;

  if (before->prev)
    before->prev->next = lnode;
  else
    list->first = lnode;

  before->prev = lnode;
  list->count++;

  paranoid_list_check(list);
  return 0;
}

static inline gh_lnode_t *gh_list_pop(gh_list_t *list) {
  gh_lnode_t *elem;

  paranoid_list_check(list);

  elem = list->first;
  if (!elem) {
    paranoid_list_check(list);
    return NULL;
  }

  list->first = list->first->next;
  if (list->first)
    list->first->prev = NULL;

  list->count--;

  if (list->count < 2)
    list->last = list->first;

  elem->prev = NULL;
  elem->next = NULL;

  paranoid_list_check(list);
  return elem;
}

static inline int gh_list_remove(gh_list_t *list, gh_lnode_t *lnode) {
  paranoid_list_check(list);

  if (lnode->prev) {
    lnode->prev->next = lnode->next;
  } else {
    assert(list->first == lnode);
    list->first = lnode->next;
  }

  if (lnode->next) {
    lnode->next->prev = lnode->prev;
  } else {
    assert(list->last == lnode);
    list->last = lnode->prev;
  }

  lnode->prev = NULL;
  lnode->next = NULL;

  list->count--;

  paranoid_list_check(list);
  return 0;
}

static inline int gh_list_free(gh_list_t *list) {
  paranoid_list_check(list);

  while (list->count > 0)
    gh_list_pop(list);

  paranoid_list_check(list);
  memset(list, 0, sizeof(gh_list_t));
  return 0;
}

static inline int gh_list_move_front(gh_list_t *list, gh_lnode_t *lnode) {
  paranoid_list_check(list);
  if (list->first == lnode)
    return 0;

  /* remove element from its current position */
  lnode->prev->next = lnode->next;

  if (lnode->next) {
    lnode->next->prev = lnode->prev;
  } else {
    assert(list->last == lnode);
    list->last = lnode->prev;
  }

  /* add element to the beginning of the list */
  list->first->prev = lnode;
  lnode->next = list->first;
  lnode->prev = NULL;
  list->first = lnode;

  paranoid_list_check(list);
  return 0;
}

/* Take a LIST ELEMENT (not just the data) and return the next one */
static inline gh_lnode_t *gh_lnode_next(gh_lnode_t *elem) {
  return elem->next;
}

/* Same as above but return the previous element */
static inline gh_lnode_t *gh_lnode_prev(gh_lnode_t *elem) {
  return elem->prev;
}

/* Take a LIST (not a list element) and return the first element */
static inline gh_lnode_t *gh_list_first_elem(gh_list_t *list) {
  return list->first;
}

/* Same as above but return the last element */
static inline gh_lnode_t *gh_list_last_elem(gh_list_t *list) {
  return list->last;
}

static inline unsigned int gh_list_count(gh_list_t *list) {
  return list->count;
}

#endif /* GH_LIST_H */