From b0ca2963595a04a7bde0d9f3db461675d672d8f8 Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Mon, 23 Dec 2024 11:28:51 -0500 Subject: [PATCH] [GC] Make sure we don't take a GC lock for grow/shrink calls without entering a busy state. (#414) --- sdlib/d/gc/tcache.d | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/sdlib/d/gc/tcache.d b/sdlib/d/gc/tcache.d index 716dc2270..afcaf0071 100644 --- a/sdlib/d/gc/tcache.d +++ b/sdlib/d/gc/tcache.d @@ -303,13 +303,13 @@ public: auto npages = getPageCount(size); if (epages < npages) { - if (!pd.arena.growLarge(emap, e, npages)) { + if (!growLarge(pd, npages)) { goto LargeResizeFailed; } triggerAllocationEvent((npages - epages) * PageSize); } else if (epages > npages) { - if (!pd.arena.shrinkLarge(emap, e, npages)) { + if (!shrinkLarge(pd, npages)) { goto LargeResizeFailed; } @@ -708,7 +708,7 @@ private: auto npages = getPageCount(newCapacity); if (epages < npages) { - if (!pd.arena.growLarge(emap, e, npages)) { + if (!growLarge(pd, npages)) { return false; } @@ -722,6 +722,20 @@ private: return true; } + bool growLarge(PageDescriptor pd, uint npages) { + state.enterBusyState(); + scope(exit) state.exitBusyState(); + + return pd.arena.growLarge(emap, pd.extent, npages); + } + + bool shrinkLarge(PageDescriptor pd, uint npages) { + state.enterBusyState(); + scope(exit) state.exitBusyState(); + + return pd.arena.shrinkLarge(emap, pd.extent, npages); + } + auto getPageDescriptor(void* ptr) { auto pd = maybeGetPageDescriptor(ptr); assert(pd.extent !is null);