sarray.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* Sparse Arrays for Objective C dispatch tables
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC 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 3, or (at your option)
  7. any later version.
  8. GCC 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. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "objc-private/common.h"
  20. #include "objc-private/sarray.h"
  21. #include "objc/runtime.h" /* For objc_malloc */
  22. #include "objc/thr.h" /* For objc_mutex_lock */
  23. #include "objc-private/module-abi-8.h"
  24. #include "objc-private/runtime.h"
  25. #include <stdio.h>
  26. #include <string.h> /* For memset */
  27. #include <assert.h> /* For assert */
  28. int nbuckets = 0; /* !T:MUTEX */
  29. int nindices = 0; /* !T:MUTEX */
  30. int narrays = 0; /* !T:MUTEX */
  31. int idxsize = 0; /* !T:MUTEX */
  32. static void *first_free_data = NULL; /* !T:MUTEX */
  33. #ifdef OBJC_SPARSE2
  34. const char *__objc_sparse2_id = "2 level sparse indices";
  35. #endif
  36. #ifdef OBJC_SPARSE3
  37. const char *__objc_sparse3_id = "3 level sparse indices";
  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. objc_mutex_lock (__objc_runtime_mutex);
  47. vp = first_free_data;
  48. first_free_data = NULL;
  49. while (vp)
  50. {
  51. np = *vp;
  52. objc_free (vp);
  53. vp = np;
  54. }
  55. objc_mutex_unlock (__objc_runtime_mutex);
  56. }
  57. /* Free a block of dynamically allocated memory. If we are in
  58. multi-threaded mode, it is ok to free it. If not, we add it to the
  59. garbage heap to be freed later. */
  60. static void
  61. sarray_free_garbage (void *vp)
  62. {
  63. objc_mutex_lock (__objc_runtime_mutex);
  64. if (__objc_runtime_threads_alive == 1)
  65. {
  66. objc_free (vp);
  67. if (first_free_data)
  68. sarray_remove_garbage ();
  69. }
  70. else
  71. {
  72. *(void **)vp = first_free_data;
  73. first_free_data = vp;
  74. }
  75. objc_mutex_unlock (__objc_runtime_mutex);
  76. }
  77. /* sarray_at_put copies data in such a way as to be thread reader
  78. 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. if ((*the_bucket)->elems[eoffset] == element)
  119. return; /* Great! we just avoided a lazy copy. */
  120. #ifdef OBJC_SPARSE3
  121. /* First, perform lazy copy/allocation of index if needed. */
  122. if ((*the_index) == array->empty_index)
  123. {
  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. nindices += 1;
  131. }
  132. else if ((*the_index)->version.version != array->version.version)
  133. {
  134. /* This index must be lazy copied. */
  135. struct sindex *old_index = *the_index;
  136. new_index = (struct sindex *) objc_malloc (sizeof (struct sindex));
  137. memcpy (new_index, old_index, sizeof (struct sindex));
  138. new_index->version.version = array->version.version;
  139. *the_index = new_index; /* Prepared for install. */
  140. the_bucket = &((*the_index)->buckets[boffset]);
  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. {
  147. /* The bucket was previously empty (or something like that),
  148. allocate a new. This is the effect of `lazy' allocation. */
  149. new_bucket = (struct sbucket *) objc_malloc (sizeof (struct sbucket));
  150. memcpy ((void *) new_bucket, (const void *) array->empty_bucket,
  151. sizeof (struct sbucket));
  152. new_bucket->version.version = array->version.version;
  153. *the_bucket = new_bucket; /* Prepared for install. */
  154. nbuckets += 1;
  155. }
  156. else if ((*the_bucket)->version.version != array->version.version)
  157. {
  158. /* Perform lazy copy. */
  159. struct sbucket *old_bucket = *the_bucket;
  160. new_bucket = (struct sbucket *) objc_malloc (sizeof (struct sbucket));
  161. memcpy (new_bucket, old_bucket, sizeof (struct sbucket));
  162. new_bucket->version.version = array->version.version;
  163. *the_bucket = new_bucket; /* Prepared for install. */
  164. nbuckets += 1;
  165. }
  166. (*the_bucket)->elems[eoffset] = element;
  167. }
  168. void
  169. sarray_at_put_safe (struct sarray *array, sidx index, void *element)
  170. {
  171. if (soffset_decode (index) >= array->capacity)
  172. sarray_realloc (array, soffset_decode (index) + 1);
  173. sarray_at_put (array, index, element);
  174. }
  175. struct sarray *
  176. sarray_new (int size, void *default_element)
  177. {
  178. struct sarray *arr;
  179. #ifdef OBJC_SPARSE3
  180. size_t num_indices = ((size - 1)/(INDEX_CAPACITY)) + 1;
  181. struct sindex **new_indices;
  182. #else /* OBJC_SPARSE2 */
  183. size_t num_indices = ((size - 1)/BUCKET_SIZE) + 1;
  184. struct sbucket **new_buckets;
  185. #endif
  186. size_t counter;
  187. assert (size > 0);
  188. /* Allocate core array. */
  189. arr = (struct sarray *) objc_malloc (sizeof (struct sarray));
  190. arr->version.version = 0;
  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. narrays += 1;
  199. idxsize += num_indices;
  200. nindices += 1;
  201. #else /* OBJC_SPARSE2 */
  202. arr->capacity = num_indices*BUCKET_SIZE;
  203. new_buckets = (struct sbucket **)
  204. objc_malloc (sizeof (struct sbucket *) * num_indices);
  205. narrays += 1;
  206. idxsize += num_indices;
  207. #endif
  208. arr->empty_bucket = (struct sbucket *) objc_malloc (sizeof (struct sbucket));
  209. arr->empty_bucket->version.version = 0;
  210. nbuckets += 1;
  211. arr->ref_count = 1;
  212. arr->is_copy_of = (struct sarray *) 0;
  213. for (counter = 0; counter < BUCKET_SIZE; counter++)
  214. arr->empty_bucket->elems[counter] = default_element;
  215. #ifdef OBJC_SPARSE3
  216. for (counter = 0; counter < INDEX_SIZE; counter++)
  217. arr->empty_index->buckets[counter] = arr->empty_bucket;
  218. for (counter = 0; counter < num_indices; counter++)
  219. new_indices[counter] = arr->empty_index;
  220. #else /* OBJC_SPARSE2 */
  221. for (counter = 0; counter < num_indices; counter++)
  222. new_buckets[counter] = arr->empty_bucket;
  223. #endif
  224. #ifdef OBJC_SPARSE3
  225. arr->indices = new_indices;
  226. #else /* OBJC_SPARSE2 */
  227. arr->buckets = new_buckets;
  228. #endif
  229. return arr;
  230. }
  231. /* Reallocate the sparse array to hold `newsize' entries Note: We
  232. really allocate and then free. We have to do this to ensure that
  233. any concurrent readers notice the update. */
  234. void
  235. sarray_realloc (struct sarray *array, int newsize)
  236. {
  237. #ifdef OBJC_SPARSE3
  238. size_t old_max_index = (array->capacity - 1)/INDEX_CAPACITY;
  239. size_t new_max_index = ((newsize - 1)/INDEX_CAPACITY);
  240. size_t rounded_size = (new_max_index + 1) * INDEX_CAPACITY;
  241. struct sindex **new_indices;
  242. struct sindex **old_indices;
  243. #else /* OBJC_SPARSE2 */
  244. size_t old_max_index = (array->capacity - 1)/BUCKET_SIZE;
  245. size_t new_max_index = ((newsize - 1)/BUCKET_SIZE);
  246. size_t rounded_size = (new_max_index + 1) * BUCKET_SIZE;
  247. struct sbucket **new_buckets;
  248. struct sbucket **old_buckets;
  249. #endif
  250. size_t counter;
  251. assert (newsize > 0);
  252. /* The size is the same, just ignore the request. */
  253. if (rounded_size <= array->capacity)
  254. return;
  255. assert (array->ref_count == 1); /* stop if lazy copied... */
  256. /* We are asked to extend the array -- allocate new bucket table,
  257. and insert empty_bucket in newly allocated places. */
  258. if (rounded_size > array->capacity)
  259. {
  260. #ifdef OBJC_SPARSE3
  261. new_max_index += 4;
  262. rounded_size = (new_max_index + 1) * INDEX_CAPACITY;
  263. #else /* OBJC_SPARSE2 */
  264. new_max_index += 4;
  265. rounded_size = (new_max_index + 1) * BUCKET_SIZE;
  266. #endif
  267. /* Update capacity. */
  268. array->capacity = rounded_size;
  269. #ifdef OBJC_SPARSE3
  270. /* Alloc to force re-read by any concurrent readers. */
  271. old_indices = array->indices;
  272. new_indices = (struct sindex **)
  273. objc_malloc ((new_max_index + 1) * sizeof (struct sindex *));
  274. #else /* OBJC_SPARSE2 */
  275. old_buckets = array->buckets;
  276. new_buckets = (struct sbucket **)
  277. objc_malloc ((new_max_index + 1) * sizeof (struct sbucket *));
  278. #endif
  279. /* Copy buckets below old_max_index (they are still valid). */
  280. for (counter = 0; counter <= old_max_index; counter++ )
  281. {
  282. #ifdef OBJC_SPARSE3
  283. new_indices[counter] = old_indices[counter];
  284. #else /* OBJC_SPARSE2 */
  285. new_buckets[counter] = old_buckets[counter];
  286. #endif
  287. }
  288. #ifdef OBJC_SPARSE3
  289. /* Reset entries above old_max_index to empty_bucket. */
  290. for (counter = old_max_index + 1; counter <= new_max_index; counter++)
  291. new_indices[counter] = array->empty_index;
  292. #else /* OBJC_SPARSE2 */
  293. /* Reset entries above old_max_index to empty_bucket. */
  294. for (counter = old_max_index + 1; counter <= new_max_index; counter++)
  295. new_buckets[counter] = array->empty_bucket;
  296. #endif
  297. #ifdef OBJC_SPARSE3
  298. /* Install the new indices. */
  299. array->indices = new_indices;
  300. #else /* OBJC_SPARSE2 */
  301. array->buckets = new_buckets;
  302. #endif
  303. #ifdef OBJC_SPARSE3
  304. /* Free the old indices. */
  305. sarray_free_garbage (old_indices);
  306. #else /* OBJC_SPARSE2 */
  307. sarray_free_garbage (old_buckets);
  308. #endif
  309. idxsize += (new_max_index-old_max_index);
  310. return;
  311. }
  312. }
  313. /* Free a sparse array allocated with sarray_new */
  314. void
  315. sarray_free (struct sarray *array) {
  316. #ifdef OBJC_SPARSE3
  317. size_t old_max_index = (array->capacity - 1)/INDEX_CAPACITY;
  318. struct sindex **old_indices;
  319. #else
  320. size_t old_max_index = (array->capacity - 1)/BUCKET_SIZE;
  321. struct sbucket **old_buckets;
  322. #endif
  323. size_t counter = 0;
  324. assert (array->ref_count != 0); /* Freed multiple times!!! */
  325. if (--(array->ref_count) != 0) /* There exists copies of me */
  326. return;
  327. #ifdef OBJC_SPARSE3
  328. old_indices = array->indices;
  329. #else
  330. old_buckets = array->buckets;
  331. #endif
  332. /* Free all entries that do not point to empty_bucket. */
  333. for (counter = 0; counter <= old_max_index; counter++ )
  334. {
  335. #ifdef OBJC_SPARSE3
  336. struct sindex *idx = old_indices[counter];
  337. if ((idx != array->empty_index)
  338. && (idx->version.version == array->version.version))
  339. {
  340. int c2;
  341. for (c2 = 0; c2 < INDEX_SIZE; c2++)
  342. {
  343. struct sbucket *bkt = idx->buckets[c2];
  344. if ((bkt != array->empty_bucket)
  345. && (bkt->version.version == array->version.version))
  346. {
  347. sarray_free_garbage (bkt);
  348. nbuckets -= 1;
  349. }
  350. }
  351. sarray_free_garbage (idx);
  352. nindices -= 1;
  353. }
  354. #else /* OBJC_SPARSE2 */
  355. struct sbucket *bkt = old_buckets[counter];
  356. if ((bkt != array->empty_bucket)
  357. && (bkt->version.version == array->version.version))
  358. {
  359. sarray_free_garbage (bkt);
  360. nbuckets -= 1;
  361. }
  362. #endif
  363. }
  364. #ifdef OBJC_SPARSE3
  365. /* Free empty_index. */
  366. if (array->empty_index->version.version == array->version.version)
  367. {
  368. sarray_free_garbage (array->empty_index);
  369. nindices -= 1;
  370. }
  371. #endif
  372. /* Free empty_bucket. */
  373. if (array->empty_bucket->version.version == array->version.version)
  374. {
  375. sarray_free_garbage (array->empty_bucket);
  376. nbuckets -= 1;
  377. }
  378. idxsize -= (old_max_index + 1);
  379. narrays -= 1;
  380. #ifdef OBJC_SPARSE3
  381. /* Free bucket table. */
  382. sarray_free_garbage (array->indices);
  383. #else
  384. /* Free bucket table. */
  385. sarray_free_garbage (array->buckets);
  386. #endif
  387. /* If this is a copy of another array, we free it (which might just
  388. decrement its reference count so it will be freed when no longer
  389. in use). */
  390. if (array->is_copy_of)
  391. sarray_free (array->is_copy_of);
  392. /* Free array. */
  393. sarray_free_garbage (array);
  394. }
  395. /* This is a lazy copy. Only the core of the structure is actually
  396. copied. */
  397. struct sarray *
  398. sarray_lazy_copy (struct sarray *oarr)
  399. {
  400. struct sarray *arr;
  401. #ifdef OBJC_SPARSE3
  402. size_t num_indices = ((oarr->capacity - 1)/INDEX_CAPACITY) + 1;
  403. struct sindex **new_indices;
  404. #else /* OBJC_SPARSE2 */
  405. size_t num_indices = ((oarr->capacity - 1)/BUCKET_SIZE) + 1;
  406. struct sbucket **new_buckets;
  407. #endif
  408. /* Allocate core array. */
  409. arr = (struct sarray *) objc_malloc (sizeof (struct sarray)); /* !!! */
  410. arr->version.version = oarr->version.version + 1;
  411. #ifdef OBJC_SPARSE3
  412. arr->empty_index = oarr->empty_index;
  413. #endif
  414. arr->empty_bucket = oarr->empty_bucket;
  415. arr->ref_count = 1;
  416. oarr->ref_count += 1;
  417. arr->is_copy_of = oarr;
  418. arr->capacity = oarr->capacity;
  419. #ifdef OBJC_SPARSE3
  420. /* Copy bucket table. */
  421. new_indices = (struct sindex **)
  422. objc_malloc (sizeof (struct sindex *) * num_indices);
  423. memcpy (new_indices, oarr->indices, sizeof (struct sindex *) * num_indices);
  424. arr->indices = new_indices;
  425. #else
  426. /* Copy bucket table. */
  427. new_buckets = (struct sbucket **)
  428. objc_malloc (sizeof (struct sbucket *) * num_indices);
  429. memcpy (new_buckets, oarr->buckets, sizeof (struct sbucket *) * num_indices);
  430. arr->buckets = new_buckets;
  431. #endif
  432. idxsize += num_indices;
  433. narrays += 1;
  434. return arr;
  435. }