diff options
author | Ico Doornekamp <ico@pruts.nl> | 2020-01-25 17:14:31 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2020-01-25 17:14:31 +0100 |
commit | 4f3dd33509070f0f50bbe5bbb6cbfc8a12b47b49 (patch) | |
tree | adfb765ed9ea91be1c5ae8510b143c3bea7fe0ec /lib/system/mm/malloc.nim | |
parent | 5124b2e575a0293c665026dd766700c03bc6a04f (diff) | |
download | Nim-4f3dd33509070f0f50bbe5bbb6cbfc8a12b47b49.tar.gz |
Cleaned up mmdisp.nim, moved implementations into lib/system/mm/ (#13254)
Diffstat (limited to 'lib/system/mm/malloc.nim')
-rw-r--r-- | lib/system/mm/malloc.nim | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/lib/system/mm/malloc.nim b/lib/system/mm/malloc.nim new file mode 100644 index 000000000..27e32f87d --- /dev/null +++ b/lib/system/mm/malloc.nim @@ -0,0 +1,80 @@ + +proc allocImpl(size: Natural): pointer = + c_malloc(size.csize_t) + +proc alloc0Impl(size: Natural): pointer = + c_calloc(size.csize_t, 1) + +proc reallocImpl(p: pointer, newsize: Natural): pointer = + c_realloc(p, newSize.csize_t) + +proc realloc0Impl(p: pointer, oldsize, newsize: Natural): pointer = + result = realloc(p, newsize.csize_t) + if newsize > oldsize: + zeroMem(cast[pointer](cast[int](result) + oldsize), newsize - oldsize) + +proc deallocImpl(p: pointer) = + c_free(p) + + +# The shared allocators map on the regular ones + +proc allocSharedImpl(size: Natural): pointer = + allocImpl(size) + +proc allocShared0Impl(size: Natural): pointer = + alloc0Impl(size) + +proc reallocSharedImpl(p: pointer, newsize: Natural): pointer = + reallocImpl(p, newsize) + +proc reallocShared0Impl(p: pointer, oldsize, newsize: Natural): pointer = + realloc0Impl(p, oldsize, newsize) + +proc deallocSharedImpl(p: pointer) = deallocImpl(p) + + +# Empty stubs for the GC + +proc GC_disable() = discard +proc GC_enable() = discard +proc GC_fullCollect() = discard +proc GC_setStrategy(strategy: GC_Strategy) = discard +proc GC_enableMarkAndSweep() = discard +proc GC_disableMarkAndSweep() = discard + +proc getOccupiedMem(): int = discard +proc getFreeMem(): int = discard +proc getTotalMem(): int = discard + +proc nimGC_setStackBottom(theStackBottom: pointer) = discard + +proc initGC() = discard + +proc newObjNoInit(typ: PNimType, size: int): pointer = + result = alloc(size) + +proc growObj(old: pointer, newsize: int): pointer = + result = realloc(old, newsize) + +proc nimGCref(p: pointer) {.compilerproc, inline.} = discard +proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard + +proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} = + dest[] = src +proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} = + dest[] = src +proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline, + deprecated: "old compiler compat".} = asgnRef(dest, src) + +type + MemRegion = object + +proc alloc(r: var MemRegion, size: int): pointer = + result = alloc(size) +proc alloc0Impl(r: var MemRegion, size: int): pointer = + result = alloc0Impl(size) +proc dealloc(r: var MemRegion, p: pointer) = dealloc(p) +proc deallocOsPages(r: var MemRegion) {.inline.} = discard +proc deallocOsPages() {.inline.} = discard + |