From 25f93309285147f8e1ee72a8da09359a197558a5 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Fri, 25 Mar 2016 20:54:01 +0800 Subject: [PATCH 1/2] store block size in when using malloc and nogc this allows for a correct implementation of realloc, which is needed as code using it assumes new values will be zeroed out / nil --- lib/system/mmdisp.nim | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/system/mmdisp.nim b/lib/system/mmdisp.nim index 4b01cf4a87..76c1b476e1 100644 --- a/lib/system/mmdisp.nim +++ b/lib/system/mmdisp.nim @@ -389,15 +389,30 @@ elif defined(nogc) and defined(useMalloc): when not defined(useNimRtl): proc alloc(size: Natural): pointer = - result = cmalloc(size) - if result == nil: raiseOutOfMem() + var x = cmalloc(size + sizeof(size)) + if x == nil: raiseOutOfMem() + + cast[ptr int](x)[] = size + result = cast[pointer](cast[int](x) + sizeof(size)) + proc alloc0(size: Natural): pointer = result = alloc(size) zeroMem(result, size) proc realloc(p: pointer, newsize: Natural): pointer = - result = crealloc(p, newsize) - if result == nil: raiseOutOfMem() - proc dealloc(p: pointer) = cfree(p) + var x = cast[pointer](cast[int](p) - sizeof(newsize)) + let oldsize = cast[ptr int](x)[] + + x = crealloc(x, newsize + sizeof(newsize)) + + if x == nil: raiseOutOfMem() + + cast[ptr int](x)[] = newsize + result = cast[pointer](cast[int](x) + sizeof(newsize)) + + if newsize > oldsize: + zeroMem(cast[pointer](cast[int](result) + oldsize), newsize - oldsize) + + proc dealloc(p: pointer) = cfree(cast[pointer](cast[int](p) - sizeof(int))) proc allocShared(size: Natural): pointer = result = cmalloc(size) From a2501321c39a89fb0bad52dcb8ef7c974d4ae5d2 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Sat, 2 Apr 2016 21:15:10 +0800 Subject: [PATCH 2/2] document useMalloc changes --- doc/nimc.txt | 4 +++- web/news.txt | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/doc/nimc.txt b/doc/nimc.txt index e7cb570379..48dbaeb219 100644 --- a/doc/nimc.txt +++ b/doc/nimc.txt @@ -243,7 +243,9 @@ Define Effect ``useFork`` Makes ``osproc`` use ``fork`` instead of ``posix_spawn``. ``useNimRtl`` Compile and link against ``nimrtl.dll``. ``useMalloc`` Makes Nim use C's `malloc`:idx: instead of Nim's - own memory manager. This only works with ``gc:none``. + own memory manager, ableit prefixing each allocation with + its size to support clearing memory on reallocation. + This only works with ``gc:none``. ``useRealtimeGC`` Enables support of Nim's GC for *soft* realtime systems. See the documentation of the `gc `_ for further information. diff --git a/web/news.txt b/web/news.txt index e818ed66e7..f0f17448f6 100644 --- a/web/news.txt +++ b/web/news.txt @@ -20,7 +20,11 @@ Changes affecting backwards compatibility new experimental ``this`` pragma to achieve a similar effect to what the old ``using`` statement tried to achieve. - Typeless parameters have been removed from the language since it would clash with ``using``. - +- When using ``useMalloc``, an additional header containing the size of the + allocation will be allocated, to support zeroing memory on realloc as expected + by the language. With this change, ``alloc`` and ``dealloc`` are no longer + aliases for ``malloc`` and ``free`` - use ``c_malloc`` and ``c_free`` if + you need that. Library Additions -----------------