BoehmGC.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_GC_BOEHM
  3. #include <stdint.h>
  4. #include "gc_wrapper.h"
  5. #include "GarbageCollector.h"
  6. #include "WriteBarrier.h"
  7. #include "WriteBarrierValidation.h"
  8. #include "os/Mutex.h"
  9. #include "vm/Array.h"
  10. #include "vm/Domain.h"
  11. #include "vm/Profiler.h"
  12. #include "utils/Il2CppHashMap.h"
  13. #include "utils/HashUtils.h"
  14. #include "il2cpp-object-internals.h"
  15. #include "Baselib.h"
  16. #include "Cpp/ReentrantLock.h"
  17. static bool s_GCInitialized = false;
  18. #if IL2CPP_ENABLE_DEFERRED_GC
  19. static bool s_PendingGC = false;
  20. #endif
  21. static void on_gc_event(GC_EventType eventType);
  22. #if IL2CPP_ENABLE_PROFILER
  23. using il2cpp::vm::Profiler;
  24. static void on_heap_resize(GC_word newSize);
  25. #endif
  26. #if !RUNTIME_TINY
  27. static GC_push_other_roots_proc default_push_other_roots;
  28. typedef Il2CppHashMap<char*, char*, il2cpp::utils::PassThroughHash<char*> > RootMap;
  29. static RootMap s_Roots;
  30. static void push_other_roots(void);
  31. typedef struct ephemeron_node ephemeron_node;
  32. static ephemeron_node* ephemeron_list;
  33. static void
  34. clear_ephemerons(void);
  35. static GC_ms_entry*
  36. push_ephemerons(GC_ms_entry* mark_stack_ptr, GC_ms_entry* mark_stack_limit);
  37. #if !IL2CPP_ENABLE_WRITE_BARRIER_VALIDATION
  38. #define ELEMENT_CHUNK_SIZE 256
  39. #define VECTOR_PROC_INDEX 6
  40. #define BYTES_PER_WORD (sizeof(GC_word))
  41. #include <gc_vector.h>
  42. GC_ms_entry* GC_gcj_vector_proc(GC_word* addr, GC_ms_entry* mark_stack_ptr,
  43. GC_ms_entry* mark_stack_limit, GC_word env)
  44. {
  45. Il2CppArraySize* a = NULL;
  46. if (env)
  47. {
  48. IL2CPP_ASSERT(env == 1);
  49. a = (Il2CppArraySize*)GC_base(addr);
  50. }
  51. else
  52. {
  53. IL2CPP_ASSERT(addr == GC_base(addr));
  54. a = (Il2CppArraySize*)addr;
  55. }
  56. if (!a->max_length)
  57. return mark_stack_ptr;
  58. il2cpp_array_size_t length = a->max_length;
  59. Il2CppClass* array_type = a->vtable->klass;
  60. Il2CppClass* element_type = array_type->element_class;
  61. GC_descr element_desc = (GC_descr)element_type->gc_desc;
  62. IL2CPP_ASSERT((element_desc & GC_DS_TAGS) == GC_DS_BITMAP);
  63. IL2CPP_ASSERT(element_type->byval_arg.valuetype);
  64. int words_per_element = array_type->element_size / BYTES_PER_WORD;
  65. GC_word* actual_start = (GC_word*)a->vector;
  66. /* start at first element or resume from last iteration */
  67. GC_word* start = env ? addr : actual_start;
  68. /* end at last element or max chunk size */
  69. GC_word* actual_end = actual_start + length * words_per_element;
  70. return GC_gcj_vector_mark_proc(mark_stack_ptr, mark_stack_limit, element_desc, start, actual_end, words_per_element);
  71. }
  72. #endif // !IL2CPP_ENABLE_WRITE_BARRIER_VALIDATION
  73. #endif // !RUNTIME_TINY
  74. void
  75. il2cpp::gc::GarbageCollector::Initialize()
  76. {
  77. if (s_GCInitialized)
  78. return;
  79. #if IL2CPP_ENABLE_WRITE_BARRIER_VALIDATION
  80. il2cpp::gc::WriteBarrierValidation::Setup();
  81. #endif
  82. // This tells the GC that we are not scanning dynamic library data segments and that
  83. // the GC tracked data structures need ot be manually pushed and marked.
  84. // Call this before GC_INIT since the initialization logic uses this value.
  85. GC_set_no_dls(1);
  86. #if !IL2CPP_DEVELOPMENT
  87. // Turn off GC logging and warnings for non-development builds
  88. GC_set_warn_proc(GC_ignore_warn_proc);
  89. #endif
  90. #if IL2CPP_ENABLE_WRITE_BARRIERS
  91. GC_enable_incremental();
  92. #if IL2CPP_INCREMENTAL_TIME_SLICE
  93. GC_set_time_limit(IL2CPP_INCREMENTAL_TIME_SLICE);
  94. #endif
  95. #endif
  96. #if !RUNTIME_TINY
  97. default_push_other_roots = GC_get_push_other_roots();
  98. GC_set_push_other_roots(push_other_roots);
  99. GC_set_mark_stack_empty(push_ephemerons);
  100. #endif // !RUNTIME_TINY
  101. GC_set_on_collection_event(&on_gc_event);
  102. #if IL2CPP_ENABLE_PROFILER
  103. GC_set_on_heap_resize(&on_heap_resize);
  104. #endif
  105. GC_INIT();
  106. #if defined(GC_THREADS)
  107. GC_set_finalize_on_demand(1);
  108. #if !RUNTIME_TINY
  109. GC_set_finalizer_notifier(&il2cpp::gc::GarbageCollector::NotifyFinalizers);
  110. #endif
  111. // We need to call this if we want to manually register threads, i.e. GC_register_my_thread
  112. #if !IL2CPP_TARGET_JAVASCRIPT
  113. GC_allow_register_threads();
  114. #endif
  115. #endif
  116. #ifdef GC_GCJ_SUPPORT
  117. GC_init_gcj_malloc(0, NULL);
  118. #endif
  119. #if !RUNTIME_TINY && !IL2CPP_ENABLE_WRITE_BARRIER_VALIDATION
  120. GC_init_gcj_vector(VECTOR_PROC_INDEX, (void*)GC_gcj_vector_proc);
  121. #endif
  122. s_GCInitialized = true;
  123. }
  124. void il2cpp::gc::GarbageCollector::UninitializeGC()
  125. {
  126. #if IL2CPP_ENABLE_WRITE_BARRIER_VALIDATION
  127. il2cpp::gc::WriteBarrierValidation::Run();
  128. #endif
  129. GC_deinit();
  130. #if IL2CPP_ENABLE_RELOAD
  131. s_GCInitialized = false;
  132. default_push_other_roots = NULL;
  133. s_Roots.clear();
  134. #endif
  135. }
  136. int32_t
  137. il2cpp::gc::GarbageCollector::GetCollectionCount(int32_t generation)
  138. {
  139. return (int32_t)GC_get_gc_no();
  140. }
  141. int32_t
  142. il2cpp::gc::GarbageCollector::GetMaxGeneration()
  143. {
  144. return 0;
  145. }
  146. void
  147. il2cpp::gc::GarbageCollector::Collect(int maxGeneration)
  148. {
  149. #if IL2CPP_ENABLE_DEFERRED_GC
  150. if (GC_is_disabled())
  151. s_PendingGC = true;
  152. #endif
  153. GC_gcollect();
  154. }
  155. int32_t
  156. il2cpp::gc::GarbageCollector::CollectALittle()
  157. {
  158. #if IL2CPP_ENABLE_DEFERRED_GC
  159. if (s_PendingGC)
  160. {
  161. s_PendingGC = false;
  162. GC_gcollect();
  163. return 0; // no more work to do
  164. }
  165. else
  166. {
  167. return GC_collect_a_little();
  168. }
  169. #else
  170. return GC_collect_a_little();
  171. #endif
  172. }
  173. void
  174. il2cpp::gc::GarbageCollector::StartIncrementalCollection()
  175. {
  176. GC_start_incremental_collection();
  177. }
  178. #if IL2CPP_ENABLE_WRITE_BARRIERS
  179. void
  180. il2cpp::gc::GarbageCollector::SetWriteBarrier(void **ptr)
  181. {
  182. GC_END_STUBBORN_CHANGE(ptr);
  183. }
  184. #endif
  185. int64_t
  186. il2cpp::gc::GarbageCollector::GetUsedHeapSize(void)
  187. {
  188. return GC_get_heap_size() - GC_get_free_bytes();
  189. }
  190. int64_t
  191. il2cpp::gc::GarbageCollector::GetAllocatedHeapSize(void)
  192. {
  193. return GC_get_heap_size();
  194. }
  195. void
  196. il2cpp::gc::GarbageCollector::Disable()
  197. {
  198. GC_disable();
  199. }
  200. void
  201. il2cpp::gc::GarbageCollector::Enable()
  202. {
  203. GC_enable();
  204. }
  205. bool
  206. il2cpp::gc::GarbageCollector::IsDisabled()
  207. {
  208. return GC_is_disabled();
  209. }
  210. static baselib::ReentrantLock s_GCSetModeLock;
  211. static Il2CppGCMode s_CurrentGCMode = IL2CPP_GC_MODE_ENABLED;
  212. void
  213. il2cpp::gc::GarbageCollector::SetMode(Il2CppGCMode mode)
  214. {
  215. os::FastAutoLock lock(&s_GCSetModeLock);
  216. switch (mode)
  217. {
  218. case IL2CPP_GC_MODE_ENABLED:
  219. if (s_CurrentGCMode == IL2CPP_GC_MODE_DISABLED)
  220. GC_enable();
  221. GC_set_disable_automatic_collection(false);
  222. break;
  223. case IL2CPP_GC_MODE_DISABLED:
  224. if (s_CurrentGCMode != IL2CPP_GC_MODE_DISABLED)
  225. GC_disable();
  226. break;
  227. case IL2CPP_GC_MODE_MANUAL:
  228. if (s_CurrentGCMode == IL2CPP_GC_MODE_DISABLED)
  229. GC_enable();
  230. GC_set_disable_automatic_collection(true);
  231. break;
  232. }
  233. s_CurrentGCMode = mode;
  234. }
  235. void
  236. il2cpp::gc::GarbageCollector::RegisterThread()
  237. {
  238. #if defined(GC_THREADS) && !IL2CPP_TARGET_JAVASCRIPT
  239. struct GC_stack_base sb;
  240. int res;
  241. res = GC_get_stack_base(&sb);
  242. if (res != GC_SUCCESS)
  243. {
  244. /* Can't determine the register stack bounds */
  245. IL2CPP_ASSERT(false && "GC_get_stack_base () failed, aborting.");
  246. /* Abort we can't scan the stack, so we can't use the GC */
  247. abort();
  248. }
  249. res = GC_register_my_thread(&sb);
  250. if ((res != GC_SUCCESS) && (res != GC_DUPLICATE))
  251. {
  252. IL2CPP_ASSERT(false && "GC_register_my_thread () failed.");
  253. /* Abort we can't use the GC on this thread, so we can't run managed code */
  254. abort();
  255. }
  256. #endif
  257. }
  258. bool
  259. il2cpp::gc::GarbageCollector::UnregisterThread()
  260. {
  261. #if defined(GC_THREADS) && !IL2CPP_TARGET_JAVASCRIPT
  262. int res;
  263. res = GC_unregister_my_thread();
  264. if (res != GC_SUCCESS)
  265. IL2CPP_ASSERT(false && "GC_unregister_my_thread () failed.");
  266. return res == GC_SUCCESS;
  267. #else
  268. return true;
  269. #endif
  270. }
  271. il2cpp::gc::GarbageCollector::FinalizerCallback il2cpp::gc::GarbageCollector::RegisterFinalizerWithCallback(Il2CppObject* obj, FinalizerCallback callback)
  272. {
  273. FinalizerCallback oldCallback;
  274. void* oldData;
  275. GC_REGISTER_FINALIZER_NO_ORDER((char*)obj, callback, NULL, &oldCallback, &oldData);
  276. IL2CPP_ASSERT(oldData == NULL);
  277. return oldCallback;
  278. }
  279. void
  280. il2cpp::gc::GarbageCollector::AddWeakLink(void **link_addr, Il2CppObject *obj, bool track)
  281. {
  282. /* libgc requires that we use HIDE_POINTER... */
  283. *link_addr = (void*)GC_HIDE_POINTER(obj);
  284. // need this since our strings are not real objects
  285. if (GC_is_heap_ptr(obj))
  286. GC_GENERAL_REGISTER_DISAPPEARING_LINK(link_addr, obj);
  287. }
  288. void
  289. il2cpp::gc::GarbageCollector::RemoveWeakLink(void **link_addr)
  290. {
  291. Il2CppObject* obj = GarbageCollector::GetWeakLink(link_addr);
  292. if (GC_is_heap_ptr(obj))
  293. GC_unregister_disappearing_link(link_addr);
  294. *link_addr = NULL;
  295. }
  296. static void*
  297. RevealLink(void* link_addr)
  298. {
  299. void **link_a = (void**)link_addr;
  300. return GC_REVEAL_POINTER(*link_a);
  301. }
  302. Il2CppObject*
  303. il2cpp::gc::GarbageCollector::GetWeakLink(void **link_addr)
  304. {
  305. Il2CppObject *obj = (Il2CppObject*)GC_call_with_alloc_lock(RevealLink, link_addr);
  306. if (obj == (Il2CppObject*)-1)
  307. return NULL;
  308. return obj;
  309. }
  310. void*
  311. il2cpp::gc::GarbageCollector::MakeDescriptorForObject(size_t *bitmap, int numbits)
  312. {
  313. #ifdef GC_GCJ_SUPPORT
  314. /* It seems there are issues when the bitmap doesn't fit: play it safe */
  315. if (numbits >= 30)
  316. return GC_NO_DESCRIPTOR;
  317. else
  318. {
  319. GC_descr desc = GC_make_descriptor((GC_bitmap)bitmap, numbits);
  320. // we should always have a GC_DS_BITMAP descriptor, as we:
  321. // 1) Always want a precise marker.
  322. // 2) Can never be GC_DS_LENGTH since we always have an object header
  323. // at the beginning of the allocation.
  324. IL2CPP_ASSERT((desc & GC_DS_TAGS) == GC_DS_BITMAP || (desc & GC_DS_TAGS) == (GC_descr)GC_NO_DESCRIPTOR);
  325. return (void*)desc;
  326. }
  327. #else
  328. return 0;
  329. #endif
  330. }
  331. void* il2cpp::gc::GarbageCollector::MakeDescriptorForString()
  332. {
  333. return GC_NO_DESCRIPTOR;
  334. }
  335. void* il2cpp::gc::GarbageCollector::MakeDescriptorForArray()
  336. {
  337. return GC_NO_DESCRIPTOR;
  338. }
  339. void il2cpp::gc::GarbageCollector::StopWorld()
  340. {
  341. GC_stop_world_external();
  342. }
  343. void il2cpp::gc::GarbageCollector::StartWorld()
  344. {
  345. GC_start_world_external();
  346. }
  347. #if RUNTIME_TINY
  348. void*
  349. il2cpp::gc::GarbageCollector::Allocate(size_t size)
  350. {
  351. return GC_MALLOC(size);
  352. }
  353. void*
  354. il2cpp::gc::GarbageCollector::AllocateObject(size_t size, void* type)
  355. {
  356. #if IL2CPP_ENABLE_WRITE_BARRIER_VALIDATION
  357. return GC_gcj_malloc(size, type);
  358. #else
  359. return GC_MALLOC(size);
  360. #endif
  361. }
  362. #endif
  363. void*
  364. il2cpp::gc::GarbageCollector::AllocateFixed(size_t size, void *descr)
  365. {
  366. // Note that we changed the implementation from mono.
  367. // In our case, we expect that
  368. // a) This memory will never be moved
  369. // b) This memory will be scanned for references
  370. // c) This memory will remain 'alive' until explicitly freed
  371. // GC_MALLOC_UNCOLLECTABLE fulfills all these requirements
  372. // It does not accept a descriptor, but there was only one
  373. // or two places in mono that pass a descriptor to this routine
  374. // and we can or will support those use cases in a different manner.
  375. IL2CPP_ASSERT(!descr);
  376. return GC_MALLOC_UNCOLLECTABLE(size);
  377. }
  378. void
  379. il2cpp::gc::GarbageCollector::FreeFixed(void* addr)
  380. {
  381. GC_FREE(addr);
  382. }
  383. #if !RUNTIME_TINY
  384. int32_t
  385. il2cpp::gc::GarbageCollector::InvokeFinalizers()
  386. {
  387. #if IL2CPP_TINY
  388. return 0; // The Tiny profile does not have finalizers
  389. #else
  390. return (int32_t)GC_invoke_finalizers();
  391. #endif
  392. }
  393. bool
  394. il2cpp::gc::GarbageCollector::HasPendingFinalizers()
  395. {
  396. return GC_should_invoke_finalizers() != 0;
  397. }
  398. #endif
  399. int64_t
  400. il2cpp::gc::GarbageCollector::GetMaxTimeSliceNs()
  401. {
  402. return GC_get_time_limit_ns();
  403. }
  404. void
  405. il2cpp::gc::GarbageCollector::SetMaxTimeSliceNs(int64_t maxTimeSlice)
  406. {
  407. GC_set_time_limit_ns(maxTimeSlice);
  408. }
  409. bool
  410. il2cpp::gc::GarbageCollector::IsIncremental()
  411. {
  412. return GC_is_incremental_mode();
  413. }
  414. void on_gc_event(GC_EventType eventType)
  415. {
  416. #if !RUNTIME_TINY
  417. if (eventType == GC_EVENT_RECLAIM_START)
  418. {
  419. clear_ephemerons();
  420. }
  421. #endif
  422. #if IL2CPP_ENABLE_PROFILER
  423. Profiler::GCEvent((Il2CppGCEvent)eventType);
  424. #endif
  425. }
  426. #if IL2CPP_ENABLE_PROFILER
  427. void on_heap_resize(GC_word newSize)
  428. {
  429. Profiler::GCHeapResize((int64_t)newSize);
  430. }
  431. #endif // IL2CPP_ENABLE_PROFILER
  432. void il2cpp::gc::GarbageCollector::ForEachHeapSection(void* user_data, HeapSectionCallback callback)
  433. {
  434. GC_foreach_heap_section(user_data, callback);
  435. }
  436. size_t il2cpp::gc::GarbageCollector::GetSectionCount()
  437. {
  438. return GC_get_heap_section_count();
  439. }
  440. void* il2cpp::gc::GarbageCollector::CallWithAllocLockHeld(GCCallWithAllocLockCallback callback, void* user_data)
  441. {
  442. return GC_call_with_alloc_lock(callback, user_data);
  443. }
  444. typedef struct
  445. {
  446. char *start;
  447. char *end;
  448. } RootData;
  449. #if !RUNTIME_TINY
  450. static void*
  451. register_root(void* arg)
  452. {
  453. RootData* root_data = (RootData*)arg;
  454. s_Roots.insert(std::make_pair(root_data->start, root_data->end));
  455. return NULL;
  456. }
  457. void il2cpp::gc::GarbageCollector::RegisterRoot(char *start, size_t size)
  458. {
  459. RootData root_data;
  460. root_data.start = start;
  461. /* Boehm root processing requires one byte past end of region to be scanned */
  462. root_data.end = start + size + 1;
  463. CallWithAllocLockHeld(register_root, &root_data);
  464. }
  465. static void*
  466. deregister_root(void* arg)
  467. {
  468. s_Roots.erase((char*)arg);
  469. return NULL;
  470. }
  471. void il2cpp::gc::GarbageCollector::UnregisterRoot(char* start)
  472. {
  473. GC_call_with_alloc_lock(deregister_root, start);
  474. }
  475. static void
  476. push_other_roots(void)
  477. {
  478. for (RootMap::iterator iter = s_Roots.begin(); iter != s_Roots.end(); ++iter)
  479. GC_push_all(iter->first, iter->second);
  480. GC_push_all(&ephemeron_list, &ephemeron_list + 1);
  481. if (default_push_other_roots)
  482. default_push_other_roots();
  483. }
  484. struct ephemeron_node
  485. {
  486. ephemeron_node* next;
  487. void* ephemeron_array_weak_link;
  488. };
  489. static void*
  490. ephemeron_array_add(void* arg)
  491. {
  492. ephemeron_node* item = (ephemeron_node*)arg;
  493. ephemeron_node* current = ephemeron_list;
  494. il2cpp::gc::WriteBarrier::GenericStore(&item->next, current);
  495. ephemeron_list = item;
  496. return NULL;
  497. }
  498. struct Ephemeron
  499. {
  500. Il2CppObject* key;
  501. Il2CppObject* value;
  502. };
  503. static void
  504. clear_ephemerons(void)
  505. {
  506. ephemeron_node* prev_node = NULL;
  507. ephemeron_node* current_node = NULL;
  508. /* iterate all registered Ephemeron[] */
  509. for (current_node = ephemeron_list; current_node; current_node = current_node->next)
  510. {
  511. Ephemeron* current_ephemeron, * array_end;
  512. Il2CppObject* tombstone = NULL;
  513. /* reveal weak link value*/
  514. Il2CppArray* array = (Il2CppArray*)GC_REVEAL_POINTER(current_node->ephemeron_array_weak_link);
  515. /* remove unmarked (non-reachable) arrays from the list */
  516. if (!GC_is_marked(array))
  517. {
  518. if (prev_node == NULL)
  519. il2cpp::gc::WriteBarrier::GenericStore(&ephemeron_list, current_node->next);
  520. else
  521. il2cpp::gc::WriteBarrier::GenericStore(&prev_node->next, current_node->next);
  522. continue;
  523. }
  524. prev_node = current_node;
  525. current_ephemeron = il2cpp_array_addr(array, Ephemeron, 0);
  526. array_end = current_ephemeron + array->max_length;
  527. tombstone = il2cpp::vm::Domain::GetCurrent()->ephemeron_tombstone;
  528. for (; current_ephemeron < array_end; ++current_ephemeron)
  529. {
  530. /* skip a null or tombstone (empty) key */
  531. if (!current_ephemeron->key || current_ephemeron->key == tombstone)
  532. continue;
  533. /* If the key is not marked, then set it to the tombstone and the value to NULL. */
  534. if (!GC_is_marked(current_ephemeron->key))
  535. {
  536. il2cpp::gc::WriteBarrier::GenericStore(&current_ephemeron->key, tombstone);
  537. current_ephemeron->value = NULL;
  538. }
  539. }
  540. }
  541. }
  542. static GC_ms_entry*
  543. push_ephemerons(GC_ms_entry* mark_stack_ptr, GC_ms_entry* mark_stack_limit)
  544. {
  545. ephemeron_node* prev_node = NULL;
  546. ephemeron_node* current_node = NULL;
  547. /* iterate all registered Ephemeron[] */
  548. for (current_node = ephemeron_list; current_node; current_node = current_node->next)
  549. {
  550. Ephemeron* current_ephemeron, * array_end;
  551. Il2CppObject* tombstone = NULL;
  552. /* reveal weak link value*/
  553. Il2CppArray* array = (Il2CppArray*)GC_REVEAL_POINTER(current_node->ephemeron_array_weak_link);
  554. /* unreferenced array */
  555. if (!GC_is_marked(array))
  556. {
  557. continue;
  558. }
  559. prev_node = current_node;
  560. current_ephemeron = il2cpp_array_addr(array, Ephemeron, 0);
  561. array_end = current_ephemeron + array->max_length;
  562. tombstone = il2cpp::vm::Domain::GetCurrent()->ephemeron_tombstone;
  563. for (; current_ephemeron < array_end; ++current_ephemeron)
  564. {
  565. /* skip a null or tombstone (empty) key */
  566. if (!current_ephemeron->key || current_ephemeron->key == tombstone)
  567. continue;
  568. /* If the key is not marked, then don't mark value. */
  569. if (!GC_is_marked(current_ephemeron->key))
  570. continue;
  571. if (current_ephemeron->value)
  572. {
  573. mark_stack_ptr = GC_mark_and_push((void*)current_ephemeron->value, mark_stack_ptr, mark_stack_limit, (void**)&current_ephemeron->value);
  574. }
  575. }
  576. }
  577. return mark_stack_ptr;
  578. }
  579. bool il2cpp::gc::GarbageCollector::EphemeronArrayAdd(Il2CppObject* obj)
  580. {
  581. ephemeron_node* item = (ephemeron_node*)GC_MALLOC(sizeof(ephemeron_node));
  582. memset(item, 0, sizeof(ephemeron_node));
  583. AddWeakLink(&item->ephemeron_array_weak_link, obj, false);
  584. GC_call_with_alloc_lock(ephemeron_array_add, item);
  585. return true;
  586. }
  587. #endif // !RUNTIME_TINY
  588. #endif