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
commit ae4142b2da
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``. ``useFork`` Makes ``osproc`` use ``fork`` instead of ``posix_spawn``.
``useNimRtl`` Compile and link against ``nimrtl.dll``. ``useNimRtl`` Compile and link against ``nimrtl.dll``.
``useMalloc`` Makes Nim use C's `malloc`:idx: instead of Nim's ``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 ``useRealtimeGC`` Enables support of Nim's GC for *soft* realtime
systems. See the documentation of the `gc <gc.html>`_ systems. See the documentation of the `gc <gc.html>`_
for further information. for further information.

View file

@ -389,15 +389,30 @@ elif defined(nogc) and defined(useMalloc):
when not defined(useNimRtl): when not defined(useNimRtl):
proc alloc(size: Natural): pointer = proc alloc(size: Natural): pointer =
result = cmalloc(size) var x = cmalloc(size + sizeof(size))
if result == nil: raiseOutOfMem() if x == nil: raiseOutOfMem()
cast[ptr int](x)[] = size
result = cast[pointer](cast[int](x) + sizeof(size))
proc alloc0(size: Natural): pointer = proc alloc0(size: Natural): pointer =
result = alloc(size) result = alloc(size)
zeroMem(result, size) zeroMem(result, size)
proc realloc(p: pointer, newsize: Natural): pointer = proc realloc(p: pointer, newsize: Natural): pointer =
result = crealloc(p, newsize) var x = cast[pointer](cast[int](p) - sizeof(newsize))
if result == nil: raiseOutOfMem() let oldsize = cast[ptr int](x)[]
proc dealloc(p: pointer) = cfree(p)
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 = proc allocShared(size: Natural): pointer =
result = cmalloc(size) result = cmalloc(size)

View file

@ -32,6 +32,11 @@ Changes affecting backwards compatibility
raises a ``KeyError`` exception. You can compile with the ``-d:nimJsonGet`` 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 flag to get a list of usages of ``[]``, as well as to restore the operator's
previous behaviour. 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 Library Additions