forked from yaosj2k/dnsforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.c
executable file
·148 lines (124 loc) · 3.31 KB
/
array.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
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "array.h"
#include "utils.h"
/* if it grows down, the InitialCount will be ignored. Otherwise, TheFirstAddress will be ignored. */
int Array_Init(__in Array *a, __in int DataLength, __in int InitialCount, __in BOOL GrowsDown, __in void *TheFirstAddress /* The first means the biggest address*/)
{
if( InitialCount < 0)
return 1;
a -> DataLength = DataLength;
a -> Used = 0;
if( GrowsDown == FALSE )
{
if( InitialCount > 0 )
{
a -> Data = SafeMalloc(DataLength * InitialCount);
if( a -> Data == NULL )
return 2;
memset(a -> Data, 0, DataLength * InitialCount);
} else {
a -> Data = NULL;
}
a -> Allocated = InitialCount;
} else {
a -> Allocated = -1;
a -> Data = TheFirstAddress;
}
return 0;
}
/* Subscripts are always non-negative. */
void *Array_GetBySubscript(__in const Array *a, __in int Subscript)
{
if( Subscript >= 0 && Subscript < a -> Used )
{
if( a -> Allocated < 0 )
{
Subscript *= (-1);
}
return (void *)((a -> Data) + (a -> DataLength) * Subscript);
} else {
return NULL;
}
}
/* Subscript returned */
int Array_PushBack(__in Array *a, __in_opt const void *Data, __in_opt void *Boundary /* Only used by grow down array */)
{
if( a -> Allocated >= 0 )
{
if( a -> Used == a -> Allocated )
{
int NewCount = (a -> Allocated) < 2 ? 2 : (a -> Allocated) + (a -> Allocated) / 2;
if( SafeRealloc((void **)&(a -> Data), NewCount * (a -> DataLength)) != 0 )
{
return -1;
}
a -> Allocated = NewCount;
}
if( Data != NULL )
memcpy((a -> Data) + (a -> DataLength) * (a -> Used), Data, a -> DataLength);
return (a -> Used)++;
} else {
if( Boundary != NULL && ((a -> Data) + (-1) * (a -> DataLength) * (a -> Used)) < (char *)Boundary )
{
return -1;
} else {
if( Data != NULL )
{
memcpy((a -> Data) + (-1) * (a -> DataLength) * (a -> Used), Data, a -> DataLength);
}
return (a -> Used)++;
}
}
}
void *Array_SetToSubscript(Array *a, int Subscript, const void *Data)
{
if( a -> Allocated >= 0 )
{
if( Subscript >= a -> Allocated )
{
if( SafeRealloc((void **)&(a -> Data), (Subscript + 1) * (a -> DataLength)) != 0 )
return NULL;
a -> Allocated = Subscript + 1;
}
memcpy((a -> Data) + (a -> DataLength) * Subscript, Data, a -> DataLength);
if( a -> Used < Subscript + 1 )
a -> Used = Subscript + 1;
return (a -> Data) + (a -> DataLength) * Subscript;
} else {
if( a -> Used < Subscript + 1 )
{
a -> Used = Subscript + 1;
}
memcpy((a -> Data) + (-1) * (a -> DataLength) * Subscript, Data, a -> DataLength);
return (a -> Data) + (-1) * (a -> DataLength) * Subscript;
}
}
void Array_Sort(Array *a, int (*Compare)(const void *, const void *))
{
if( a -> Allocated < 0 )
{
qsort(a -> Data - (a -> Used * a -> DataLength), a -> Used, a -> DataLength, Compare);
} else {
qsort(a -> Data, a -> Used, a -> DataLength, Compare);
}
}
void Array_Fill(Array *a, int Num, const void *DataSample)
{
int i;
for( i = 0; i < Num; ++i )
{
Array_SetToSubscript(a, i, DataSample);
}
}
void Array_Free(Array *a)
{
if( a -> Allocated >= 0 )
{
SafeFree(a -> Data);
}
a -> Data = NULL;
a -> Used = 0;
a -> Allocated = 0;
}