Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed lib/system/allocators.nim. seqs_v2 and strs_v2 now uses allocShared0. #13190

Merged
merged 11 commits into from
Jan 23, 2020
Merged
Prev Previous commit
Next Next commit
Fixed -d:useMalloc allocShared / reallocShared / deallocShared. These…
… now use the alloc/dealloc/realloc implementation that also takes care of zeroing memory at realloc.
zevv committed Jan 22, 2020
commit 122bdebc6780e76624c3b3e9a33c37f2fa433fc1
11 changes: 8 additions & 3 deletions lib/system/mmdisp.nim
Original file line number Diff line number Diff line change
@@ -356,6 +356,10 @@ elif defined(gogc):

elif (defined(nogc) or defined(gcDestructors)) and defined(useMalloc):

# libc realloc() does not zero memory when the buffer grows, so we need to do
# that here. Every allocated buffer is prepended with the size of the
# allocation so we know what to zero when growing the buffer with realloc()

when not defined(useNimRtl):
proc alloc(size: Natural): pointer =
var x = c_malloc (size + sizeof(size)).csize_t
@@ -384,15 +388,16 @@ elif (defined(nogc) or defined(gcDestructors)) and defined(useMalloc):
proc dealloc(p: pointer) = c_free(cast[pointer](cast[int](p) - sizeof(int)))

proc allocShared(size: Natural): pointer =
result = c_malloc(size.csize_t)
result = alloc(size.csize_t)
if result == nil: raiseOutOfMem()
proc allocShared0(size: Natural): pointer =
result = alloc(size)
zeroMem(result, size)
proc reallocShared(p: pointer, newsize: Natural): pointer =
result = c_realloc(p, newsize.csize_t)
result = realloc(p, newsize.csize_t)
if result == nil: raiseOutOfMem()
proc deallocShared(p: pointer) = c_free(p)
proc deallocShared(p: pointer) =
dealloc(p)

proc GC_disable() = discard
proc GC_enable() = discard