Skip to content

Commit

Permalink
Replace unsafeAddr by baseAddr (#32)
Browse files Browse the repository at this point in the history
* Replace unsafeAddr xxx[0] by baseAddr(xxx)

* Fix styleCheck:usages

* Update CI conf

* Fix charsToBytes

* Fix outputs write

* Add test
  • Loading branch information
lchenut committed Nov 24, 2022
1 parent d68f642 commit b42daf4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 1 addition & 3 deletions faststreams/buffers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,7 @@ template writeByte*(span: var PageSpan, val: byte) =
span.startAddr = offset(span.startAddr, 1)

template charsToBytes*(chars: openArray[char]): untyped =
bind makeOpenArray
var charsStart = unsafeAddr chars[0]
makeOpenArray(cast[ptr byte](charsStart), chars.len)
chars.toOpenArrayByte(0, chars.len - 1)

type
ReadFlag* = enum
Expand Down
2 changes: 1 addition & 1 deletion faststreams/inputs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ func memoryInput*(data: openArray[byte]): InputStreamHandle =
page = buffers.addWritablePage(data.len)
pageSpan = page.fullSpan

copyMem(pageSpan.startAddr, unsafeAddr data[0], data.len)
copyMem(pageSpan.startAddr, baseAddr(data), data.len)

InputStream(buffers: buffers,
span: pageSpan,
Expand Down
21 changes: 10 additions & 11 deletions faststreams/outputs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ proc finalize*(cursor: var WriteCursor) =

proc finalWrite*(cursor: var WriteCursor, data: openArray[byte]) =
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

proc finalWrite*(c: var VarSizeWriteCursor, data: openArray[byte]) =
Expand All @@ -548,14 +548,14 @@ proc finalWrite*(c: var VarSizeWriteCursor, data: openArray[byte]) =
if cursor.span.startAddr == baseAddr:
# This is page starting cursor
page.consumedTo = overestimatedBytes
copyMem(offset(baseAddr, overestimatedBytes), unsafeAddr data[0], data.len)
copyMem(offset(baseAddr, overestimatedBytes), baseAddr(data), data.len)
finalize cursor
return

if page.readableEnd == cursor.span.endAddr:
# This is a page ending cursor
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
return

Expand Down Expand Up @@ -671,7 +671,7 @@ template write*(s: OutputStream|var WriteCursor, x: char) =
proc writeToANewPage(s: OutputStream, bytes: openArray[byte]) =
var
runway = s.span.len
inputPos = unsafeAddr bytes[0]
inputPos = baseAddr(bytes)
inputLen = bytes.len

template reduceInput(delta: int) =
Expand Down Expand Up @@ -704,7 +704,7 @@ template writeBytesImpl(s: OutputStream,
# page is full:
let runway = s.span.len
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)
elif s.vtable == nil or s.extCursorsCount > 0:
# We are not ready to flush, so we must create pending pages.
Expand All @@ -716,7 +716,7 @@ template writeBytesImpl(s: OutputStream,

proc write*(s: OutputStream, bytes: openArray[byte]) =
writeBytesImpl(s, bytes):
drainAllBuffersSync(s, unsafeAddr bytes[0], bytes.len)
drainAllBuffersSync(s, baseAddr(bytes), bytes.len)

proc write*(s: OutputStream, chars: openArray[char]) =
write s, charsToBytes(chars)
Expand All @@ -742,7 +742,7 @@ when fsAsyncSupport:
bytes: openArray[byte]): Future[void] =
let s = sp
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,
chars: openArray[char]): Future[void] =
Expand Down Expand Up @@ -774,7 +774,7 @@ when fsAsyncSupport:
proc writeBytesToCursor(c: var WriteCursor, bytes: openArray[byte]) =
var
runway = c.span.len
inputPos = unsafeAddr bytes[0]
inputPos = baseAddr(bytes)
inputLen = bytes.len

template reduceInput(delta: int) =
Expand All @@ -787,7 +787,7 @@ proc writeBytesToCursor(c: var WriteCursor, bytes: openArray[byte]) =
else:
# This must be a split cursor. We need to complete its first page first,
# 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
# 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
Expand All @@ -805,8 +805,7 @@ template write*(c: var WriteCursor, bytes: openArray[byte]) =
writeBytesToCursor(c, bytes)

proc write*(c: var WriteCursor, chars: openArray[char]) {.inline.} =
var charsStart = unsafeAddr chars[0]
writeBytesToCursor(c, makeOpenArray(cast[ptr byte](charsStart), chars.len))
writeBytesToCursor(c, chars.toOpenArrayByte(0, chars.len - 1))

proc writeMemCopy*[T](c: var WriteCursor, value: T) =
writeBytesToCursor(c, memCopyToBytes(value))
Expand Down
3 changes: 3 additions & 0 deletions tests/test_outputs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ suite "output stream":
test "delayed write":
output "initial output\n"
const delayedWriteContent = bytes "delayed write\n"
let memStream2 = memoryOutput()

var memCursor = memStream.delayFixedSizeWrite(delayedWriteContent.len)
var fileCursor = fileStream.delayVarSizeWrite(delayedWriteContent.len + 50)
var memCursor2 = memStream2.delayVarSizeWrite(10)

let cursorStart = memStream.pos

Expand All @@ -197,6 +199,7 @@ suite "output stream":

memCursor.finalWrite delayedWriteContent
fileCursor.finalWrite delayedWriteContent
memCursor2.finalWrite []

checkOutputsMatch(skipUnbufferedFile = true)

Expand Down

0 comments on commit b42daf4

Please sign in to comment.