Skip to content

Commit

Permalink
Fixed -d:useMalloc allocShared / reallocShared / deallocShared. These…
Browse files Browse the repository at this point in the history
… now use the alloc/dealloc/realloc implementation that also takes care of zeroing memory at realloc.
  • Loading branch information
zevv committed Jan 18, 2020
1 parent 6751721 commit 4076b5d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/system/mmdisp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4076b5d

Please sign in to comment.