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
|
/*======================================================================
FILE: icalarray.c
CREATOR: Damon Chaplin 07 March 2001
(C) COPYRIGHT 2001, Ximian, Inc.
This library is free software; you can redistribute it and/or modify
it under the terms of either:
The LGPL as published by the Free Software Foundation, version
2.1, available at: https://www.gnu.org/licenses/lgpl-2.1.html
Or:
The Mozilla Public License Version 2.0. You may obtain a copy of
the License at https://www.mozilla.org/MPL/
======================================================================*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "icalarray.h"
#include "icalerror.h"
#include "icalmemory.h"
#include "qsort_gen.h"
#include <stdlib.h>
#include <string.h>
static void icalarray_expand(icalarray *array, size_t space_needed);
icalarray *icalarray_new(size_t element_size, size_t increment_size)
{
icalarray *array;
array = (icalarray *) icalmemory_new_buffer(sizeof(icalarray));
if (!array) {
icalerror_set_errno(ICAL_NEWFAILED_ERROR);
return NULL;
}
array->element_size = element_size;
array->increment_size = increment_size;
array->num_elements = 0;
array->space_allocated = 0;
array->chunks = NULL;
return array;
}
static void *icalarray_alloc_chunk(icalarray *array)
{
void *chunk = icalmemory_new_buffer(array->element_size * array->increment_size);
if (!chunk) {
icalerror_set_errno(ICAL_NEWFAILED_ERROR);
}
return chunk;
}
icalarray *icalarray_copy(icalarray *originalarray)
{
icalarray *array = icalarray_new(originalarray->element_size, originalarray->increment_size);
size_t chunks = originalarray->space_allocated / originalarray->increment_size;
size_t chunk;
if (!array) {
return NULL;
}
array->chunks = icalmemory_new_buffer(chunks * sizeof(void *));
if (array->chunks) {
for (chunk = 0; chunk < chunks; chunk++) {
array->chunks[chunk] = icalarray_alloc_chunk(array);
if (array->chunks[chunk]) {
memcpy(array->chunks[chunk], originalarray->chunks[chunk],
array->increment_size * array->element_size);
array->space_allocated += array->increment_size;
} else {
icalerror_set_errno(ICAL_ALLOCATION_ERROR);
icalarray_free(array);
return NULL;
}
}
} else {
icalerror_set_errno(ICAL_ALLOCATION_ERROR);
icalarray_free(array);
return NULL;
}
array->num_elements = originalarray->num_elements;
return array;
}
void icalarray_free(icalarray *array)
{
if (array->chunks) {
size_t chunks = array->space_allocated / array->increment_size;
size_t chunk;
for (chunk = 0; chunk < chunks; chunk++) {
icalmemory_free_buffer(array->chunks[chunk]);
}
icalmemory_free_buffer(array->chunks);
array->chunks = 0;
}
icalmemory_free_buffer(array);
}
void icalarray_append(icalarray *array, const void *element)
{
size_t pos;
if (array->num_elements >= array->space_allocated) {
icalarray_expand(array, 1);
if (array->num_elements >= array->space_allocated) {
/* expansion failed. Error has already been set. */
return;
}
}
pos = array->num_elements++;
memcpy(icalarray_element_at(array, pos), element, array->element_size);
}
void *icalarray_element_at(icalarray *array, size_t position)
{
size_t chunk = position / array->increment_size;
size_t offset = position % array->increment_size;
return (char *)(array->chunks[chunk]) + (offset * array->element_size);
}
void icalarray_remove_element_at(icalarray *array, size_t position)
{
while (position < array->num_elements - 1) {
memmove(icalarray_element_at(array, position),
icalarray_element_at(array, position + 1), array->element_size);
position++;
}
array->num_elements--;
}
struct _icalarray_sort_context {
icalarray *array;
int (*compare)(const void *, const void *);
};
static int icalarray_fcompare(const void *context, size_t i, size_t j) {
struct _icalarray_sort_context *sort_context = (struct _icalarray_sort_context *)context;
void *pI = icalarray_element_at(sort_context->array, i);
void *pJ = icalarray_element_at(sort_context->array, j);
return sort_context->compare(pI, pJ);
}
static void icalarray_fswap(void *context, size_t i, size_t j) {
struct _icalarray_sort_context *sort_context = (struct _icalarray_sort_context *)context;
void *pI = icalarray_element_at(sort_context->array, i);
void *pJ = icalarray_element_at(sort_context->array, j);
qsort_gen_memswap(pI, pJ, sort_context->array->element_size);
}
void icalarray_sort(icalarray *array, int (*compare) (const void *, const void *))
{
struct _icalarray_sort_context sort_context;
sort_context.array = array;
sort_context.compare = compare;
if (array->num_elements <= 1) {
return;
}
qsort_gen(&sort_context, array->num_elements, icalarray_fcompare, icalarray_fswap);
}
static void icalarray_expand(icalarray *array, size_t space_needed)
{
size_t num_chunks = array->space_allocated / array->increment_size;
size_t num_new_chunks;
size_t c;
void **new_chunks;
num_new_chunks = (space_needed + array->increment_size - 1) / array->increment_size;
if (!num_new_chunks) {
num_new_chunks = 1;
}
new_chunks = icalmemory_new_buffer((num_chunks + num_new_chunks) * sizeof(void *));
if (new_chunks) {
if (array->chunks && num_chunks) {
memcpy(new_chunks, array->chunks, num_chunks * sizeof(void *));
}
for (c = 0; c < num_new_chunks; c++) {
new_chunks[c + num_chunks] = icalarray_alloc_chunk(array);
if (!new_chunks[c + num_chunks]) {
num_new_chunks = c;
break;
}
}
if (array->chunks) {
icalmemory_free_buffer(array->chunks);
}
array->chunks = new_chunks;
array->space_allocated = array->space_allocated + num_new_chunks * array->increment_size;
} else {
icalerror_set_errno(ICAL_ALLOCATION_ERROR);
}
}
|