diff options
-rw-r--r-- | doc/nimc.txt | 4 | ||||
-rw-r--r-- | lib/system/mmdisp.nim | 25 | ||||
-rw-r--r-- | web/news.txt | 5 |
3 files changed, 28 insertions, 6 deletions
diff --git a/doc/nimc.txt b/doc/nimc.txt index e7cb57037..48dbaeb21 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 <gc.html>`_ for further information. diff --git a/lib/system/mmdisp.nim b/lib/system/mmdisp.nim index 85d16d2fe..d7010a1a3 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) diff --git a/web/news.txt b/web/news.txt index 0a83503c1..c9561c7a5 100644 --- a/web/news.txt +++ b/web/news.txt @@ -32,6 +32,11 @@ Changes affecting backwards compatibility raises a ``KeyError`` exception. You can compile with the ``-d:nimJsonGet`` flag to get a list of usages of ``[]``, as well as to restore the operator's previous behaviour. +- 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 |