annotate libobjc/sarray.c @ 136:4627f235cf2a

fix c-next example
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 08 Nov 2018 14:11:56 +0900
parents 84e7813d76e9
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Sparse Arrays for Objective C dispatch tables
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 1993-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify
kono
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
kono
parents:
diff changeset
8 the Free Software Foundation; either version 3, or (at your option)
kono
parents:
diff changeset
9 any later version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful,
kono
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
14 GNU General Public License for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
17 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
18 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
21 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
23 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 #include "objc-private/common.h"
kono
parents:
diff changeset
26 #include "objc-private/sarray.h"
kono
parents:
diff changeset
27 #include "objc/runtime.h" /* For objc_malloc */
kono
parents:
diff changeset
28 #include "objc/thr.h" /* For objc_mutex_lock */
kono
parents:
diff changeset
29 #include "objc-private/module-abi-8.h"
kono
parents:
diff changeset
30 #include "objc-private/runtime.h"
kono
parents:
diff changeset
31 #include <stdio.h>
kono
parents:
diff changeset
32 #include <string.h> /* For memset */
kono
parents:
diff changeset
33 #include <assert.h> /* For assert */
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 int nbuckets = 0; /* !T:MUTEX */
kono
parents:
diff changeset
36 int nindices = 0; /* !T:MUTEX */
kono
parents:
diff changeset
37 int narrays = 0; /* !T:MUTEX */
kono
parents:
diff changeset
38 int idxsize = 0; /* !T:MUTEX */
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 static void *first_free_data = NULL; /* !T:MUTEX */
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 #ifdef OBJC_SPARSE2
kono
parents:
diff changeset
43 const char *__objc_sparse2_id = "2 level sparse indices";
kono
parents:
diff changeset
44 #endif
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
47 const char *__objc_sparse3_id = "3 level sparse indices";
kono
parents:
diff changeset
48 #endif
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 /* This function removes any structures left over from free operations
kono
parents:
diff changeset
51 that were not safe in a multi-threaded environment. */
kono
parents:
diff changeset
52 void
kono
parents:
diff changeset
53 sarray_remove_garbage (void)
kono
parents:
diff changeset
54 {
kono
parents:
diff changeset
55 void **vp;
kono
parents:
diff changeset
56 void *np;
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 objc_mutex_lock (__objc_runtime_mutex);
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 vp = first_free_data;
kono
parents:
diff changeset
61 first_free_data = NULL;
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 while (vp)
kono
parents:
diff changeset
64 {
kono
parents:
diff changeset
65 np = *vp;
kono
parents:
diff changeset
66 objc_free (vp);
kono
parents:
diff changeset
67 vp = np;
kono
parents:
diff changeset
68 }
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 objc_mutex_unlock (__objc_runtime_mutex);
kono
parents:
diff changeset
71 }
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 /* Free a block of dynamically allocated memory. If we are in
kono
parents:
diff changeset
74 multi-threaded mode, it is ok to free it. If not, we add it to the
kono
parents:
diff changeset
75 garbage heap to be freed later. */
kono
parents:
diff changeset
76 static void
kono
parents:
diff changeset
77 sarray_free_garbage (void *vp)
kono
parents:
diff changeset
78 {
kono
parents:
diff changeset
79 objc_mutex_lock (__objc_runtime_mutex);
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 if (__objc_runtime_threads_alive == 1)
kono
parents:
diff changeset
82 {
kono
parents:
diff changeset
83 objc_free (vp);
kono
parents:
diff changeset
84 if (first_free_data)
kono
parents:
diff changeset
85 sarray_remove_garbage ();
kono
parents:
diff changeset
86 }
kono
parents:
diff changeset
87 else
kono
parents:
diff changeset
88 {
kono
parents:
diff changeset
89 *(void **)vp = first_free_data;
kono
parents:
diff changeset
90 first_free_data = vp;
kono
parents:
diff changeset
91 }
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 objc_mutex_unlock (__objc_runtime_mutex);
kono
parents:
diff changeset
94 }
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 /* sarray_at_put copies data in such a way as to be thread reader
kono
parents:
diff changeset
97 safe. */
kono
parents:
diff changeset
98 void
kono
parents:
diff changeset
99 sarray_at_put (struct sarray *array, sidx index, void *element)
kono
parents:
diff changeset
100 {
kono
parents:
diff changeset
101 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
102 struct sindex **the_index;
kono
parents:
diff changeset
103 struct sindex *new_index;
kono
parents:
diff changeset
104 #endif
kono
parents:
diff changeset
105 struct sbucket **the_bucket;
kono
parents:
diff changeset
106 struct sbucket *new_bucket;
kono
parents:
diff changeset
107 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
108 size_t ioffset;
kono
parents:
diff changeset
109 #endif
kono
parents:
diff changeset
110 size_t boffset;
kono
parents:
diff changeset
111 size_t eoffset;
kono
parents:
diff changeset
112 #ifdef PRECOMPUTE_SELECTORS
kono
parents:
diff changeset
113 union sofftype xx;
kono
parents:
diff changeset
114 xx.idx = index;
kono
parents:
diff changeset
115 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
116 ioffset = xx.off.ioffset;
kono
parents:
diff changeset
117 #endif
kono
parents:
diff changeset
118 boffset = xx.off.boffset;
kono
parents:
diff changeset
119 eoffset = xx.off.eoffset;
kono
parents:
diff changeset
120 #else /* not PRECOMPUTE_SELECTORS */
kono
parents:
diff changeset
121 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
122 ioffset = index/INDEX_CAPACITY;
kono
parents:
diff changeset
123 boffset = (index/BUCKET_SIZE)%INDEX_SIZE;
kono
parents:
diff changeset
124 eoffset = index%BUCKET_SIZE;
kono
parents:
diff changeset
125 #else
kono
parents:
diff changeset
126 boffset = index/BUCKET_SIZE;
kono
parents:
diff changeset
127 eoffset = index%BUCKET_SIZE;
kono
parents:
diff changeset
128 #endif
kono
parents:
diff changeset
129 #endif /* not PRECOMPUTE_SELECTORS */
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 assert (soffset_decode (index) < array->capacity); /* Range check */
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
134 the_index = &(array->indices[ioffset]);
kono
parents:
diff changeset
135 the_bucket = &((*the_index)->buckets[boffset]);
kono
parents:
diff changeset
136 #else
kono
parents:
diff changeset
137 the_bucket = &(array->buckets[boffset]);
kono
parents:
diff changeset
138 #endif
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 if ((*the_bucket)->elems[eoffset] == element)
kono
parents:
diff changeset
141 return; /* Great! we just avoided a lazy copy. */
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 /* First, perform lazy copy/allocation of index if needed. */
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 if ((*the_index) == array->empty_index)
kono
parents:
diff changeset
148 {
kono
parents:
diff changeset
149 /* The index was previously empty, allocate a new. */
kono
parents:
diff changeset
150 new_index = (struct sindex *) objc_malloc (sizeof (struct sindex));
kono
parents:
diff changeset
151 memcpy (new_index, array->empty_index, sizeof (struct sindex));
kono
parents:
diff changeset
152 new_index->version.version = array->version.version;
kono
parents:
diff changeset
153 *the_index = new_index; /* Prepared for install. */
kono
parents:
diff changeset
154 the_bucket = &((*the_index)->buckets[boffset]);
kono
parents:
diff changeset
155
kono
parents:
diff changeset
156 nindices += 1;
kono
parents:
diff changeset
157 }
kono
parents:
diff changeset
158 else if ((*the_index)->version.version != array->version.version)
kono
parents:
diff changeset
159 {
kono
parents:
diff changeset
160 /* This index must be lazy copied. */
kono
parents:
diff changeset
161 struct sindex *old_index = *the_index;
kono
parents:
diff changeset
162 new_index = (struct sindex *) objc_malloc (sizeof (struct sindex));
kono
parents:
diff changeset
163 memcpy (new_index, old_index, sizeof (struct sindex));
kono
parents:
diff changeset
164 new_index->version.version = array->version.version;
kono
parents:
diff changeset
165 *the_index = new_index; /* Prepared for install. */
kono
parents:
diff changeset
166 the_bucket = &((*the_index)->buckets[boffset]);
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 nindices += 1;
kono
parents:
diff changeset
169 }
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 #endif /* OBJC_SPARSE3 */
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 /* Next, perform lazy allocation/copy of the bucket if needed. */
kono
parents:
diff changeset
174 if ((*the_bucket) == array->empty_bucket)
kono
parents:
diff changeset
175 {
kono
parents:
diff changeset
176 /* The bucket was previously empty (or something like that),
kono
parents:
diff changeset
177 allocate a new. This is the effect of `lazy' allocation. */
kono
parents:
diff changeset
178 new_bucket = (struct sbucket *) objc_malloc (sizeof (struct sbucket));
kono
parents:
diff changeset
179 memcpy ((void *) new_bucket, (const void *) array->empty_bucket,
kono
parents:
diff changeset
180 sizeof (struct sbucket));
kono
parents:
diff changeset
181 new_bucket->version.version = array->version.version;
kono
parents:
diff changeset
182 *the_bucket = new_bucket; /* Prepared for install. */
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 nbuckets += 1;
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 }
kono
parents:
diff changeset
187 else if ((*the_bucket)->version.version != array->version.version)
kono
parents:
diff changeset
188 {
kono
parents:
diff changeset
189 /* Perform lazy copy. */
kono
parents:
diff changeset
190 struct sbucket *old_bucket = *the_bucket;
kono
parents:
diff changeset
191 new_bucket = (struct sbucket *) objc_malloc (sizeof (struct sbucket));
kono
parents:
diff changeset
192 memcpy (new_bucket, old_bucket, sizeof (struct sbucket));
kono
parents:
diff changeset
193 new_bucket->version.version = array->version.version;
kono
parents:
diff changeset
194 *the_bucket = new_bucket; /* Prepared for install. */
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 nbuckets += 1;
kono
parents:
diff changeset
197 }
kono
parents:
diff changeset
198 (*the_bucket)->elems[eoffset] = element;
kono
parents:
diff changeset
199 }
kono
parents:
diff changeset
200
kono
parents:
diff changeset
201 void
kono
parents:
diff changeset
202 sarray_at_put_safe (struct sarray *array, sidx index, void *element)
kono
parents:
diff changeset
203 {
kono
parents:
diff changeset
204 if (soffset_decode (index) >= array->capacity)
kono
parents:
diff changeset
205 sarray_realloc (array, soffset_decode (index) + 1);
kono
parents:
diff changeset
206 sarray_at_put (array, index, element);
kono
parents:
diff changeset
207 }
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 struct sarray *
kono
parents:
diff changeset
210 sarray_new (int size, void *default_element)
kono
parents:
diff changeset
211 {
kono
parents:
diff changeset
212 struct sarray *arr;
kono
parents:
diff changeset
213 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
214 size_t num_indices = ((size - 1)/(INDEX_CAPACITY)) + 1;
kono
parents:
diff changeset
215 struct sindex **new_indices;
kono
parents:
diff changeset
216 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
217 size_t num_indices = ((size - 1)/BUCKET_SIZE) + 1;
kono
parents:
diff changeset
218 struct sbucket **new_buckets;
kono
parents:
diff changeset
219 #endif
kono
parents:
diff changeset
220 size_t counter;
kono
parents:
diff changeset
221
kono
parents:
diff changeset
222 assert (size > 0);
kono
parents:
diff changeset
223
kono
parents:
diff changeset
224 /* Allocate core array. */
kono
parents:
diff changeset
225 arr = (struct sarray *) objc_malloc (sizeof (struct sarray));
kono
parents:
diff changeset
226 arr->version.version = 0;
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 /* Initialize members. */
kono
parents:
diff changeset
229 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
230 arr->capacity = num_indices*INDEX_CAPACITY;
kono
parents:
diff changeset
231 new_indices = (struct sindex **)
kono
parents:
diff changeset
232 objc_malloc (sizeof (struct sindex *) * num_indices);
kono
parents:
diff changeset
233
kono
parents:
diff changeset
234 arr->empty_index = (struct sindex *) objc_malloc (sizeof (struct sindex));
kono
parents:
diff changeset
235 arr->empty_index->version.version = 0;
kono
parents:
diff changeset
236
kono
parents:
diff changeset
237 narrays += 1;
kono
parents:
diff changeset
238 idxsize += num_indices;
kono
parents:
diff changeset
239 nindices += 1;
kono
parents:
diff changeset
240
kono
parents:
diff changeset
241 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
242 arr->capacity = num_indices*BUCKET_SIZE;
kono
parents:
diff changeset
243 new_buckets = (struct sbucket **)
kono
parents:
diff changeset
244 objc_malloc (sizeof (struct sbucket *) * num_indices);
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 narrays += 1;
kono
parents:
diff changeset
247 idxsize += num_indices;
kono
parents:
diff changeset
248
kono
parents:
diff changeset
249 #endif
kono
parents:
diff changeset
250
kono
parents:
diff changeset
251 arr->empty_bucket = (struct sbucket *) objc_malloc (sizeof (struct sbucket));
kono
parents:
diff changeset
252 arr->empty_bucket->version.version = 0;
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 nbuckets += 1;
kono
parents:
diff changeset
255
kono
parents:
diff changeset
256 arr->ref_count = 1;
kono
parents:
diff changeset
257 arr->is_copy_of = (struct sarray *) 0;
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 for (counter = 0; counter < BUCKET_SIZE; counter++)
kono
parents:
diff changeset
260 arr->empty_bucket->elems[counter] = default_element;
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
263 for (counter = 0; counter < INDEX_SIZE; counter++)
kono
parents:
diff changeset
264 arr->empty_index->buckets[counter] = arr->empty_bucket;
kono
parents:
diff changeset
265
kono
parents:
diff changeset
266 for (counter = 0; counter < num_indices; counter++)
kono
parents:
diff changeset
267 new_indices[counter] = arr->empty_index;
kono
parents:
diff changeset
268
kono
parents:
diff changeset
269 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
270
kono
parents:
diff changeset
271 for (counter = 0; counter < num_indices; counter++)
kono
parents:
diff changeset
272 new_buckets[counter] = arr->empty_bucket;
kono
parents:
diff changeset
273
kono
parents:
diff changeset
274 #endif
kono
parents:
diff changeset
275
kono
parents:
diff changeset
276 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
277 arr->indices = new_indices;
kono
parents:
diff changeset
278 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
279 arr->buckets = new_buckets;
kono
parents:
diff changeset
280 #endif
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 return arr;
kono
parents:
diff changeset
283 }
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285
kono
parents:
diff changeset
286 /* Reallocate the sparse array to hold `newsize' entries Note: We
kono
parents:
diff changeset
287 really allocate and then free. We have to do this to ensure that
kono
parents:
diff changeset
288 any concurrent readers notice the update. */
kono
parents:
diff changeset
289 void
kono
parents:
diff changeset
290 sarray_realloc (struct sarray *array, int newsize)
kono
parents:
diff changeset
291 {
kono
parents:
diff changeset
292 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
293 size_t old_max_index = (array->capacity - 1)/INDEX_CAPACITY;
kono
parents:
diff changeset
294 size_t new_max_index = ((newsize - 1)/INDEX_CAPACITY);
kono
parents:
diff changeset
295 size_t rounded_size = (new_max_index + 1) * INDEX_CAPACITY;
kono
parents:
diff changeset
296
kono
parents:
diff changeset
297 struct sindex **new_indices;
kono
parents:
diff changeset
298 struct sindex **old_indices;
kono
parents:
diff changeset
299
kono
parents:
diff changeset
300 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
301 size_t old_max_index = (array->capacity - 1)/BUCKET_SIZE;
kono
parents:
diff changeset
302 size_t new_max_index = ((newsize - 1)/BUCKET_SIZE);
kono
parents:
diff changeset
303 size_t rounded_size = (new_max_index + 1) * BUCKET_SIZE;
kono
parents:
diff changeset
304
kono
parents:
diff changeset
305 struct sbucket **new_buckets;
kono
parents:
diff changeset
306 struct sbucket **old_buckets;
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308 #endif
kono
parents:
diff changeset
309
kono
parents:
diff changeset
310 size_t counter;
kono
parents:
diff changeset
311
kono
parents:
diff changeset
312 assert (newsize > 0);
kono
parents:
diff changeset
313
kono
parents:
diff changeset
314 /* The size is the same, just ignore the request. */
kono
parents:
diff changeset
315 if (rounded_size <= array->capacity)
kono
parents:
diff changeset
316 return;
kono
parents:
diff changeset
317
kono
parents:
diff changeset
318 assert (array->ref_count == 1); /* stop if lazy copied... */
kono
parents:
diff changeset
319
kono
parents:
diff changeset
320 /* We are asked to extend the array -- allocate new bucket table,
kono
parents:
diff changeset
321 and insert empty_bucket in newly allocated places. */
kono
parents:
diff changeset
322 if (rounded_size > array->capacity)
kono
parents:
diff changeset
323 {
kono
parents:
diff changeset
324 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
325 new_max_index += 4;
kono
parents:
diff changeset
326 rounded_size = (new_max_index + 1) * INDEX_CAPACITY;
kono
parents:
diff changeset
327 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
328 new_max_index += 4;
kono
parents:
diff changeset
329 rounded_size = (new_max_index + 1) * BUCKET_SIZE;
kono
parents:
diff changeset
330 #endif
kono
parents:
diff changeset
331
kono
parents:
diff changeset
332 /* Update capacity. */
kono
parents:
diff changeset
333 array->capacity = rounded_size;
kono
parents:
diff changeset
334
kono
parents:
diff changeset
335 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
336 /* Alloc to force re-read by any concurrent readers. */
kono
parents:
diff changeset
337 old_indices = array->indices;
kono
parents:
diff changeset
338 new_indices = (struct sindex **)
kono
parents:
diff changeset
339 objc_malloc ((new_max_index + 1) * sizeof (struct sindex *));
kono
parents:
diff changeset
340 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
341 old_buckets = array->buckets;
kono
parents:
diff changeset
342 new_buckets = (struct sbucket **)
kono
parents:
diff changeset
343 objc_malloc ((new_max_index + 1) * sizeof (struct sbucket *));
kono
parents:
diff changeset
344 #endif
kono
parents:
diff changeset
345
kono
parents:
diff changeset
346 /* Copy buckets below old_max_index (they are still valid). */
kono
parents:
diff changeset
347 for (counter = 0; counter <= old_max_index; counter++ )
kono
parents:
diff changeset
348 {
kono
parents:
diff changeset
349 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
350 new_indices[counter] = old_indices[counter];
kono
parents:
diff changeset
351 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
352 new_buckets[counter] = old_buckets[counter];
kono
parents:
diff changeset
353 #endif
kono
parents:
diff changeset
354 }
kono
parents:
diff changeset
355
kono
parents:
diff changeset
356 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
357 /* Reset entries above old_max_index to empty_bucket. */
kono
parents:
diff changeset
358 for (counter = old_max_index + 1; counter <= new_max_index; counter++)
kono
parents:
diff changeset
359 new_indices[counter] = array->empty_index;
kono
parents:
diff changeset
360 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
361 /* Reset entries above old_max_index to empty_bucket. */
kono
parents:
diff changeset
362 for (counter = old_max_index + 1; counter <= new_max_index; counter++)
kono
parents:
diff changeset
363 new_buckets[counter] = array->empty_bucket;
kono
parents:
diff changeset
364 #endif
kono
parents:
diff changeset
365
kono
parents:
diff changeset
366 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
367 /* Install the new indices. */
kono
parents:
diff changeset
368 array->indices = new_indices;
kono
parents:
diff changeset
369 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
370 array->buckets = new_buckets;
kono
parents:
diff changeset
371 #endif
kono
parents:
diff changeset
372
kono
parents:
diff changeset
373 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
374 /* Free the old indices. */
kono
parents:
diff changeset
375 sarray_free_garbage (old_indices);
kono
parents:
diff changeset
376 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
377 sarray_free_garbage (old_buckets);
kono
parents:
diff changeset
378 #endif
kono
parents:
diff changeset
379
kono
parents:
diff changeset
380 idxsize += (new_max_index-old_max_index);
kono
parents:
diff changeset
381 return;
kono
parents:
diff changeset
382 }
kono
parents:
diff changeset
383 }
kono
parents:
diff changeset
384
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 /* Free a sparse array allocated with sarray_new */
kono
parents:
diff changeset
387 void
kono
parents:
diff changeset
388 sarray_free (struct sarray *array) {
kono
parents:
diff changeset
389 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
390 size_t old_max_index = (array->capacity - 1)/INDEX_CAPACITY;
kono
parents:
diff changeset
391 struct sindex **old_indices;
kono
parents:
diff changeset
392 #else
kono
parents:
diff changeset
393 size_t old_max_index = (array->capacity - 1)/BUCKET_SIZE;
kono
parents:
diff changeset
394 struct sbucket **old_buckets;
kono
parents:
diff changeset
395 #endif
kono
parents:
diff changeset
396 size_t counter = 0;
kono
parents:
diff changeset
397
kono
parents:
diff changeset
398 assert (array->ref_count != 0); /* Freed multiple times!!! */
kono
parents:
diff changeset
399
kono
parents:
diff changeset
400 if (--(array->ref_count) != 0) /* There exists copies of me */
kono
parents:
diff changeset
401 return;
kono
parents:
diff changeset
402
kono
parents:
diff changeset
403 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
404 old_indices = array->indices;
kono
parents:
diff changeset
405 #else
kono
parents:
diff changeset
406 old_buckets = array->buckets;
kono
parents:
diff changeset
407 #endif
kono
parents:
diff changeset
408
kono
parents:
diff changeset
409 /* Free all entries that do not point to empty_bucket. */
kono
parents:
diff changeset
410 for (counter = 0; counter <= old_max_index; counter++ )
kono
parents:
diff changeset
411 {
kono
parents:
diff changeset
412 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
413 struct sindex *idx = old_indices[counter];
kono
parents:
diff changeset
414 if ((idx != array->empty_index)
kono
parents:
diff changeset
415 && (idx->version.version == array->version.version))
kono
parents:
diff changeset
416 {
kono
parents:
diff changeset
417 int c2;
kono
parents:
diff changeset
418 for (c2 = 0; c2 < INDEX_SIZE; c2++)
kono
parents:
diff changeset
419 {
kono
parents:
diff changeset
420 struct sbucket *bkt = idx->buckets[c2];
kono
parents:
diff changeset
421 if ((bkt != array->empty_bucket)
kono
parents:
diff changeset
422 && (bkt->version.version == array->version.version))
kono
parents:
diff changeset
423 {
kono
parents:
diff changeset
424 sarray_free_garbage (bkt);
kono
parents:
diff changeset
425 nbuckets -= 1;
kono
parents:
diff changeset
426 }
kono
parents:
diff changeset
427 }
kono
parents:
diff changeset
428 sarray_free_garbage (idx);
kono
parents:
diff changeset
429 nindices -= 1;
kono
parents:
diff changeset
430 }
kono
parents:
diff changeset
431 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
432 struct sbucket *bkt = old_buckets[counter];
kono
parents:
diff changeset
433 if ((bkt != array->empty_bucket)
kono
parents:
diff changeset
434 && (bkt->version.version == array->version.version))
kono
parents:
diff changeset
435 {
kono
parents:
diff changeset
436 sarray_free_garbage (bkt);
kono
parents:
diff changeset
437 nbuckets -= 1;
kono
parents:
diff changeset
438 }
kono
parents:
diff changeset
439 #endif
kono
parents:
diff changeset
440 }
kono
parents:
diff changeset
441
kono
parents:
diff changeset
442 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
443 /* Free empty_index. */
kono
parents:
diff changeset
444 if (array->empty_index->version.version == array->version.version)
kono
parents:
diff changeset
445 {
kono
parents:
diff changeset
446 sarray_free_garbage (array->empty_index);
kono
parents:
diff changeset
447 nindices -= 1;
kono
parents:
diff changeset
448 }
kono
parents:
diff changeset
449 #endif
kono
parents:
diff changeset
450
kono
parents:
diff changeset
451 /* Free empty_bucket. */
kono
parents:
diff changeset
452 if (array->empty_bucket->version.version == array->version.version)
kono
parents:
diff changeset
453 {
kono
parents:
diff changeset
454 sarray_free_garbage (array->empty_bucket);
kono
parents:
diff changeset
455 nbuckets -= 1;
kono
parents:
diff changeset
456 }
kono
parents:
diff changeset
457 idxsize -= (old_max_index + 1);
kono
parents:
diff changeset
458 narrays -= 1;
kono
parents:
diff changeset
459
kono
parents:
diff changeset
460 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
461 /* Free bucket table. */
kono
parents:
diff changeset
462 sarray_free_garbage (array->indices);
kono
parents:
diff changeset
463 #else
kono
parents:
diff changeset
464 /* Free bucket table. */
kono
parents:
diff changeset
465 sarray_free_garbage (array->buckets);
kono
parents:
diff changeset
466 #endif
kono
parents:
diff changeset
467
kono
parents:
diff changeset
468 /* If this is a copy of another array, we free it (which might just
kono
parents:
diff changeset
469 decrement its reference count so it will be freed when no longer
kono
parents:
diff changeset
470 in use). */
kono
parents:
diff changeset
471 if (array->is_copy_of)
kono
parents:
diff changeset
472 sarray_free (array->is_copy_of);
kono
parents:
diff changeset
473
kono
parents:
diff changeset
474 /* Free array. */
kono
parents:
diff changeset
475 sarray_free_garbage (array);
kono
parents:
diff changeset
476 }
kono
parents:
diff changeset
477
kono
parents:
diff changeset
478 /* This is a lazy copy. Only the core of the structure is actually
kono
parents:
diff changeset
479 copied. */
kono
parents:
diff changeset
480 struct sarray *
kono
parents:
diff changeset
481 sarray_lazy_copy (struct sarray *oarr)
kono
parents:
diff changeset
482 {
kono
parents:
diff changeset
483 struct sarray *arr;
kono
parents:
diff changeset
484
kono
parents:
diff changeset
485 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
486 size_t num_indices = ((oarr->capacity - 1)/INDEX_CAPACITY) + 1;
kono
parents:
diff changeset
487 struct sindex **new_indices;
kono
parents:
diff changeset
488 #else /* OBJC_SPARSE2 */
kono
parents:
diff changeset
489 size_t num_indices = ((oarr->capacity - 1)/BUCKET_SIZE) + 1;
kono
parents:
diff changeset
490 struct sbucket **new_buckets;
kono
parents:
diff changeset
491 #endif
kono
parents:
diff changeset
492
kono
parents:
diff changeset
493 /* Allocate core array. */
kono
parents:
diff changeset
494 arr = (struct sarray *) objc_malloc (sizeof (struct sarray)); /* !!! */
kono
parents:
diff changeset
495 arr->version.version = oarr->version.version + 1;
kono
parents:
diff changeset
496 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
497 arr->empty_index = oarr->empty_index;
kono
parents:
diff changeset
498 #endif
kono
parents:
diff changeset
499 arr->empty_bucket = oarr->empty_bucket;
kono
parents:
diff changeset
500 arr->ref_count = 1;
kono
parents:
diff changeset
501 oarr->ref_count += 1;
kono
parents:
diff changeset
502 arr->is_copy_of = oarr;
kono
parents:
diff changeset
503 arr->capacity = oarr->capacity;
kono
parents:
diff changeset
504
kono
parents:
diff changeset
505 #ifdef OBJC_SPARSE3
kono
parents:
diff changeset
506 /* Copy bucket table. */
kono
parents:
diff changeset
507 new_indices = (struct sindex **)
kono
parents:
diff changeset
508 objc_malloc (sizeof (struct sindex *) * num_indices);
kono
parents:
diff changeset
509 memcpy (new_indices, oarr->indices, sizeof (struct sindex *) * num_indices);
kono
parents:
diff changeset
510 arr->indices = new_indices;
kono
parents:
diff changeset
511 #else
kono
parents:
diff changeset
512 /* Copy bucket table. */
kono
parents:
diff changeset
513 new_buckets = (struct sbucket **)
kono
parents:
diff changeset
514 objc_malloc (sizeof (struct sbucket *) * num_indices);
kono
parents:
diff changeset
515 memcpy (new_buckets, oarr->buckets, sizeof (struct sbucket *) * num_indices);
kono
parents:
diff changeset
516 arr->buckets = new_buckets;
kono
parents:
diff changeset
517 #endif
kono
parents:
diff changeset
518
kono
parents:
diff changeset
519 idxsize += num_indices;
kono
parents:
diff changeset
520 narrays += 1;
kono
parents:
diff changeset
521
kono
parents:
diff changeset
522 return arr;
kono
parents:
diff changeset
523 }