Changeset 1391 for branches/GNU/src/gcc/libobjc/sarray.c
- Timestamp:
- Apr 27, 2004, 8:39:34 PM (21 years ago)
- Location:
- branches/GNU/src/gcc
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GNU/src/gcc
- Property svn:ignore
-
old new 26 26 configure.vr 27 27 configure.vrs 28 dir.info 28 29 Makefile 29 dir.info30 30 lost+found 31 31 update.out
-
- Property svn:ignore
-
branches/GNU/src/gcc/libobjc/sarray.c
-
Property cvs2svn:cvs-rev
changed from
1.1
to1.1.1.2
r1390 r1391 1 1 /* Sparse Arrays for Objective C dispatch tables 2 Copyright (C) 1993, 1995, 1996 Free Software Foundation, Inc.2 Copyright (C) 1993, 1995, 1996, 2002 Free Software Foundation, Inc. 3 3 4 4 This file is part of GNU CC. … … 35 35 int idxsize = 0; /* !T:MUTEX */ 36 36 37 static void * 37 static void *first_free_data = NULL; /* !T:MUTEX */ 38 38 39 39 #ifdef OBJC_SPARSE2 40 const char *__objc_sparse2_id = "2 level sparse indices";41 #endif 42 43 #ifdef OBJC_SPARSE3 44 const char *__objc_sparse3_id = "3 level sparse indices";40 const char *__objc_sparse2_id = "2 level sparse indices"; 41 #endif 42 43 #ifdef OBJC_SPARSE3 44 const char *__objc_sparse3_id = "3 level sparse indices"; 45 45 #endif 46 46 … … 48 48 that were not safe in a multi-threaded environment. */ 49 49 void 50 sarray_remove_garbage (void)50 sarray_remove_garbage (void) 51 51 { 52 52 void **vp; 53 53 void *np; 54 54 55 objc_mutex_lock (__objc_runtime_mutex);55 objc_mutex_lock (__objc_runtime_mutex); 56 56 57 57 vp = first_free_data; … … 60 60 while (vp) { 61 61 np = *vp; 62 objc_free (vp);62 objc_free (vp); 63 63 vp = np; 64 64 } 65 65 66 objc_mutex_unlock (__objc_runtime_mutex);66 objc_mutex_unlock (__objc_runtime_mutex); 67 67 } 68 68 … … 72 72 73 73 static void 74 sarray_free_garbage (void *vp)75 { 76 objc_mutex_lock (__objc_runtime_mutex);74 sarray_free_garbage (void *vp) 75 { 76 objc_mutex_lock (__objc_runtime_mutex); 77 77 78 78 if (__objc_runtime_threads_alive == 1) { 79 objc_free (vp);79 objc_free (vp); 80 80 if (first_free_data) 81 sarray_remove_garbage ();81 sarray_remove_garbage (); 82 82 } 83 83 else { … … 86 86 } 87 87 88 objc_mutex_unlock (__objc_runtime_mutex);88 objc_mutex_unlock (__objc_runtime_mutex); 89 89 } 90 90 91 91 /* sarray_at_put : copies data in such a way as to be thread reader safe. */ 92 92 void 93 sarray_at_put (struct sarray* array, sidx index, void*element)94 { 95 #ifdef OBJC_SPARSE3 96 struct sindex **the_index;97 struct sindex *new_index;98 #endif 99 struct sbucket **the_bucket;100 struct sbucket *new_bucket;93 sarray_at_put (struct sarray *array, sidx index, void *element) 94 { 95 #ifdef OBJC_SPARSE3 96 struct sindex **the_index; 97 struct sindex *new_index; 98 #endif 99 struct sbucket **the_bucket; 100 struct sbucket *new_bucket; 101 101 #ifdef OBJC_SPARSE3 102 102 size_t ioffset; … … 123 123 #endif /* not PRECOMPUTE_SELECTORS */ 124 124 125 assert (soffset_decode(index) < array->capacity); /* Range check */125 assert (soffset_decode (index) < array->capacity); /* Range check */ 126 126 127 127 #ifdef OBJC_SPARSE3 … … 142 142 143 143 /* The index was previously empty, allocate a new */ 144 new_index = (struct sindex *)objc_malloc(sizeof(struct sindex));145 memcpy (new_index, array->empty_index, sizeof(struct sindex));144 new_index = (struct sindex *) objc_malloc (sizeof (struct sindex)); 145 memcpy (new_index, array->empty_index, sizeof (struct sindex)); 146 146 new_index->version.version = array->version.version; 147 147 *the_index = new_index; /* Prepared for install. */ … … 152 152 153 153 /* This index must be lazy copied */ 154 struct sindex *old_index = *the_index;155 new_index = (struct sindex *)objc_malloc(sizeof(struct sindex));156 memcpy ( new_index, old_index, sizeof(struct sindex));154 struct sindex *old_index = *the_index; 155 new_index = (struct sindex *) objc_malloc (sizeof (struct sindex)); 156 memcpy (new_index, old_index, sizeof (struct sindex)); 157 157 new_index->version.version = array->version.version; 158 158 *the_index = new_index; /* Prepared for install. */ … … 170 170 /* The bucket was previously empty (or something like that), */ 171 171 /* allocate a new. This is the effect of `lazy' allocation */ 172 new_bucket = (struct sbucket *)objc_malloc(sizeof(struct sbucket));173 memcpy ((void *) new_bucket, (const void*)array->empty_bucket,174 sizeof(struct sbucket));172 new_bucket = (struct sbucket *) objc_malloc (sizeof (struct sbucket)); 173 memcpy ((void *) new_bucket, (const void *) array->empty_bucket, 174 sizeof (struct sbucket)); 175 175 new_bucket->version.version = array->version.version; 176 176 *the_bucket = new_bucket; /* Prepared for install. */ … … 181 181 182 182 /* Perform lazy copy. */ 183 struct sbucket *old_bucket = *the_bucket;184 new_bucket = (struct sbucket *)objc_malloc(sizeof(struct sbucket));185 memcpy ( new_bucket, old_bucket, sizeof(struct sbucket));183 struct sbucket *old_bucket = *the_bucket; 184 new_bucket = (struct sbucket *) objc_malloc (sizeof (struct sbucket)); 185 memcpy (new_bucket, old_bucket, sizeof (struct sbucket)); 186 186 new_bucket->version.version = array->version.version; 187 187 *the_bucket = new_bucket; /* Prepared for install. */ … … 194 194 195 195 void 196 sarray_at_put_safe (struct sarray* array, sidx index, void*element)197 { 198 if (soffset_decode(index) >= array->capacity)199 sarray_realloc (array, soffset_decode(index)+1);200 sarray_at_put (array, index, element);201 } 202 203 struct sarray *204 sarray_new (int size, void *default_element)205 { 206 struct sarray *arr;207 #ifdef OBJC_SPARSE3 208 size_t num_indices = ((size -1)/(INDEX_CAPACITY))+1;209 struct sindex ** 210 #else /* OBJC_SPARSE2 */ 211 size_t num_indices = ((size -1)/BUCKET_SIZE)+1;212 struct sbucket ** 213 #endif 214 int counter;215 216 assert (size > 0);196 sarray_at_put_safe (struct sarray *array, sidx index, void *element) 197 { 198 if (soffset_decode (index) >= array->capacity) 199 sarray_realloc (array, soffset_decode (index) + 1); 200 sarray_at_put (array, index, element); 201 } 202 203 struct sarray * 204 sarray_new (int size, void *default_element) 205 { 206 struct sarray *arr; 207 #ifdef OBJC_SPARSE3 208 size_t num_indices = ((size - 1)/(INDEX_CAPACITY)) + 1; 209 struct sindex **new_indices; 210 #else /* OBJC_SPARSE2 */ 211 size_t num_indices = ((size - 1)/BUCKET_SIZE) + 1; 212 struct sbucket **new_buckets; 213 #endif 214 size_t counter; 215 216 assert (size > 0); 217 217 218 218 /* Allocate core array */ 219 arr = (struct sarray *) objc_malloc(sizeof(struct sarray));219 arr = (struct sarray *) objc_malloc (sizeof (struct sarray)); 220 220 arr->version.version = 0; 221 221 … … 223 223 #ifdef OBJC_SPARSE3 224 224 arr->capacity = num_indices*INDEX_CAPACITY; 225 new_indices = (struct sindex **)226 objc_malloc (sizeof(struct sindex*)*num_indices);227 228 arr->empty_index = (struct sindex *) objc_malloc(sizeof(struct sindex));225 new_indices = (struct sindex **) 226 objc_malloc (sizeof (struct sindex *) * num_indices); 227 228 arr->empty_index = (struct sindex *) objc_malloc (sizeof (struct sindex)); 229 229 arr->empty_index->version.version = 0; 230 230 … … 235 235 #else /* OBJC_SPARSE2 */ 236 236 arr->capacity = num_indices*BUCKET_SIZE; 237 new_buckets = (struct sbucket **)238 objc_malloc (sizeof(struct sbucket*)*num_indices);237 new_buckets = (struct sbucket **) 238 objc_malloc (sizeof (struct sbucket *) * num_indices); 239 239 240 240 narrays += 1; … … 243 243 #endif 244 244 245 arr->empty_bucket = (struct sbucket *) objc_malloc(sizeof(struct sbucket));245 arr->empty_bucket = (struct sbucket *) objc_malloc (sizeof (struct sbucket)); 246 246 arr->empty_bucket->version.version = 0; 247 247 … … 249 249 250 250 arr->ref_count = 1; 251 arr->is_copy_of = (struct sarray *)0;252 253 for (counter =0; counter<BUCKET_SIZE; counter++)251 arr->is_copy_of = (struct sarray *) 0; 252 253 for (counter = 0; counter < BUCKET_SIZE; counter++) 254 254 arr->empty_bucket->elems[counter] = default_element; 255 255 256 256 #ifdef OBJC_SPARSE3 257 for (counter =0; counter<INDEX_SIZE; counter++)257 for (counter = 0; counter < INDEX_SIZE; counter++) 258 258 arr->empty_index->buckets[counter] = arr->empty_bucket; 259 259 260 for (counter =0; counter<num_indices; counter++)260 for (counter = 0; counter < num_indices; counter++) 261 261 new_indices[counter] = arr->empty_index; 262 262 263 263 #else /* OBJC_SPARSE2 */ 264 264 265 for (counter =0; counter<num_indices; counter++)265 for (counter = 0; counter < num_indices; counter++) 266 266 new_buckets[counter] = arr->empty_bucket; 267 267 … … 284 284 285 285 void 286 sarray_realloc (struct sarray*array, int newsize)287 { 288 #ifdef OBJC_SPARSE3 289 size_t old_max_index = (array->capacity -1)/INDEX_CAPACITY;290 size_t new_max_index = ((newsize -1)/INDEX_CAPACITY);291 size_t rounded_size = (new_max_index +1)*INDEX_CAPACITY;292 293 struct sindex ** 294 struct sindex ** 295 296 #else /* OBJC_SPARSE2 */ 297 size_t old_max_index = (array->capacity -1)/BUCKET_SIZE;298 size_t new_max_index = ((newsize -1)/BUCKET_SIZE);299 size_t rounded_size = (new_max_index +1)*BUCKET_SIZE;300 301 struct sbucket ** 302 struct sbucket ** 303 304 #endif 305 306 int counter;307 308 assert (newsize > 0);286 sarray_realloc (struct sarray *array, int newsize) 287 { 288 #ifdef OBJC_SPARSE3 289 size_t old_max_index = (array->capacity - 1)/INDEX_CAPACITY; 290 size_t new_max_index = ((newsize - 1)/INDEX_CAPACITY); 291 size_t rounded_size = (new_max_index + 1) * INDEX_CAPACITY; 292 293 struct sindex **new_indices; 294 struct sindex **old_indices; 295 296 #else /* OBJC_SPARSE2 */ 297 size_t old_max_index = (array->capacity - 1)/BUCKET_SIZE; 298 size_t new_max_index = ((newsize - 1)/BUCKET_SIZE); 299 size_t rounded_size = (new_max_index + 1) * BUCKET_SIZE; 300 301 struct sbucket **new_buckets; 302 struct sbucket **old_buckets; 303 304 #endif 305 306 size_t counter; 307 308 assert (newsize > 0); 309 309 310 310 /* The size is the same, just ignore the request */ 311 if (rounded_size <= array->capacity)311 if (rounded_size <= array->capacity) 312 312 return; 313 313 314 assert (array->ref_count == 1); /* stop if lazy copied... */314 assert (array->ref_count == 1); /* stop if lazy copied... */ 315 315 316 316 /* We are asked to extend the array -- allocate new bucket table, */ 317 317 /* and insert empty_bucket in newly allocated places. */ 318 if (rounded_size > array->capacity)318 if (rounded_size > array->capacity) 319 319 { 320 320 321 321 #ifdef OBJC_SPARSE3 322 322 new_max_index += 4; 323 rounded_size = (new_max_index +1)*INDEX_CAPACITY;323 rounded_size = (new_max_index + 1) * INDEX_CAPACITY; 324 324 325 325 #else /* OBJC_SPARSE2 */ 326 326 new_max_index += 4; 327 rounded_size = (new_max_index +1)*BUCKET_SIZE;327 rounded_size = (new_max_index + 1) * BUCKET_SIZE; 328 328 #endif 329 329 … … 334 334 /* alloc to force re-read by any concurrent readers. */ 335 335 old_indices = array->indices; 336 new_indices = (struct sindex **)337 objc_malloc ((new_max_index+1)*sizeof(struct sindex*));336 new_indices = (struct sindex **) 337 objc_malloc ((new_max_index + 1) * sizeof (struct sindex *)); 338 338 #else /* OBJC_SPARSE2 */ 339 339 old_buckets = array->buckets; 340 new_buckets = (struct sbucket **)341 objc_malloc ((new_max_index+1)*sizeof(struct sbucket*));340 new_buckets = (struct sbucket **) 341 objc_malloc ((new_max_index + 1) * sizeof (struct sbucket *)); 342 342 #endif 343 343 344 344 /* copy buckets below old_max_index (they are still valid) */ 345 for (counter = 0; counter <= old_max_index; counter++ ) {345 for (counter = 0; counter <= old_max_index; counter++ ) { 346 346 #ifdef OBJC_SPARSE3 347 347 new_indices[counter] = old_indices[counter]; … … 353 353 #ifdef OBJC_SPARSE3 354 354 /* reset entries above old_max_index to empty_bucket */ 355 for (counter = old_max_index+1; counter <= new_max_index; counter++)355 for (counter = old_max_index + 1; counter <= new_max_index; counter++) 356 356 new_indices[counter] = array->empty_index; 357 357 #else /* OBJC_SPARSE2 */ 358 358 /* reset entries above old_max_index to empty_bucket */ 359 for (counter = old_max_index+1; counter <= new_max_index; counter++)359 for (counter = old_max_index + 1; counter <= new_max_index; counter++) 360 360 new_buckets[counter] = array->empty_bucket; 361 361 #endif … … 370 370 #ifdef OBJC_SPARSE3 371 371 /* free the old indices */ 372 sarray_free_garbage (old_indices);373 #else /* OBJC_SPARSE2 */ 374 sarray_free_garbage (old_buckets);372 sarray_free_garbage (old_indices); 373 #else /* OBJC_SPARSE2 */ 374 sarray_free_garbage (old_buckets); 375 375 #endif 376 376 … … 385 385 386 386 void 387 sarray_free(struct sarray* array) { 388 389 #ifdef OBJC_SPARSE3 390 size_t old_max_index = (array->capacity-1)/INDEX_CAPACITY; 391 struct sindex ** old_indices; 387 sarray_free (struct sarray *array) { 388 #ifdef OBJC_SPARSE3 389 size_t old_max_index = (array->capacity - 1)/INDEX_CAPACITY; 390 struct sindex **old_indices; 392 391 #else 393 size_t old_max_index = (array->capacity -1)/BUCKET_SIZE;394 struct sbucket ** 395 #endif 396 int counter = 0;397 398 assert (array->ref_count != 0); /* Freed multiple times!!! */399 400 if (--(array->ref_count) != 0) /* There exists copies of me */392 size_t old_max_index = (array->capacity - 1)/BUCKET_SIZE; 393 struct sbucket **old_buckets; 394 #endif 395 size_t counter = 0; 396 397 assert (array->ref_count != 0); /* Freed multiple times!!! */ 398 399 if (--(array->ref_count) != 0) /* There exists copies of me */ 401 400 return; 402 401 … … 407 406 #endif 408 407 409 if ((array->is_copy_of) && ((array->is_copy_of->ref_count - 1) == 0))410 sarray_free (array->is_copy_of);408 if ((array->is_copy_of) && ((array->is_copy_of->ref_count - 1) == 0)) 409 sarray_free (array->is_copy_of); 411 410 412 411 /* Free all entries that do not point to empty_bucket */ 413 for (counter = 0; counter <= old_max_index; counter++ ) {414 #ifdef OBJC_SPARSE3 415 struct sindex *idx = old_indices[counter];416 if ((idx != array->empty_index) &&412 for (counter = 0; counter <= old_max_index; counter++ ) { 413 #ifdef OBJC_SPARSE3 414 struct sindex *idx = old_indices[counter]; 415 if ((idx != array->empty_index) && 417 416 (idx->version.version == array->version.version)) { 418 417 int c2; 419 for (c2=0; c2<INDEX_SIZE; c2++) {420 struct sbucket *bkt = idx->buckets[c2];421 if ((bkt != array->empty_bucket) &&418 for (c2 = 0; c2 < INDEX_SIZE; c2++) { 419 struct sbucket *bkt = idx->buckets[c2]; 420 if ((bkt != array->empty_bucket) && 422 421 (bkt->version.version == array->version.version)) 423 422 { 424 sarray_free_garbage (bkt);423 sarray_free_garbage (bkt); 425 424 nbuckets -= 1; 426 425 } 427 426 } 428 sarray_free_garbage (idx);427 sarray_free_garbage (idx); 429 428 nindices -= 1; 430 429 } 431 430 #else /* OBJC_SPARSE2 */ 432 struct sbucket *bkt = array->buckets[counter];431 struct sbucket *bkt = array->buckets[counter]; 433 432 if ((bkt != array->empty_bucket) && 434 433 (bkt->version.version == array->version.version)) 435 434 { 436 sarray_free_garbage (bkt);435 sarray_free_garbage (bkt); 437 436 nbuckets -= 1; 438 437 } … … 442 441 #ifdef OBJC_SPARSE3 443 442 /* free empty_index */ 444 if (array->empty_index->version.version == array->version.version) {445 sarray_free_garbage (array->empty_index);443 if (array->empty_index->version.version == array->version.version) { 444 sarray_free_garbage (array->empty_index); 446 445 nindices -= 1; 447 446 } … … 449 448 450 449 /* free empty_bucket */ 451 if (array->empty_bucket->version.version == array->version.version) {452 sarray_free_garbage (array->empty_bucket);450 if (array->empty_bucket->version.version == array->version.version) { 451 sarray_free_garbage (array->empty_bucket); 453 452 nbuckets -= 1; 454 453 } 455 idxsize -= (old_max_index +1);454 idxsize -= (old_max_index + 1); 456 455 narrays -= 1; 457 456 458 457 #ifdef OBJC_SPARSE3 459 458 /* free bucket table */ 460 sarray_free_garbage (array->indices);459 sarray_free_garbage (array->indices); 461 460 462 461 #else 463 462 /* free bucket table */ 464 sarray_free_garbage (array->buckets);463 sarray_free_garbage (array->buckets); 465 464 466 465 #endif 467 466 468 467 /* free array */ 469 sarray_free_garbage (array);468 sarray_free_garbage (array); 470 469 } 471 470 … … 473 472 /* copied. */ 474 473 475 struct sarray *476 sarray_lazy_copy (struct sarray*oarr)477 { 478 struct sarray *arr;479 480 #ifdef OBJC_SPARSE3 481 size_t num_indices = ((oarr->capacity -1)/INDEX_CAPACITY)+1;482 struct sindex ** 483 #else /* OBJC_SPARSE2 */ 484 size_t num_indices = ((oarr->capacity -1)/BUCKET_SIZE)+1;485 struct sbucket ** 474 struct sarray * 475 sarray_lazy_copy (struct sarray *oarr) 476 { 477 struct sarray *arr; 478 479 #ifdef OBJC_SPARSE3 480 size_t num_indices = ((oarr->capacity - 1)/INDEX_CAPACITY) + 1; 481 struct sindex **new_indices; 482 #else /* OBJC_SPARSE2 */ 483 size_t num_indices = ((oarr->capacity - 1)/BUCKET_SIZE) + 1; 484 struct sbucket **new_buckets; 486 485 #endif 487 486 488 487 /* Allocate core array */ 489 arr = (struct sarray *) objc_malloc(sizeof(struct sarray)); /* !!! */488 arr = (struct sarray *) objc_malloc (sizeof (struct sarray)); /* !!! */ 490 489 arr->version.version = oarr->version.version + 1; 491 490 #ifdef OBJC_SPARSE3 … … 500 499 #ifdef OBJC_SPARSE3 501 500 /* Copy bucket table */ 502 new_indices = (struct sindex**) 503 objc_malloc(sizeof(struct sindex*)*num_indices); 504 memcpy( new_indices,oarr->indices, 505 sizeof(struct sindex*)*num_indices); 501 new_indices = (struct sindex **) 502 objc_malloc (sizeof (struct sindex *) * num_indices); 503 memcpy (new_indices, oarr->indices, sizeof (struct sindex *) * num_indices); 506 504 arr->indices = new_indices; 507 505 #else 508 506 /* Copy bucket table */ 509 new_buckets = (struct sbucket**) 510 objc_malloc(sizeof(struct sbucket*)*num_indices); 511 memcpy( new_buckets,oarr->buckets, 512 sizeof(struct sbucket*)*num_indices); 507 new_buckets = (struct sbucket **) 508 objc_malloc (sizeof (struct sbucket *) * num_indices); 509 memcpy (new_buckets, oarr->buckets, sizeof (struct sbucket *) * num_indices); 513 510 arr->buckets = new_buckets; 514 511 #endif -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.