allocators.nim: use zero initialization
This commit is contained in:
parent
430814fdb3
commit
58df5b0a8f
1 changed files with 8 additions and 3 deletions
|
|
@ -29,7 +29,11 @@ var
|
||||||
|
|
||||||
when defined(useMalloc) and not defined(nimscript):
|
when defined(useMalloc) and not defined(nimscript):
|
||||||
import "system/ansi_c"
|
import "system/ansi_c"
|
||||||
import "system/memory"
|
|
||||||
|
import "system/memory"
|
||||||
|
|
||||||
|
template `+!`(p: pointer, s: int): pointer =
|
||||||
|
cast[pointer](cast[int](p) +% s)
|
||||||
|
|
||||||
proc getLocalAllocator*(): Allocator =
|
proc getLocalAllocator*(): Allocator =
|
||||||
result = localAllocator
|
result = localAllocator
|
||||||
|
|
@ -41,7 +45,7 @@ proc getLocalAllocator*(): Allocator =
|
||||||
# XXX do we need this?
|
# XXX do we need this?
|
||||||
nimZeroMem(result, size)
|
nimZeroMem(result, size)
|
||||||
else:
|
else:
|
||||||
result = system.alloc(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):
|
||||||
|
|
@ -54,8 +58,9 @@ proc getLocalAllocator*(): Allocator =
|
||||||
result = c_realloc(p, newSize)
|
result = c_realloc(p, newSize)
|
||||||
else:
|
else:
|
||||||
result = system.realloc(p, newSize)
|
result = system.realloc(p, newSize)
|
||||||
|
nimZeroMem(result +! oldSize, newSize - oldSize)
|
||||||
result.deallocAll = nil
|
result.deallocAll = nil
|
||||||
result.flags = {ThreadLocal}
|
result.flags = {ThreadLocal, ZerosMem}
|
||||||
result.name = "nim_local"
|
result.name = "nim_local"
|
||||||
localAllocator = result
|
localAllocator = result
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue