sarray.c
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:14k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

  1. /* Sparse Arrays for Objective C dispatch tables
  2.    Copyright (C) 1993, 1995, 1996 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU CC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU CC; see the file COPYING.  If not, write to
  14. the Free Software Foundation, 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.  */
  16. /* As a special exception, if you link this library with files
  17.    compiled with GCC to produce an executable, this does not cause
  18.    the resulting executable to be covered by the GNU General Public License.
  19.    This exception does not however invalidate any other reasons why
  20.    the executable file might be covered by the GNU General Public License.  */
  21. #include "sarray.h"
  22. #include "runtime.h"
  23. #include <stdio.h>
  24. #include "assert.h"
  25. int nbuckets = 0; /* !T:MUTEX */
  26. int nindices = 0; /* !T:MUTEX */
  27. int narrays = 0; /* !T:MUTEX */
  28. int idxsize = 0; /* !T:MUTEX */
  29. static void * first_free_data = NULL; /* !T:MUTEX */
  30. #ifdef OBJC_SPARSE2
  31. externobjcvardef const char* __objc_sparse2_id = "2 level sparse indices";
  32. #endif
  33. #ifdef OBJC_SPARSE3
  34. externobjcvardef const char* __objc_sparse3_id = "3 level sparse indices";
  35. #endif
  36. #ifdef __alpha__
  37. const void *memcpy (void*, const void*, size_t);
  38. #endif
  39. /* This function removes any structures left over from free operations
  40.    that were not safe in a multi-threaded environment. */
  41. void
  42. sarray_remove_garbage(void)
  43. {
  44.   void **vp;
  45.   void *np;
  46.   
  47.   objc_mutex_lock(__objc_runtime_mutex);
  48.   vp = first_free_data;
  49.   first_free_data = NULL;
  50.   while (vp) {
  51.     np = *vp;
  52.     objc_free(vp);
  53.     vp = np;
  54.   }
  55.   
  56.   objc_mutex_unlock(__objc_runtime_mutex);
  57. }
  58. /* Free a block of dynamically allocated memory.  If we are in multi-threaded
  59.    mode, it is ok to free it.  If not, we add it to the garbage heap to be
  60.    freed later. */
  61. static void
  62. sarray_free_garbage(void *vp)
  63. {
  64.   objc_mutex_lock(__objc_runtime_mutex);
  65.   
  66.   if (__objc_runtime_threads_alive == 1) {
  67.     objc_free(vp);
  68.     if (first_free_data)
  69.       sarray_remove_garbage();
  70.   }
  71.   else {
  72.     *(void **)vp = first_free_data;
  73.     first_free_data = vp;
  74.   }
  75.       
  76.   objc_mutex_unlock(__objc_runtime_mutex);
  77. }
  78. /* sarray_at_put : copies data in such a way as to be thread reader safe. */
  79. void
  80. sarray_at_put(struct sarray* array, sidx index, void* element)
  81. {
  82. #ifdef OBJC_SPARSE3
  83.   struct sindex** the_index;
  84.   struct sindex*  new_index;
  85. #endif
  86.   struct sbucket** the_bucket;
  87.   struct sbucket*  new_bucket;
  88. #ifdef OBJC_SPARSE3
  89.   size_t ioffset;
  90. #endif
  91.   size_t boffset;
  92.   size_t eoffset;
  93. #ifdef PRECOMPUTE_SELECTORS
  94.   union sofftype xx; 
  95.   xx.idx = index;
  96. #ifdef OBJC_SPARSE3
  97.   ioffset = xx.off.ioffset;
  98. #endif
  99.   boffset = xx.off.boffset;
  100.   eoffset = xx.off.eoffset;
  101. #else /* not PRECOMPUTE_SELECTORS */
  102. #ifdef OBJC_SPARSE3
  103.   ioffset = index/INDEX_CAPACITY;
  104.   boffset = (index/BUCKET_SIZE)%INDEX_SIZE;
  105.   eoffset = index%BUCKET_SIZE;
  106. #else
  107.   boffset = index/BUCKET_SIZE;
  108.   eoffset = index%BUCKET_SIZE;
  109. #endif
  110. #endif /* not PRECOMPUTE_SELECTORS */
  111.   assert(soffset_decode(index) < array->capacity); /* Range check */
  112. #ifdef OBJC_SPARSE3
  113.   the_index = &(array->indices[ioffset]);
  114.   the_bucket = &((*the_index)->buckets[boffset]);
  115. #else
  116.   the_bucket = &(array->buckets[boffset]);
  117. #endif
  118.   
  119.   if ((*the_bucket)->elems[eoffset] == element)
  120.     return; /* great! we just avoided a lazy copy */
  121. #ifdef OBJC_SPARSE3
  122.   /* First, perform lazy copy/allocation of index if needed */
  123.   if ((*the_index) == array->empty_index) {
  124.     /* The index was previously empty, allocate a new */
  125.     new_index = (struct sindex*)objc_malloc(sizeof(struct sindex));
  126.     memcpy(new_index, array->empty_index, sizeof(struct sindex));
  127.     new_index->version.version = array->version.version;
  128.     *the_index = new_index;                     /* Prepared for install. */
  129.     the_bucket = &((*the_index)->buckets[boffset]);
  130.     
  131.     nindices += 1;
  132.   } else if ((*the_index)->version.version != array->version.version) {
  133.     /* This index must be lazy copied */
  134.     struct sindex* old_index = *the_index;
  135.     new_index = (struct sindex*)objc_malloc(sizeof(struct sindex));
  136.     memcpy( new_index, old_index, sizeof(struct sindex));
  137.     new_index->version.version = array->version.version;
  138.     *the_index = new_index;                     /* Prepared for install. */
  139.     the_bucket = &((*the_index)->buckets[boffset]);
  140.     
  141.     nindices += 1;
  142.   }
  143. #endif /* OBJC_SPARSE3 */
  144.   /* next, perform lazy allocation/copy of the bucket if needed */
  145.   if ((*the_bucket) == array->empty_bucket) {
  146.     /* The bucket was previously empty (or something like that), */
  147.     /* allocate a new.  This is the effect of `lazy' allocation */  
  148.     new_bucket = (struct sbucket*)objc_malloc(sizeof(struct sbucket));
  149.     memcpy((void *) new_bucket, (const void*)array->empty_bucket, 
  150.    sizeof(struct sbucket));
  151.     new_bucket->version.version = array->version.version;
  152.     *the_bucket = new_bucket;                   /* Prepared for install. */
  153.     
  154.     nbuckets += 1;
  155.   } else if ((*the_bucket)->version.version != array->version.version) {
  156.     /* Perform lazy copy. */
  157.     struct sbucket* old_bucket = *the_bucket;
  158.     new_bucket = (struct sbucket*)objc_malloc(sizeof(struct sbucket));
  159.     memcpy( new_bucket, old_bucket, sizeof(struct sbucket));
  160.     new_bucket->version.version = array->version.version;
  161.     *the_bucket = new_bucket;                   /* Prepared for install. */
  162.     
  163.     nbuckets += 1;
  164.   }
  165.   (*the_bucket)->elems[eoffset] = element;
  166. }
  167. void
  168. sarray_at_put_safe(struct sarray* array, sidx index, void* element)
  169. {
  170.   if(soffset_decode(index) >= array->capacity)
  171.     sarray_realloc(array, soffset_decode(index)+1);
  172.   sarray_at_put(array, index, element);
  173. }
  174. struct sarray* 
  175. sarray_new (int size, void* default_element)
  176. {
  177.   struct sarray* arr;
  178. #ifdef OBJC_SPARSE3
  179.   size_t num_indices = ((size-1)/(INDEX_CAPACITY))+1;
  180.   struct sindex ** new_indices;
  181. #else /* OBJC_SPARSE2 */
  182.   size_t num_indices = ((size-1)/BUCKET_SIZE)+1;
  183.   struct sbucket ** new_buckets;
  184. #endif
  185.   int counter;
  186.   assert(size > 0);
  187.   /* Allocate core array */
  188.   arr = (struct sarray*) objc_malloc(sizeof(struct sarray));
  189.   arr->version.version = 0;
  190.   
  191.   /* Initialize members */
  192. #ifdef OBJC_SPARSE3
  193.   arr->capacity = num_indices*INDEX_CAPACITY;
  194.   new_indices = (struct sindex**) 
  195.     objc_malloc(sizeof(struct sindex*)*num_indices);
  196.   arr->empty_index = (struct sindex*) objc_malloc(sizeof(struct sindex));
  197.   arr->empty_index->version.version = 0;
  198.   
  199.   narrays  += 1;
  200.   idxsize  += num_indices;
  201.   nindices += 1;
  202. #else /* OBJC_SPARSE2 */
  203.   arr->capacity = num_indices*BUCKET_SIZE;
  204.   new_buckets = (struct sbucket**) 
  205.     objc_malloc(sizeof(struct sbucket*)*num_indices);
  206.   
  207.   narrays  += 1;
  208.   idxsize  += num_indices;
  209. #endif
  210.   arr->empty_bucket = (struct sbucket*) objc_malloc(sizeof(struct sbucket));
  211.   arr->empty_bucket->version.version = 0;
  212.   
  213.   nbuckets += 1;
  214.   arr->ref_count = 1;
  215.   arr->is_copy_of = (struct sarray*)0;
  216.   
  217.   for (counter=0; counter<BUCKET_SIZE; counter++)
  218.     arr->empty_bucket->elems[counter] = default_element;
  219. #ifdef OBJC_SPARSE3
  220.   for (counter=0; counter<INDEX_SIZE; counter++)
  221.     arr->empty_index->buckets[counter] = arr->empty_bucket;
  222.   for (counter=0; counter<num_indices; counter++)
  223.     new_indices[counter] = arr->empty_index;
  224. #else /* OBJC_SPARSE2 */
  225.   for (counter=0; counter<num_indices; counter++)
  226.     new_buckets[counter] = arr->empty_bucket;
  227. #endif
  228.   
  229. #ifdef OBJC_SPARSE3
  230.   arr->indices = new_indices;
  231. #else /* OBJC_SPARSE2 */
  232.   arr->buckets = new_buckets;
  233. #endif
  234.   
  235.   return arr;
  236. }
  237. /* Reallocate the sparse array to hold `newsize' entries
  238.    Note: We really allocate and then free.  We have to do this to ensure that
  239.    any concurrent readers notice the update. */
  240. void 
  241. sarray_realloc(struct sarray* array, int newsize)
  242. {
  243. #ifdef OBJC_SPARSE3
  244.   size_t old_max_index = (array->capacity-1)/INDEX_CAPACITY;
  245.   size_t new_max_index = ((newsize-1)/INDEX_CAPACITY);
  246.   size_t rounded_size = (new_max_index+1)*INDEX_CAPACITY;
  247.   struct sindex ** new_indices;
  248.   struct sindex ** old_indices;
  249.   
  250. #else /* OBJC_SPARSE2 */
  251.   size_t old_max_index = (array->capacity-1)/BUCKET_SIZE;
  252.   size_t new_max_index = ((newsize-1)/BUCKET_SIZE);
  253.   size_t rounded_size = (new_max_index+1)*BUCKET_SIZE;
  254.   struct sbucket ** new_buckets;
  255.   struct sbucket ** old_buckets;
  256.   
  257. #endif
  258.   int counter;
  259.   assert(newsize > 0);
  260.   /* The size is the same, just ignore the request */
  261.   if(rounded_size <= array->capacity)
  262.     return;
  263.   assert(array->ref_count == 1); /* stop if lazy copied... */
  264.   /* We are asked to extend the array -- allocate new bucket table, */
  265.   /* and insert empty_bucket in newly allocated places. */
  266.   if(rounded_size > array->capacity) 
  267.     {
  268. #ifdef OBJC_SPARSE3
  269.       new_max_index += 4;
  270.       rounded_size = (new_max_index+1)*INDEX_CAPACITY;
  271.       
  272. #else /* OBJC_SPARSE2 */
  273.       new_max_index += 4;
  274.       rounded_size = (new_max_index+1)*BUCKET_SIZE;
  275. #endif
  276.       
  277.       /* update capacity */
  278.       array->capacity = rounded_size;
  279. #ifdef OBJC_SPARSE3
  280.       /* alloc to force re-read by any concurrent readers. */
  281.       old_indices = array->indices;
  282.       new_indices = (struct sindex**)
  283. objc_malloc((new_max_index+1)*sizeof(struct sindex*));
  284. #else /* OBJC_SPARSE2 */
  285.       old_buckets = array->buckets;
  286.       new_buckets = (struct sbucket**)
  287. objc_malloc((new_max_index+1)*sizeof(struct sbucket*));
  288. #endif
  289.       /* copy buckets below old_max_index (they are still valid) */
  290.       for(counter = 0; counter <= old_max_index; counter++ ) {
  291. #ifdef OBJC_SPARSE3
  292. new_indices[counter] = old_indices[counter];
  293. #else /* OBJC_SPARSE2 */
  294. new_buckets[counter] = old_buckets[counter];
  295. #endif
  296.       }
  297. #ifdef OBJC_SPARSE3
  298.       /* reset entries above old_max_index to empty_bucket */
  299.       for(counter = old_max_index+1; counter <= new_max_index; counter++)
  300. new_indices[counter] = array->empty_index;
  301. #else /* OBJC_SPARSE2 */
  302.       /* reset entries above old_max_index to empty_bucket */
  303.       for(counter = old_max_index+1; counter <= new_max_index; counter++)
  304. new_buckets[counter] = array->empty_bucket;
  305. #endif
  306.       
  307. #ifdef OBJC_SPARSE3
  308.       /* install the new indices */
  309.       array->indices = new_indices;
  310. #else /* OBJC_SPARSE2 */
  311.       array->buckets = new_buckets;
  312. #endif
  313. #ifdef OBJC_SPARSE3
  314.       /* free the old indices */
  315.       sarray_free_garbage(old_indices);
  316. #else /* OBJC_SPARSE2 */
  317.       sarray_free_garbage(old_buckets);
  318. #endif
  319.       
  320.       idxsize += (new_max_index-old_max_index);
  321.       return;
  322.     }
  323. }
  324. /* Free a sparse array allocated with sarray_new */
  325. void 
  326. sarray_free(struct sarray* array) {
  327. #ifdef OBJC_SPARSE3
  328.   size_t old_max_index = (array->capacity-1)/INDEX_CAPACITY;
  329.   struct sindex ** old_indices;
  330. #else
  331.   size_t old_max_index = (array->capacity-1)/BUCKET_SIZE;
  332.   struct sbucket ** old_buckets;
  333. #endif
  334.   int counter = 0;
  335.   assert(array->ref_count != 0); /* Freed multiple times!!! */
  336.   if(--(array->ref_count) != 0) /* There exists copies of me */
  337.     return;
  338. #ifdef OBJC_SPARSE3
  339.   old_indices = array->indices;
  340. #else
  341.   old_buckets = array->buckets;
  342. #endif
  343.   
  344.   if((array->is_copy_of) && ((array->is_copy_of->ref_count - 1) == 0))
  345.     sarray_free(array->is_copy_of);
  346.   /* Free all entries that do not point to empty_bucket */
  347.   for(counter = 0; counter <= old_max_index; counter++ ) {
  348. #ifdef OBJC_SPARSE3
  349.     struct sindex* idx = old_indices[counter];
  350.     if((idx != array->empty_index) &&
  351.        (idx->version.version == array->version.version)) {
  352.       int c2; 
  353.       for(c2=0; c2<INDEX_SIZE; c2++) {
  354. struct sbucket* bkt = idx->buckets[c2];
  355. if((bkt != array->empty_bucket) &&
  356.    (bkt->version.version == array->version.version))
  357.   {
  358.     sarray_free_garbage(bkt);
  359.     nbuckets -= 1;
  360.   }
  361.       }
  362.       sarray_free_garbage(idx);
  363.       nindices -= 1;
  364.     }
  365. #else /* OBJC_SPARSE2 */
  366.     struct sbucket* bkt = array->buckets[counter];
  367.     if ((bkt != array->empty_bucket) &&
  368. (bkt->version.version == array->version.version))
  369.       {
  370. sarray_free_garbage(bkt);
  371. nbuckets -= 1;
  372.       }
  373. #endif
  374.   }
  375. #ifdef OBJC_SPARSE3  
  376.   /* free empty_index */
  377.   if(array->empty_index->version.version == array->version.version) {
  378.     sarray_free_garbage(array->empty_index);
  379.     nindices -= 1;
  380.   }
  381. #endif
  382.   /* free empty_bucket */
  383.   if(array->empty_bucket->version.version == array->version.version) {
  384.     sarray_free_garbage(array->empty_bucket);
  385.     nbuckets -= 1;
  386.   }
  387.   idxsize -= (old_max_index+1);
  388.   narrays -= 1;
  389. #ifdef OBJC_SPARSE3
  390.   /* free bucket table */
  391.   sarray_free_garbage(array->indices);
  392. #else
  393.   /* free bucket table */
  394.   sarray_free_garbage(array->buckets);
  395. #endif
  396.   
  397.   /* free array */
  398.   sarray_free_garbage(array);
  399. }
  400. /* This is a lazy copy.  Only the core of the structure is actually */
  401. /* copied.   */
  402. struct sarray* 
  403. sarray_lazy_copy(struct sarray* oarr)
  404. {
  405.   struct sarray* arr;
  406. #ifdef OBJC_SPARSE3
  407.   size_t num_indices = ((oarr->capacity-1)/INDEX_CAPACITY)+1;
  408.   struct sindex ** new_indices;
  409. #else /* OBJC_SPARSE2 */
  410.   size_t num_indices = ((oarr->capacity-1)/BUCKET_SIZE)+1;
  411.   struct sbucket ** new_buckets;
  412. #endif
  413.   /* Allocate core array */
  414.   arr = (struct sarray*) objc_malloc(sizeof(struct sarray)); /* !!! */
  415.   arr->version.version = oarr->version.version + 1;
  416. #ifdef OBJC_SPARSE3
  417.   arr->empty_index = oarr->empty_index;
  418. #endif
  419.   arr->empty_bucket = oarr->empty_bucket;
  420.   arr->ref_count = 1;
  421.   oarr->ref_count += 1;
  422.   arr->is_copy_of = oarr;
  423.   arr->capacity = oarr->capacity;
  424.   
  425. #ifdef OBJC_SPARSE3
  426.   /* Copy bucket table */
  427.   new_indices = (struct sindex**) 
  428.     objc_malloc(sizeof(struct sindex*)*num_indices);
  429.   memcpy( new_indices,oarr->indices, 
  430. sizeof(struct sindex*)*num_indices);
  431.   arr->indices = new_indices;
  432. #else 
  433.   /* Copy bucket table */
  434.   new_buckets = (struct sbucket**) 
  435.     objc_malloc(sizeof(struct sbucket*)*num_indices);
  436.   memcpy( new_buckets,oarr->buckets, 
  437. sizeof(struct sbucket*)*num_indices);
  438.   arr->buckets = new_buckets;
  439. #endif
  440.   idxsize += num_indices;
  441.   narrays += 1;
  442.   
  443.   return arr;
  444. }