forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 9
/
linkedqueue.c
executable file
·118 lines (96 loc) · 2.09 KB
/
linkedqueue.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
#include <string.h>
#include "linkedqueue.h"
static int LinkedQueue_Add(LinkedQueue *l, const void *Data)
{
ListHead *d;
/* Fill the new node */
if( l == NULL || Data == NULL )
{
return -10;
}
d = SafeMalloc(sizeof(ListHead) + l->DataLength);
if( d == NULL )
{
return -16;
}
memcpy((void *)(d + 1), Data, l->DataLength);
/* Find the place to add */
if( l->First == NULL || l->Compare(Data, (void *)(l->First + 1)) <= 0 )
{
d->Next = l->First;
l->First = d;
} else {
ListHead *n;
n = l->First;
while( n->Next != NULL && l->Compare(Data, (void *)(n->Next + 1)) > 0 )
{
n = n->Next;
}
d->Next = n->Next;
n->Next = d;
}
return 0;
}
static void *LinkedQueue_Get(LinkedQueue *l)
{
ListHead *h;
if( l == NULL )
{
return NULL;
}
if( l->First == NULL )
{
return NULL;
}
h = l->First;
l->First = h->Next;
return (void *)(h + 1);
}
static void LinkedQueue_Free(LinkedQueue *l)
{
void *d;
while( (d = LinkedQueue_Get(l)) != NULL )
{
LinkedQueue_FreeNode(d);
}
}
int LinkedQueue_Init(LinkedQueue *l,
int DataLength,
int (*CompareFunc)(const void *One, const void *Two)
)
{
l->First = NULL;
l->DataLength = DataLength;
l->Compare = CompareFunc;
l->Add = LinkedQueue_Add;
l->Get = LinkedQueue_Get;
l->Free = LinkedQueue_Free;
return 0;
}
/** Iterator Implementation */
static void *LinkedQueueIterator_Next(LinkedQueueIterator *i)
{
if( i->Current == NULL )
{
i->Current = i->l->First;
} else {
i->Current = i->Current->Next;
}
if( i->Current == NULL )
{
return NULL;
} else {
return (void *)(i->Current + 1);
}
}
int LinkedQueueIterator_Init(LinkedQueueIterator *i, LinkedQueue *l)
{
if( i == NULL )
{
return -96;
}
i->l = l;
i->Current = NULL;
i->Next = LinkedQueueIterator_Next;
return 0;
}