sarray.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Sparse Arrays for Objective C dispatch tables
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. Contributed by Kresten Krab Thorup.
  4. This file is part of GCC.
  5. GCC 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 3, or (at your option)
  8. any later version.
  9. GCC 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. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #ifndef __sarray_INCLUDE_GNU
  21. #define __sarray_INCLUDE_GNU
  22. #define OBJC_SPARSE2 /* 2-level sparse array. */
  23. /* #define OBJC_SPARSE3 */ /* 3-level sparse array. */
  24. #ifdef OBJC_SPARSE2
  25. extern const char* __objc_sparse2_id;
  26. #endif
  27. #ifdef OBJC_SPARSE3
  28. extern const char* __objc_sparse3_id;
  29. #endif
  30. #include <stddef.h>
  31. extern int nbuckets; /* for stats */
  32. extern int nindices;
  33. extern int narrays;
  34. extern int idxsize;
  35. /* An unsigned integer of same size as a pointer. */
  36. #define SIZET_BITS (sizeof (size_t) * 8)
  37. #if defined (__sparc__) || defined (OBJC_SPARSE2)
  38. #define PRECOMPUTE_SELECTORS
  39. #endif
  40. #ifdef OBJC_SPARSE3
  41. /* Buckets are 8 words each. */
  42. #define BUCKET_BITS 3
  43. #define BUCKET_SIZE (1 << BUCKET_BITS)
  44. #define BUCKET_MASK (BUCKET_SIZE - 1)
  45. /* Indices are 16 words each. */
  46. #define INDEX_BITS 4
  47. #define INDEX_SIZE (1 << INDEX_BITS)
  48. #define INDEX_MASK (INDEX_SIZE - 1)
  49. #define INDEX_CAPACITY (BUCKET_SIZE * INDEX_SIZE)
  50. #else /* OBJC_SPARSE2 */
  51. /* Buckets are 32 words each. */
  52. #define BUCKET_BITS 5
  53. #define BUCKET_SIZE (1 << BUCKET_BITS)
  54. #define BUCKET_MASK (BUCKET_SIZE - 1)
  55. #endif /* OBJC_SPARSE2 */
  56. typedef size_t sidx;
  57. #ifdef PRECOMPUTE_SELECTORS
  58. struct soffset
  59. {
  60. #ifdef OBJC_SPARSE3
  61. unsigned int unused : SIZET_BITS / 4;
  62. unsigned int eoffset : SIZET_BITS / 4;
  63. unsigned int boffset : SIZET_BITS / 4;
  64. unsigned int ioffset : SIZET_BITS / 4;
  65. #else /* OBJC_SPARSE2 */
  66. #ifdef __sparc__
  67. unsigned long boffset : (SIZET_BITS - 2) - BUCKET_BITS;
  68. unsigned int eoffset : BUCKET_BITS;
  69. unsigned int unused : 2;
  70. #else
  71. unsigned int boffset : SIZET_BITS / 2;
  72. unsigned int eoffset : SIZET_BITS / 2;
  73. #endif
  74. #endif /* OBJC_SPARSE2 */
  75. };
  76. union sofftype
  77. {
  78. struct soffset off;
  79. sidx idx;
  80. };
  81. #endif /* not PRECOMPUTE_SELECTORS */
  82. union sversion
  83. {
  84. int version;
  85. void *next_free;
  86. };
  87. struct sbucket
  88. {
  89. /* Elements stored in array. */
  90. void* elems[BUCKET_SIZE];
  91. /* Used for copy-on-write. */
  92. union sversion version;
  93. };
  94. #ifdef OBJC_SPARSE3
  95. struct sindex
  96. {
  97. struct sbucket* buckets[INDEX_SIZE];
  98. /* Used for copy-on-write. */
  99. union sversion version;
  100. };
  101. #endif /* OBJC_SPARSE3 */
  102. struct sarray
  103. {
  104. #ifdef OBJC_SPARSE3
  105. struct sindex** indices;
  106. struct sindex* empty_index;
  107. #else /* OBJC_SPARSE2 */
  108. struct sbucket** buckets;
  109. #endif /* OBJC_SPARSE2 */
  110. struct sbucket* empty_bucket;
  111. /* Used for copy-on-write. */
  112. union sversion version;
  113. short ref_count;
  114. struct sarray* is_copy_of;
  115. size_t capacity;
  116. };
  117. struct sarray* sarray_new (int, void* default_element);
  118. void sarray_free (struct sarray*);
  119. struct sarray* sarray_lazy_copy (struct sarray*);
  120. void sarray_realloc (struct sarray*, int new_size);
  121. void sarray_at_put (struct sarray*, sidx indx, void* elem);
  122. void sarray_at_put_safe (struct sarray*, sidx indx, void* elem);
  123. struct sarray* sarray_hard_copy (struct sarray*); /* ... like the name ? */
  124. void sarray_remove_garbage (void);
  125. #ifdef PRECOMPUTE_SELECTORS
  126. /* Transform soffset values to ints and vice versa. */
  127. static inline unsigned int
  128. soffset_decode (sidx indx)
  129. {
  130. union sofftype x;
  131. x.idx = indx;
  132. #ifdef OBJC_SPARSE3
  133. return x.off.eoffset
  134. + (x.off.boffset * BUCKET_SIZE)
  135. + (x.off.ioffset * INDEX_CAPACITY);
  136. #else /* OBJC_SPARSE2 */
  137. return x.off.eoffset + (x.off.boffset * BUCKET_SIZE);
  138. #endif /* OBJC_SPARSE2 */
  139. }
  140. static inline sidx
  141. soffset_encode (size_t offset)
  142. {
  143. union sofftype x;
  144. x.off.eoffset = offset % BUCKET_SIZE;
  145. #ifdef OBJC_SPARSE3
  146. x.off.boffset = (offset / BUCKET_SIZE) % INDEX_SIZE;
  147. x.off.ioffset = offset / INDEX_CAPACITY;
  148. #else /* OBJC_SPARSE2 */
  149. x.off.boffset = offset / BUCKET_SIZE;
  150. #endif
  151. return (sidx)x.idx;
  152. }
  153. #else /* not PRECOMPUTE_SELECTORS */
  154. static inline size_t
  155. soffset_decode (sidx indx)
  156. {
  157. return indx;
  158. }
  159. static inline sidx
  160. soffset_encode (size_t offset)
  161. {
  162. return offset;
  163. }
  164. #endif /* not PRECOMPUTE_SELECTORS */
  165. /* Get element from the Sparse array `array' at offset `indx'. */
  166. static inline void* sarray_get (struct sarray* array, sidx indx)
  167. {
  168. #ifdef PRECOMPUTE_SELECTORS
  169. union sofftype x;
  170. x.idx = indx;
  171. #ifdef OBJC_SPARSE3
  172. return array->
  173. indices[x.off.ioffset]->
  174. buckets[x.off.boffset]->
  175. elems[x.off.eoffset];
  176. #else /* OBJC_SPARSE2 */
  177. return array->buckets[x.off.boffset]->elems[x.off.eoffset];
  178. #endif /* OBJC_SPARSE2 */
  179. #else /* not PRECOMPUTE_SELECTORS */
  180. #ifdef OBJC_SPARSE3
  181. return array->
  182. indices[indx / INDEX_CAPACITY]->
  183. buckets[(indx / BUCKET_SIZE) % INDEX_SIZE]->
  184. elems[indx % BUCKET_SIZE];
  185. #else /* OBJC_SPARSE2 */
  186. return array->buckets[indx / BUCKET_SIZE]->elems[indx % BUCKET_SIZE];
  187. #endif /* not OBJC_SPARSE3 */
  188. #endif /* not PRECOMPUTE_SELECTORS */
  189. }
  190. static inline void* sarray_get_safe (struct sarray* array, sidx indx)
  191. {
  192. if (soffset_decode (indx) < array->capacity)
  193. return sarray_get (array, indx);
  194. else
  195. return (array->empty_bucket->elems[0]);
  196. }
  197. #endif /* __sarray_INCLUDE_GNU */