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

信息检索与抽取

开发平台:

Unix_Linux

  1. /* Sparse Arrays for Objective C dispatch tables
  2.    Copyright (C) 1993, 1995, 1996 Free Software Foundation, Inc.
  3.    Contributed by Kresten Krab Thorup.
  4. This file is part of GNU CC.
  5. GNU CC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU CC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU CC; see the file COPYING.  If not, write to
  15. the Free Software Foundation, 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA.  */
  17. /* As a special exception, if you link this library with files
  18.    compiled with GCC to produce an executable, this does not cause
  19.    the resulting executable to be covered by the GNU General Public License.
  20.    This exception does not however invalidate any other reasons why
  21.    the executable file might be covered by the GNU General Public License.  */
  22. #ifndef __sarray_INCLUDE_GNU
  23. #define __sarray_INCLUDE_GNU
  24. #include <objc/externvar.h>
  25. #define OBJC_SPARSE2 /* 2-level sparse array */
  26. /* #define OBJC_SPARSE3 */      /* 3-level sparse array */
  27. #ifdef OBJC_SPARSE2
  28. externobjcvar const char* __objc_sparse2_id;
  29. #endif
  30. #ifdef OBJC_SPARSE3
  31. externobjcvar const char* __objc_sparse3_id;
  32. #endif
  33. #include <stddef.h>
  34. #include "objc/thr.h"
  35. externobjcvar int nbuckets; /* for stats */
  36. externobjcvar int nindices;
  37. externobjcvar int narrays;
  38. externobjcvar int idxsize;
  39. #include <assert.h>
  40. /* An unsigned integer of same size as a pointer */
  41. #define SIZET_BITS (sizeof(size_t)*8)
  42. #if defined(__sparc__) || defined(OBJC_SPARSE2)
  43. #define PRECOMPUTE_SELECTORS
  44. #endif
  45. #ifdef OBJC_SPARSE3
  46. /* Buckets are 8 words each */
  47. #define BUCKET_BITS 3
  48. #define BUCKET_SIZE (1<<BUCKET_BITS)
  49. #define BUCKET_MASK (BUCKET_SIZE-1)
  50. /* Indices are 16 words each */
  51. #define INDEX_BITS 4
  52. #define INDEX_SIZE (1<<INDEX_BITS)
  53. #define INDEX_MASK (INDEX_SIZE-1)
  54. #define INDEX_CAPACITY (BUCKET_SIZE*INDEX_SIZE)
  55. #else /* OBJC_SPARSE2 */
  56. /* Buckets are 32 words each */
  57. #define BUCKET_BITS 5
  58. #define BUCKET_SIZE (1<<BUCKET_BITS)
  59. #define BUCKET_MASK (BUCKET_SIZE-1)
  60. #endif /* OBJC_SPARSE2 */
  61. typedef size_t sidx;
  62. #ifdef PRECOMPUTE_SELECTORS
  63. struct soffset {
  64. #ifdef OBJC_SPARSE3
  65.   unsigned int unused : SIZET_BITS/4;
  66.   unsigned int eoffset : SIZET_BITS/4;
  67.   unsigned int boffset : SIZET_BITS/4;
  68.   unsigned int ioffset : SIZET_BITS/4;
  69. #else /* OBJC_SPARSE2 */
  70. #ifdef __sparc__
  71.   unsigned long boffset : (SIZET_BITS - 2) - BUCKET_BITS;
  72.   unsigned int eoffset : BUCKET_BITS;
  73.   unsigned int unused  : 2;
  74. #else
  75.   unsigned int boffset : SIZET_BITS/2;
  76.   unsigned int eoffset : SIZET_BITS/2;
  77. #endif
  78. #endif /* OBJC_SPARSE2 */
  79. };
  80. union sofftype {
  81.   struct soffset off;
  82.   sidx idx;
  83. };
  84. #endif /* not PRECOMPUTE_SELECTORS */
  85. union sversion {
  86.   int version;
  87.   void *next_free;
  88. };
  89. struct sbucket {
  90.   void* elems[BUCKET_SIZE]; /* elements stored in array */
  91.   union sversion version; /* used for copy-on-write */
  92. };
  93. #ifdef OBJC_SPARSE3
  94. struct sindex {
  95.   struct sbucket* buckets[INDEX_SIZE];
  96.   union sversion version; /* used for copy-on-write */
  97. };
  98. #endif /* OBJC_SPARSE3 */
  99. struct sarray {
  100. #ifdef OBJC_SPARSE3
  101.   struct sindex** indices;
  102.   struct sindex* empty_index;
  103. #else /* OBJC_SPARSE2 */
  104.   struct sbucket** buckets;
  105. #endif  /* OBJC_SPARSE2 */
  106.   struct sbucket* empty_bucket;
  107.   union sversion version; /* used for copy-on-write */
  108.   short ref_count;
  109.   struct sarray* is_copy_of;
  110.   size_t capacity;
  111. };
  112. struct sarray* sarray_new(int, void* default_element);
  113. void sarray_free(struct sarray*);
  114. struct sarray* sarray_lazy_copy(struct sarray*);
  115. void sarray_realloc(struct sarray*, int new_size);
  116. void sarray_at_put(struct sarray*, sidx index, void* elem);
  117. void sarray_at_put_safe(struct sarray*, sidx index, void* elem);
  118. struct sarray* sarray_hard_copy(struct sarray*); /* ... like the name? */
  119. void sarray_remove_garbage(void);
  120. #ifdef PRECOMPUTE_SELECTORS
  121. /* Transform soffset values to ints and vica verca */
  122. static inline unsigned int
  123. soffset_decode(sidx index)
  124. {
  125.   union sofftype x;
  126.   x.idx = index;
  127. #ifdef OBJC_SPARSE3
  128.   return x.off.eoffset
  129.     + (x.off.boffset*BUCKET_SIZE)
  130.       + (x.off.ioffset*INDEX_CAPACITY);
  131. #else /* OBJC_SPARSE2 */
  132.   return x.off.eoffset + (x.off.boffset*BUCKET_SIZE);
  133. #endif /* OBJC_SPARSE2 */
  134. }
  135. static inline sidx
  136. soffset_encode(size_t offset)
  137. {
  138.   union sofftype x;
  139.   x.off.eoffset = offset%BUCKET_SIZE;
  140. #ifdef OBJC_SPARSE3
  141.   x.off.boffset = (offset/BUCKET_SIZE)%INDEX_SIZE;
  142.   x.off.ioffset = offset/INDEX_CAPACITY;
  143. #else /* OBJC_SPARSE2 */
  144.   x.off.boffset = offset/BUCKET_SIZE;
  145. #endif
  146.   return (sidx)x.idx;
  147. }
  148. #else /* not PRECOMPUTE_SELECTORS */
  149. static inline size_t
  150. soffset_decode(sidx index)
  151. {
  152.   return index;
  153. }
  154. static inline sidx
  155. soffset_encode(size_t offset)
  156. {
  157.   return offset;
  158. }
  159. #endif /* not PRECOMPUTE_SELECTORS */
  160. /* Get element from the Sparse array `array' at offset `index' */
  161. static inline void* sarray_get(struct sarray* array, sidx index)
  162. {
  163. #ifdef PRECOMPUTE_SELECTORS
  164.   union sofftype x;
  165.   x.idx = index;
  166. #ifdef OBJC_SPARSE3
  167.   return 
  168.     array->
  169.       indices[x.off.ioffset]->
  170. buckets[x.off.boffset]->
  171.   elems[x.off.eoffset];
  172. #else /* OBJC_SPARSE2 */
  173.   return array->buckets[x.off.boffset]->elems[x.off.eoffset];
  174. #endif /* OBJC_SPARSE2 */
  175. #else /* not PRECOMPUTE_SELECTORS */
  176. #ifdef OBJC_SPARSE3
  177.   return array->
  178.     indices[index/INDEX_CAPACITY]->
  179.       buckets[(index/BUCKET_SIZE)%INDEX_SIZE]->
  180. elems[index%BUCKET_SIZE];
  181. #else /* OBJC_SPARSE2 */
  182.   return array->buckets[index/BUCKET_SIZE]->elems[index%BUCKET_SIZE];
  183. #endif /* not OBJC_SPARSE3 */
  184. #endif /* not PRECOMPUTE_SELECTORS */
  185. }
  186. static inline void* sarray_get_safe(struct sarray* array, sidx index)
  187. {
  188.   if(soffset_decode(index) < array->capacity)
  189.     return sarray_get(array, index);
  190.   else
  191.     return (array->empty_bucket->elems[0]);
  192. }
  193. #endif /* __sarray_INCLUDE_GNU */