Replace unsafeAddr by baseAddr (#32)
* Replace unsafeAddr xxx[0] by baseAddr(xxx) * Fix styleCheck:usages * Update CI conf * Fix charsToBytes * Fix outputs write * Add test
This commit is contained in:
parent
d68f64254e
commit
b42daf41d8
4 changed files with 15 additions and 15 deletions
|
|
@ -334,9 +334,7 @@ template writeByte*(span: var PageSpan, val: byte) =
|
||||||
span.startAddr = offset(span.startAddr, 1)
|
span.startAddr = offset(span.startAddr, 1)
|
||||||
|
|
||||||
template charsToBytes*(chars: openArray[char]): untyped =
|
template charsToBytes*(chars: openArray[char]): untyped =
|
||||||
bind makeOpenArray
|
chars.toOpenArrayByte(0, chars.len - 1)
|
||||||
var charsStart = unsafeAddr chars[0]
|
|
||||||
makeOpenArray(cast[ptr byte](charsStart), chars.len)
|
|
||||||
|
|
||||||
type
|
type
|
||||||
ReadFlag* = enum
|
ReadFlag* = enum
|
||||||
|
|
|
||||||
|
|
@ -481,7 +481,7 @@ func memoryInput*(data: openArray[byte]): InputStreamHandle =
|
||||||
page = buffers.addWritablePage(data.len)
|
page = buffers.addWritablePage(data.len)
|
||||||
pageSpan = page.fullSpan
|
pageSpan = page.fullSpan
|
||||||
|
|
||||||
copyMem(pageSpan.startAddr, unsafeAddr data[0], data.len)
|
copyMem(pageSpan.startAddr, baseAddr(data), data.len)
|
||||||
|
|
||||||
InputStream(buffers: buffers,
|
InputStream(buffers: buffers,
|
||||||
span: pageSpan,
|
span: pageSpan,
|
||||||
|
|
|
||||||
|
|
@ -532,7 +532,7 @@ proc finalize*(cursor: var WriteCursor) =
|
||||||
|
|
||||||
proc finalWrite*(cursor: var WriteCursor, data: openArray[byte]) =
|
proc finalWrite*(cursor: var WriteCursor, data: openArray[byte]) =
|
||||||
fsAssert data.len == cursor.span.len
|
fsAssert data.len == cursor.span.len
|
||||||
copyMem(cursor.span.startAddr, unsafeAddr data[0], data.len)
|
copyMem(cursor.span.startAddr, baseAddr(data), data.len)
|
||||||
finalize cursor
|
finalize cursor
|
||||||
|
|
||||||
proc finalWrite*(c: var VarSizeWriteCursor, data: openArray[byte]) =
|
proc finalWrite*(c: var VarSizeWriteCursor, data: openArray[byte]) =
|
||||||
|
|
@ -548,14 +548,14 @@ proc finalWrite*(c: var VarSizeWriteCursor, data: openArray[byte]) =
|
||||||
if cursor.span.startAddr == baseAddr:
|
if cursor.span.startAddr == baseAddr:
|
||||||
# This is page starting cursor
|
# This is page starting cursor
|
||||||
page.consumedTo = overestimatedBytes
|
page.consumedTo = overestimatedBytes
|
||||||
copyMem(offset(baseAddr, overestimatedBytes), unsafeAddr data[0], data.len)
|
copyMem(offset(baseAddr, overestimatedBytes), baseAddr(data), data.len)
|
||||||
finalize cursor
|
finalize cursor
|
||||||
return
|
return
|
||||||
|
|
||||||
if page.readableEnd == cursor.span.endAddr:
|
if page.readableEnd == cursor.span.endAddr:
|
||||||
# This is a page ending cursor
|
# This is a page ending cursor
|
||||||
page.writtenTo = distance(baseAddr, cursor.span.startAddr) + data.len
|
page.writtenTo = distance(baseAddr, cursor.span.startAddr) + data.len
|
||||||
copyMem(cursor.span.startAddr, unsafeAddr data[0], data.len)
|
copyMem(cursor.span.startAddr, baseAddr(data), data.len)
|
||||||
finalize cursor
|
finalize cursor
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
@ -671,7 +671,7 @@ template write*(s: OutputStream|var WriteCursor, x: char) =
|
||||||
proc writeToANewPage(s: OutputStream, bytes: openArray[byte]) =
|
proc writeToANewPage(s: OutputStream, bytes: openArray[byte]) =
|
||||||
var
|
var
|
||||||
runway = s.span.len
|
runway = s.span.len
|
||||||
inputPos = unsafeAddr bytes[0]
|
inputPos = baseAddr(bytes)
|
||||||
inputLen = bytes.len
|
inputLen = bytes.len
|
||||||
|
|
||||||
template reduceInput(delta: int) =
|
template reduceInput(delta: int) =
|
||||||
|
|
@ -704,7 +704,7 @@ template writeBytesImpl(s: OutputStream,
|
||||||
# page is full:
|
# page is full:
|
||||||
let runway = s.span.len
|
let runway = s.span.len
|
||||||
if inputLen <= runway:
|
if inputLen <= runway:
|
||||||
copyMem(s.span.startAddr, unsafeAddr bytes[0], inputLen)
|
copyMem(s.span.startAddr, baseAddr(bytes), inputLen)
|
||||||
s.span.startAddr = offset(s.span.startAddr, inputLen)
|
s.span.startAddr = offset(s.span.startAddr, inputLen)
|
||||||
elif s.vtable == nil or s.extCursorsCount > 0:
|
elif s.vtable == nil or s.extCursorsCount > 0:
|
||||||
# We are not ready to flush, so we must create pending pages.
|
# We are not ready to flush, so we must create pending pages.
|
||||||
|
|
@ -716,7 +716,7 @@ template writeBytesImpl(s: OutputStream,
|
||||||
|
|
||||||
proc write*(s: OutputStream, bytes: openArray[byte]) =
|
proc write*(s: OutputStream, bytes: openArray[byte]) =
|
||||||
writeBytesImpl(s, bytes):
|
writeBytesImpl(s, bytes):
|
||||||
drainAllBuffersSync(s, unsafeAddr bytes[0], bytes.len)
|
drainAllBuffersSync(s, baseAddr(bytes), bytes.len)
|
||||||
|
|
||||||
proc write*(s: OutputStream, chars: openArray[char]) =
|
proc write*(s: OutputStream, chars: openArray[char]) =
|
||||||
write s, charsToBytes(chars)
|
write s, charsToBytes(chars)
|
||||||
|
|
@ -742,7 +742,7 @@ when fsAsyncSupport:
|
||||||
bytes: openArray[byte]): Future[void] =
|
bytes: openArray[byte]): Future[void] =
|
||||||
let s = sp
|
let s = sp
|
||||||
writeBytesImpl(s, bytes):
|
writeBytesImpl(s, bytes):
|
||||||
return s.vtable.writeAsync(s, unsafeAddr bytes[0], bytes.len)
|
return s.vtable.writeAsync(s, baseAddr(bytes), bytes.len)
|
||||||
|
|
||||||
proc writeBytesAsyncImpl(s: OutputStream,
|
proc writeBytesAsyncImpl(s: OutputStream,
|
||||||
chars: openArray[char]): Future[void] =
|
chars: openArray[char]): Future[void] =
|
||||||
|
|
@ -774,7 +774,7 @@ when fsAsyncSupport:
|
||||||
proc writeBytesToCursor(c: var WriteCursor, bytes: openArray[byte]) =
|
proc writeBytesToCursor(c: var WriteCursor, bytes: openArray[byte]) =
|
||||||
var
|
var
|
||||||
runway = c.span.len
|
runway = c.span.len
|
||||||
inputPos = unsafeAddr bytes[0]
|
inputPos = baseAddr(bytes)
|
||||||
inputLen = bytes.len
|
inputLen = bytes.len
|
||||||
|
|
||||||
template reduceInput(delta: int) =
|
template reduceInput(delta: int) =
|
||||||
|
|
@ -787,7 +787,7 @@ proc writeBytesToCursor(c: var WriteCursor, bytes: openArray[byte]) =
|
||||||
else:
|
else:
|
||||||
# This must be a split cursor. We need to complete its first page first,
|
# This must be a split cursor. We need to complete its first page first,
|
||||||
# then switch to the second and continue the write there.
|
# then switch to the second and continue the write there.
|
||||||
copyMem(c.span.startAddr, unsafeAddr bytes[0], runway)
|
copyMem(c.span.startAddr, baseAddr(bytes), runway)
|
||||||
reduceInput runway
|
reduceInput runway
|
||||||
# If this really is a split cursor, the following operation will succeed.
|
# If this really is a split cursor, the following operation will succeed.
|
||||||
# Otherwise, it will Defect and the conclusion is that this was a write
|
# Otherwise, it will Defect and the conclusion is that this was a write
|
||||||
|
|
@ -805,8 +805,7 @@ template write*(c: var WriteCursor, bytes: openArray[byte]) =
|
||||||
writeBytesToCursor(c, bytes)
|
writeBytesToCursor(c, bytes)
|
||||||
|
|
||||||
proc write*(c: var WriteCursor, chars: openArray[char]) {.inline.} =
|
proc write*(c: var WriteCursor, chars: openArray[char]) {.inline.} =
|
||||||
var charsStart = unsafeAddr chars[0]
|
writeBytesToCursor(c, chars.toOpenArrayByte(0, chars.len - 1))
|
||||||
writeBytesToCursor(c, makeOpenArray(cast[ptr byte](charsStart), chars.len))
|
|
||||||
|
|
||||||
proc writeMemCopy*[T](c: var WriteCursor, value: T) =
|
proc writeMemCopy*[T](c: var WriteCursor, value: T) =
|
||||||
writeBytesToCursor(c, memCopyToBytes(value))
|
writeBytesToCursor(c, memCopyToBytes(value))
|
||||||
|
|
|
||||||
|
|
@ -181,9 +181,11 @@ suite "output stream":
|
||||||
test "delayed write":
|
test "delayed write":
|
||||||
output "initial output\n"
|
output "initial output\n"
|
||||||
const delayedWriteContent = bytes "delayed write\n"
|
const delayedWriteContent = bytes "delayed write\n"
|
||||||
|
let memStream2 = memoryOutput()
|
||||||
|
|
||||||
var memCursor = memStream.delayFixedSizeWrite(delayedWriteContent.len)
|
var memCursor = memStream.delayFixedSizeWrite(delayedWriteContent.len)
|
||||||
var fileCursor = fileStream.delayVarSizeWrite(delayedWriteContent.len + 50)
|
var fileCursor = fileStream.delayVarSizeWrite(delayedWriteContent.len + 50)
|
||||||
|
var memCursor2 = memStream2.delayVarSizeWrite(10)
|
||||||
|
|
||||||
let cursorStart = memStream.pos
|
let cursorStart = memStream.pos
|
||||||
|
|
||||||
|
|
@ -197,6 +199,7 @@ suite "output stream":
|
||||||
|
|
||||||
memCursor.finalWrite delayedWriteContent
|
memCursor.finalWrite delayedWriteContent
|
||||||
fileCursor.finalWrite delayedWriteContent
|
fileCursor.finalWrite delayedWriteContent
|
||||||
|
memCursor2.finalWrite []
|
||||||
|
|
||||||
checkOutputsMatch(skipUnbufferedFile = true)
|
checkOutputsMatch(skipUnbufferedFile = true)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue