Skip to content

Commit 4076b5d

Browse files
committed
Fixed -d:useMalloc allocShared / reallocShared / deallocShared. These now use the alloc/dealloc/realloc implementation that also takes care of zeroing memory at realloc.
1 parent 6751721 commit 4076b5d

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

lib/system/mmdisp.nim

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ elif defined(gogc):
356356

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

359+
# libc realloc() does not zero memory when the buffer grows, so we need to do
360+
# that here. Every allocated buffer is prepended with the size of the
361+
# allocation so we know what to zero when growing the buffer with realloc()
362+
359363
when not defined(useNimRtl):
360364
proc alloc(size: Natural): pointer =
361365
var x = c_malloc (size + sizeof(size)).csize_t
@@ -384,15 +388,16 @@ elif (defined(nogc) or defined(gcDestructors)) and defined(useMalloc):
384388
proc dealloc(p: pointer) = c_free(cast[pointer](cast[int](p) - sizeof(int)))
385389

386390
proc allocShared(size: Natural): pointer =
387-
result = c_malloc(size.csize_t)
391+
result = alloc(size.csize_t)
388392
if result == nil: raiseOutOfMem()
389393
proc allocShared0(size: Natural): pointer =
390394
result = alloc(size)
391395
zeroMem(result, size)
392396
proc reallocShared(p: pointer, newsize: Natural): pointer =
393-
result = c_realloc(p, newsize.csize_t)
397+
result = realloc(p, newsize.csize_t)
394398
if result == nil: raiseOutOfMem()
395-
proc deallocShared(p: pointer) = c_free(p)
399+
proc deallocShared(p: pointer) =
400+
dealloc(p)
396401

397402
proc GC_disable() = discard
398403
proc GC_enable() = discard

0 commit comments

Comments
 (0)