store block size in when using malloc and nogc
this allows for a correct implementation of realloc, which is needed as code using it assumes new values will be zeroed out / nil
This commit is contained in:
parent
31bb5c4ae0
commit
25f9330928
1 changed files with 20 additions and 5 deletions
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue