alloc et al don't have any effect; fixes #9746

This commit is contained in:
Araq 2019-03-01 14:50:41 +01:00
commit e1d17ece5b
3 changed files with 25 additions and 25 deletions

View file

@ -13,10 +13,10 @@ type
ZerosMem ## the allocator always zeros the memory on an allocation ZerosMem ## the allocator always zeros the memory on an allocation
Allocator* = ptr AllocatorObj Allocator* = ptr AllocatorObj
AllocatorObj* {.inheritable.} = object AllocatorObj* {.inheritable.} = object
alloc*: proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall.} alloc*: proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall, raises: [], tags: [].}
dealloc*: proc (a: Allocator; p: pointer; size: int) {.nimcall.} dealloc*: proc (a: Allocator; p: pointer; size: int) {.nimcall, raises: [], tags: [].}
realloc*: proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall.} realloc*: proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall, raises: [], tags: [].}
deallocAll*: proc (a: Allocator) {.nimcall.} deallocAll*: proc (a: Allocator) {.nimcall, raises: [], tags: [].}
flags*: set[AllocatorFlag] flags*: set[AllocatorFlag]
name*: cstring name*: cstring
allocCount: int allocCount: int
@ -31,13 +31,13 @@ proc getLocalAllocator*(): Allocator =
result = localAllocator result = localAllocator
if result == nil: if result == nil:
result = addr allocatorStorage result = addr allocatorStorage
result.alloc = proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall.} = result.alloc = proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall, raises: [].} =
result = system.alloc(size) result = system.alloc(size)
inc a.allocCount inc a.allocCount
result.dealloc = proc (a: Allocator; p: pointer; size: int) {.nimcall.} = result.dealloc = proc (a: Allocator; p: pointer; size: int) {.nimcall, raises: [].} =
system.dealloc(p) system.dealloc(p)
inc a.deallocCount inc a.deallocCount
result.realloc = proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall.} = result.realloc = proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall, raises: [].} =
result = system.realloc(p, newSize) result = system.realloc(p, newSize)
result.deallocAll = nil result.deallocAll = nil
result.flags = {ThreadLocal} result.flags = {ThreadLocal}

View file

@ -80,7 +80,7 @@ type
cap: int cap: int
region: Allocator region: Allocator
proc newSeqPayload(cap, elemSize: int): pointer {.compilerRtl.} = proc newSeqPayload(cap, elemSize: int): pointer {.compilerRtl, raises: [].} =
# we have to use type erasure here as Nim does not support generic # we have to use type erasure here as Nim does not support generic
# compilerProcs. Oh well, this will all be inlined anyway. # compilerProcs. Oh well, this will all be inlined anyway.
if cap > 0: if cap > 0:
@ -93,7 +93,7 @@ proc newSeqPayload(cap, elemSize: int): pointer {.compilerRtl.} =
result = nil result = nil
proc prepareSeqAdd(len: int; p: pointer; addlen, elemSize: int): pointer {. proc prepareSeqAdd(len: int; p: pointer; addlen, elemSize: int): pointer {.
compilerRtl, noSideEffect.} = compilerRtl, noSideEffect, raises: [].} =
{.noSideEffect.}: {.noSideEffect.}:
if len+addlen <= len: if len+addlen <= len:
result = p result = p

View file

@ -1898,14 +1898,14 @@ when not defined(nimscript) and not defined(JS):
when not defined(nimscript): when not defined(nimscript):
when hasAlloc: when hasAlloc:
proc alloc*(size: Natural): pointer {.noconv, rtl, tags: [], benign.} proc alloc*(size: Natural): pointer {.noconv, rtl, tags: [], benign, raises: [].}
## allocates a new memory block with at least ``size`` bytes. The ## allocates a new memory block with at least ``size`` bytes. The
## block has to be freed with ``realloc(block, 0)`` or ## block has to be freed with ``realloc(block, 0)`` or
## ``dealloc(block)``. The block is not initialized, so reading ## ``dealloc(block)``. The block is not initialized, so reading
## from it before writing to it is undefined behaviour! ## from it before writing to it is undefined behaviour!
## The allocated memory belongs to its allocating thread! ## The allocated memory belongs to its allocating thread!
## Use `allocShared` to allocate from a shared heap. ## Use `allocShared` to allocate from a shared heap.
proc createU*(T: typedesc, size = 1.Positive): ptr T {.inline, benign.} = proc createU*(T: typedesc, size = 1.Positive): ptr T {.inline, benign, raises: [].} =
## allocates a new memory block with at least ``T.sizeof * size`` ## allocates a new memory block with at least ``T.sizeof * size``
## bytes. The block has to be freed with ``resize(block, 0)`` or ## bytes. The block has to be freed with ``resize(block, 0)`` or
## ``dealloc(block)``. The block is not initialized, so reading ## ``dealloc(block)``. The block is not initialized, so reading
@ -1913,14 +1913,14 @@ when not defined(nimscript):
## The allocated memory belongs to its allocating thread! ## The allocated memory belongs to its allocating thread!
## Use `createSharedU` to allocate from a shared heap. ## Use `createSharedU` to allocate from a shared heap.
cast[ptr T](alloc(T.sizeof * size)) cast[ptr T](alloc(T.sizeof * size))
proc alloc0*(size: Natural): pointer {.noconv, rtl, tags: [], benign.} proc alloc0*(size: Natural): pointer {.noconv, rtl, tags: [], benign, raises: [].}
## allocates a new memory block with at least ``size`` bytes. The ## allocates a new memory block with at least ``size`` bytes. The
## block has to be freed with ``realloc(block, 0)`` or ## block has to be freed with ``realloc(block, 0)`` or
## ``dealloc(block)``. The block is initialized with all bytes ## ``dealloc(block)``. The block is initialized with all bytes
## containing zero, so it is somewhat safer than ``alloc``. ## containing zero, so it is somewhat safer than ``alloc``.
## The allocated memory belongs to its allocating thread! ## The allocated memory belongs to its allocating thread!
## Use `allocShared0` to allocate from a shared heap. ## Use `allocShared0` to allocate from a shared heap.
proc create*(T: typedesc, size = 1.Positive): ptr T {.inline, benign.} = proc create*(T: typedesc, size = 1.Positive): ptr T {.inline, benign, raises: [].} =
## allocates a new memory block with at least ``T.sizeof * size`` ## allocates a new memory block with at least ``T.sizeof * size``
## bytes. The block has to be freed with ``resize(block, 0)`` or ## bytes. The block has to be freed with ``resize(block, 0)`` or
## ``dealloc(block)``. The block is initialized with all bytes ## ``dealloc(block)``. The block is initialized with all bytes
@ -1929,7 +1929,7 @@ when not defined(nimscript):
## Use `createShared` to allocate from a shared heap. ## Use `createShared` to allocate from a shared heap.
cast[ptr T](alloc0(sizeof(T) * size)) cast[ptr T](alloc0(sizeof(T) * size))
proc realloc*(p: pointer, newSize: Natural): pointer {.noconv, rtl, tags: [], proc realloc*(p: pointer, newSize: Natural): pointer {.noconv, rtl, tags: [],
benign.} benign, raises: [].}
## grows or shrinks a given memory block. If p is **nil** then a new ## grows or shrinks a given memory block. If p is **nil** then a new
## memory block is returned. In either way the block has at least ## memory block is returned. In either way the block has at least
## ``newSize`` bytes. If ``newSize == 0`` and p is not **nil** ## ``newSize`` bytes. If ``newSize == 0`` and p is not **nil**
@ -1937,7 +1937,7 @@ when not defined(nimscript):
## be freed with ``dealloc``. ## be freed with ``dealloc``.
## The allocated memory belongs to its allocating thread! ## The allocated memory belongs to its allocating thread!
## Use `reallocShared` to reallocate from a shared heap. ## Use `reallocShared` to reallocate from a shared heap.
proc resize*[T](p: ptr T, newSize: Natural): ptr T {.inline, benign.} = proc resize*[T](p: ptr T, newSize: Natural): ptr T {.inline, benign, raises: [].} =
## grows or shrinks a given memory block. If p is **nil** then a new ## grows or shrinks a given memory block. If p is **nil** then a new
## memory block is returned. In either way the block has at least ## memory block is returned. In either way the block has at least
## ``T.sizeof * newSize`` bytes. If ``newSize == 0`` and p is not ## ``T.sizeof * newSize`` bytes. If ``newSize == 0`` and p is not
@ -1946,7 +1946,7 @@ when not defined(nimscript):
## its allocating thread! ## its allocating thread!
## Use `resizeShared` to reallocate from a shared heap. ## Use `resizeShared` to reallocate from a shared heap.
cast[ptr T](realloc(p, T.sizeof * newSize)) cast[ptr T](realloc(p, T.sizeof * newSize))
proc dealloc*(p: pointer) {.noconv, rtl, tags: [], benign.} proc dealloc*(p: pointer) {.noconv, rtl, tags: [], benign, raises: [].}
## frees the memory allocated with ``alloc``, ``alloc0`` or ## frees the memory allocated with ``alloc``, ``alloc0`` or
## ``realloc``. This procedure is dangerous! If one forgets to ## ``realloc``. This procedure is dangerous! If one forgets to
## free the memory a leak occurs; if one tries to access freed ## free the memory a leak occurs; if one tries to access freed
@ -1955,27 +1955,27 @@ when not defined(nimscript):
## The freed memory must belong to its allocating thread! ## The freed memory must belong to its allocating thread!
## Use `deallocShared` to deallocate from a shared heap. ## Use `deallocShared` to deallocate from a shared heap.
proc allocShared*(size: Natural): pointer {.noconv, rtl, benign.} proc allocShared*(size: Natural): pointer {.noconv, rtl, benign, raises: [].}
## allocates a new memory block on the shared heap with at ## allocates a new memory block on the shared heap with at
## least ``size`` bytes. The block has to be freed with ## least ``size`` bytes. The block has to be freed with
## ``reallocShared(block, 0)`` or ``deallocShared(block)``. The block ## ``reallocShared(block, 0)`` or ``deallocShared(block)``. The block
## is not initialized, so reading from it before writing to it is ## is not initialized, so reading from it before writing to it is
## undefined behaviour! ## undefined behaviour!
proc createSharedU*(T: typedesc, size = 1.Positive): ptr T {.inline, proc createSharedU*(T: typedesc, size = 1.Positive): ptr T {.inline,
benign.} = benign, raises: [].} =
## allocates a new memory block on the shared heap with at ## allocates a new memory block on the shared heap with at
## least ``T.sizeof * size`` bytes. The block has to be freed with ## least ``T.sizeof * size`` bytes. The block has to be freed with
## ``resizeShared(block, 0)`` or ``freeShared(block)``. The block ## ``resizeShared(block, 0)`` or ``freeShared(block)``. The block
## is not initialized, so reading from it before writing to it is ## is not initialized, so reading from it before writing to it is
## undefined behaviour! ## undefined behaviour!
cast[ptr T](allocShared(T.sizeof * size)) cast[ptr T](allocShared(T.sizeof * size))
proc allocShared0*(size: Natural): pointer {.noconv, rtl, benign.} proc allocShared0*(size: Natural): pointer {.noconv, rtl, benign, raises: [].}
## allocates a new memory block on the shared heap with at ## allocates a new memory block on the shared heap with at
## least ``size`` bytes. The block has to be freed with ## least ``size`` bytes. The block has to be freed with
## ``reallocShared(block, 0)`` or ``deallocShared(block)``. ## ``reallocShared(block, 0)`` or ``deallocShared(block)``.
## The block is initialized with all bytes ## The block is initialized with all bytes
## containing zero, so it is somewhat safer than ``allocShared``. ## containing zero, so it is somewhat safer than ``allocShared``.
proc createShared*(T: typedesc, size = 1.Positive): ptr T {.inline.} = proc createShared*(T: typedesc, size = 1.Positive): ptr T {.inline, raises: [].} =
## allocates a new memory block on the shared heap with at ## allocates a new memory block on the shared heap with at
## least ``T.sizeof * size`` bytes. The block has to be freed with ## least ``T.sizeof * size`` bytes. The block has to be freed with
## ``resizeShared(block, 0)`` or ``freeShared(block)``. ## ``resizeShared(block, 0)`` or ``freeShared(block)``.
@ -1983,26 +1983,26 @@ when not defined(nimscript):
## containing zero, so it is somewhat safer than ``createSharedU``. ## containing zero, so it is somewhat safer than ``createSharedU``.
cast[ptr T](allocShared0(T.sizeof * size)) cast[ptr T](allocShared0(T.sizeof * size))
proc reallocShared*(p: pointer, newSize: Natural): pointer {.noconv, rtl, proc reallocShared*(p: pointer, newSize: Natural): pointer {.noconv, rtl,
benign.} benign, raises: [].}
## grows or shrinks a given memory block on the heap. If p is **nil** ## grows or shrinks a given memory block on the heap. If p is **nil**
## then a new memory block is returned. In either way the block has at ## then a new memory block is returned. In either way the block has at
## least ``newSize`` bytes. If ``newSize == 0`` and p is not **nil** ## least ``newSize`` bytes. If ``newSize == 0`` and p is not **nil**
## ``reallocShared`` calls ``deallocShared(p)``. In other cases the ## ``reallocShared`` calls ``deallocShared(p)``. In other cases the
## block has to be freed with ``deallocShared``. ## block has to be freed with ``deallocShared``.
proc resizeShared*[T](p: ptr T, newSize: Natural): ptr T {.inline.} = proc resizeShared*[T](p: ptr T, newSize: Natural): ptr T {.inline, raises: [].} =
## grows or shrinks a given memory block on the heap. If p is **nil** ## grows or shrinks a given memory block on the heap. If p is **nil**
## then a new memory block is returned. In either way the block has at ## then a new memory block is returned. In either way the block has at
## least ``T.sizeof * newSize`` bytes. If ``newSize == 0`` and p is ## least ``T.sizeof * newSize`` bytes. If ``newSize == 0`` and p is
## not **nil** ``resizeShared`` calls ``freeShared(p)``. In other ## not **nil** ``resizeShared`` calls ``freeShared(p)``. In other
## cases the block has to be freed with ``freeShared``. ## cases the block has to be freed with ``freeShared``.
cast[ptr T](reallocShared(p, T.sizeof * newSize)) cast[ptr T](reallocShared(p, T.sizeof * newSize))
proc deallocShared*(p: pointer) {.noconv, rtl, benign.} proc deallocShared*(p: pointer) {.noconv, rtl, benign, raises: [].}
## frees the memory allocated with ``allocShared``, ``allocShared0`` or ## frees the memory allocated with ``allocShared``, ``allocShared0`` or
## ``reallocShared``. This procedure is dangerous! If one forgets to ## ``reallocShared``. This procedure is dangerous! If one forgets to
## free the memory a leak occurs; if one tries to access freed ## free the memory a leak occurs; if one tries to access freed
## memory (or just freeing it twice!) a core dump may happen ## memory (or just freeing it twice!) a core dump may happen
## or other memory may be corrupted. ## or other memory may be corrupted.
proc freeShared*[T](p: ptr T) {.inline, benign.} = proc freeShared*[T](p: ptr T) {.inline, benign, raises: [].} =
## frees the memory allocated with ``createShared``, ``createSharedU`` or ## frees the memory allocated with ``createShared``, ``createSharedU`` or
## ``resizeShared``. This procedure is dangerous! If one forgets to ## ``resizeShared``. This procedure is dangerous! If one forgets to
## free the memory a leak occurs; if one tries to access freed ## free the memory a leak occurs; if one tries to access freed
@ -2929,7 +2929,7 @@ var
## do when setting this. If ``localRaiseHook`` returns false, the exception ## do when setting this. If ``localRaiseHook`` returns false, the exception
## is caught and does not propagate further through the call stack. ## is caught and does not propagate further through the call stack.
outOfMemHook*: proc () {.nimcall, tags: [], benign.} outOfMemHook*: proc () {.nimcall, tags: [], benign, raises: [].}
## set this variable to provide a procedure that should be called ## set this variable to provide a procedure that should be called
## in case of an `out of memory`:idx: event. The standard handler ## in case of an `out of memory`:idx: event. The standard handler
## writes an error message and terminates the program. `outOfMemHook` can ## writes an error message and terminates the program. `outOfMemHook` can