commit f716536100e86bb410341daf3f84cf23f4e3adc2 Author: Lukas Fittl Date: Sat Jan 9 23:42:42 2021 -0800 Add AllocSetDeleteFreeList to clean up aset.c's freelist This frees up the memory allocated to memory contexts that are kept for future allocations. This behaves similar to changing aset.c's MAX_FREE_CONTEXTS to 0, but only does the cleanup when called, and allows the freelist approach to be used during Postgres operations. diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c index 0bbbf93672..b9f0679b35 100644 --- a/src/backend/utils/mmgr/aset.c +++ b/src/backend/utils/mmgr/aset.c @@ -1655,3 +1655,25 @@ AllocSetCheck(MemoryContext context) } #endif /* MEMORY_CONTEXT_CHECKING */ + +void +AllocSetDeleteFreeList(MemoryContext context) +{ + AllocSet set = (AllocSet) context; + if (set->freeListIndex >= 0) + { + AllocSetFreeList *freelist = &context_freelists[set->freeListIndex]; + + while (freelist->first_free != NULL) + { + AllocSetContext *oldset = freelist->first_free; + + freelist->first_free = (AllocSetContext *) oldset->header.nextchild; + freelist->num_free--; + + /* All that remains is to free the header/initial block */ + free(oldset); + } + Assert(freelist->num_free == 0); + } +} diff --git a/src/include/utils/memutils.h b/src/include/utils/memutils.h index 21640d62a6..ca9d9304ae 100644 --- a/src/include/utils/memutils.h +++ b/src/include/utils/memutils.h @@ -114,6 +114,7 @@ extern MemoryContext AllocSetContextCreateInternal(MemoryContext parent, Size minContextSize, Size initBlockSize, Size maxBlockSize); +extern void AllocSetDeleteFreeList(MemoryContext context); /* * This wrapper macro exists to check for non-constant strings used as context