ARC: default to a shared heap with --threads:on

This commit is contained in:
Araq 2019-12-24 12:08:22 +01:00 • committed by Andreas Rumpf
commit 5aa7b1a44b
3 changed files with 15 additions and 5 deletions

View file

@ -2469,7 +2469,7 @@ when not defined(nimscript):
## The freed memory must belong to its allocating thread! ## The freed memory must belong to its allocating thread!
## Use `deallocShared <#deallocShared,pointer>`_ to deallocate from a shared heap. ## Use `deallocShared <#deallocShared,pointer>`_ to deallocate from a shared heap.
proc allocShared*(size: Natural): pointer {.noconv, rtl, benign, raises: [].} proc allocShared*(size: Natural): pointer {.noconv, rtl, benign, raises: [], tags: [].}
## 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. ## least ``size`` bytes.
## ##
@ -2482,7 +2482,7 @@ when not defined(nimscript):
## ##
## See also: ## See also:
## `allocShared0 <#allocShared0,Natural>`_. ## `allocShared0 <#allocShared0,Natural>`_.
proc createSharedU*(T: typedesc, size = 1.Positive): ptr T {.inline, proc createSharedU*(T: typedesc, size = 1.Positive): ptr T {.inline, tags: [],
benign, raises: [].} = 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. ## least ``T.sizeof * size`` bytes.
@ -2498,7 +2498,7 @@ when not defined(nimscript):
## * `createShared <#createShared,typedesc>`_ ## * `createShared <#createShared,typedesc>`_
cast[ptr T](allocShared(T.sizeof * size)) cast[ptr T](allocShared(T.sizeof * size))
proc allocShared0*(size: Natural): pointer {.noconv, rtl, benign, raises: [].} proc allocShared0*(size: Natural): pointer {.noconv, rtl, benign, raises: [], tags: [].}
## 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. ## least ``size`` bytes.
## ##
@ -2522,7 +2522,7 @@ when not defined(nimscript):
## `createSharedU <#createSharedU,typedesc>`_. ## `createSharedU <#createSharedU,typedesc>`_.
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, tags: [],
benign, raises: [].} benign, raises: [].}
## Grows or shrinks a given memory block on the heap. ## Grows or shrinks a given memory block on the heap.
## ##
@ -2543,7 +2543,7 @@ when not defined(nimscript):
## `freeShared <#freeShared,ptr.T>`_. ## `freeShared <#freeShared,ptr.T>`_.
cast[ptr T](reallocShared(p, T.sizeof * newSize)) cast[ptr T](reallocShared(p, T.sizeof * newSize))
proc deallocShared*(p: pointer) {.noconv, rtl, benign, raises: [].} proc deallocShared*(p: pointer) {.noconv, rtl, benign, raises: [], tags: [].}
## Frees the memory allocated with ``allocShared``, ``allocShared0`` or ## Frees the memory allocated with ``allocShared``, ``allocShared0`` or
## ``reallocShared``. ## ``reallocShared``.
## ##

View file

@ -46,18 +46,24 @@ proc getLocalAllocator*(): Allocator =
result = c_malloc(cuint size) result = c_malloc(cuint size)
# XXX do we need this? # XXX do we need this?
nimZeroMem(result, size) nimZeroMem(result, size)
elif compileOption("threads"):
result = system.allocShared0(size)
else: else:
result = system.alloc0(size) result = system.alloc0(size)
inc a.allocCount inc a.allocCount
result.dealloc = proc (a: Allocator; p: pointer; size: int) {.nimcall, raises: [].} = result.dealloc = proc (a: Allocator; p: pointer; size: int) {.nimcall, raises: [].} =
when defined(useMalloc) and not defined(nimscript): when defined(useMalloc) and not defined(nimscript):
c_free(p) c_free(p)
elif compileOption("threads"):
system.deallocShared(p)
else: else:
system.dealloc(p) system.dealloc(p)
inc a.deallocCount inc a.deallocCount
result.realloc = proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall, raises: [].} = result.realloc = proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall, raises: [].} =
when defined(useMalloc) and not defined(nimscript): when defined(useMalloc) and not defined(nimscript):
result = c_realloc(p, cuint newSize) result = c_realloc(p, cuint newSize)
elif compileOption("threads"):
result = system.reallocShared(p, newSize)
else: else:
result = system.realloc(p, newSize) result = system.realloc(p, newSize)
nimZeroMem(result +! oldSize, newSize - oldSize) nimZeroMem(result +! oldSize, newSize - oldSize)

View file

@ -67,6 +67,8 @@ proc nimNewObj(size: int): pointer {.compilerRtl.} =
var orig = c_malloc(cuint s) var orig = c_malloc(cuint s)
nimZeroMem(orig, s) nimZeroMem(orig, s)
result = orig +! sizeof(RefHeader) result = orig +! sizeof(RefHeader)
elif compileOption("threads"):
result = allocShared0(s) +! sizeof(RefHeader)
else: else:
result = alloc0(s) +! sizeof(RefHeader) result = alloc0(s) +! sizeof(RefHeader)
when hasThreadSupport: when hasThreadSupport:
@ -93,6 +95,8 @@ proc nimRawDispose(p: pointer) {.compilerRtl.} =
quit 1 quit 1
when defined(useMalloc): when defined(useMalloc):
c_free(p -! sizeof(RefHeader)) c_free(p -! sizeof(RefHeader))
elif compileOption("threads"):
deallocShared(p -! sizeof(RefHeader))
else: else:
dealloc(p -! sizeof(RefHeader)) dealloc(p -! sizeof(RefHeader))
if allocs > 0: if allocs > 0: