Merge pull request #4002 from arnetheduck/malloc-store-size

store block size in when using malloc and nogc
This commit is contained in:
Andreas Rumpf
2016-04-26 16:54:27 +02:00
3 changed files with 28 additions and 6 deletions

View File

@@ -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.

View File

@@ -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)

View File

@@ -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