Skip to content

Commit

Permalink
Handle edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
SirOlaf committed Nov 5, 2024
1 parent 2bfdc79 commit ea71530
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/system/alloc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,18 @@ proc realloc0(allocator: var MemRegion, p: pointer, oldsize, newsize: Natural):
proc abandonAllocator(a: var MemRegion) =
# In the current implementation, this is allowed to leak memory
# when any of it has been shared and not reclaimed yet
for s in 0 .. max(1, SmallChunkSize div MemAlign-1):
# Edge case: A shared cell was deallocated and this thread didn't allocate again before being abandoned
# -> must adjust the small cell counter
when hasThreadSupport:
var it = atomicExchangeN(addr a.sharedFreeLists[s], nil, ATOMIC_RELAXED)
else:
var it = a.sharedFreeLists[s]
a.sharedFreeLists[s] = nil
while it != nil:
dec a.smallCellsInUse
it = it.next

if a.smallCellsInUse == 0 and a.bigChunksInUse == 0:
# Deallocating is only safe if all pages are unused
deallocOsPages(a)
Expand Down

0 comments on commit ea71530

Please sign in to comment.