Some fixes

This commit is contained in:
Yuriy Glukhov 2018-04-19 19:54:04 +03:00 committed by zah
parent 3cc2b9eb5f
commit 3e3d2d4a2f
1 changed files with 4 additions and 3 deletions

View File

@ -92,17 +92,18 @@ iterator mpairs*(a: var StackArray): (int, var a.T) =
yield (i, a.buffer[i])
template allocStackArray*(T: typedesc, size: int): auto =
if size < 0: raiseRangeError "allocation with a negative size"
let sz = int(size) # Evaluate size only once
if sz < 0: raiseRangeError "allocation with a negative size"
# XXX: is it possible to perform a stack size check before calling `alloca`?
# On thread init, Nim may record the base address and the capacity of the stack,
# so in theory we can verify that we still have enough room for the allocation.
# Research this.
var
bufferSize = size * sizeof(T)
bufferSize = sz * sizeof(T)
totalSize = sizeof(int32) + bufferSize
arr = cast[StackArray[T]](alloca(totalSize))
zeroMem(addr arr.buffer[0], bufferSize)
arr.bufferLen = size
arr.bufferLen = int32(sz)
arr
template toOpenArray*(a: StackArray): auto =