From e9f50bc8478b261c734f704d3aac3853bcbe8a30 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Wed, 6 May 2020 15:09:01 +0300 Subject: [PATCH] Bugfix: Fix an invalid Defect detection when a cursor is creater right at a page boundary --- faststreams/outputs.nim | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/faststreams/outputs.nim b/faststreams/outputs.nim index 7c6cbdf..75f4113 100644 --- a/faststreams/outputs.nim +++ b/faststreams/outputs.nim @@ -331,15 +331,31 @@ proc createCursor(s: OutputStream, size: int): WriteCursor = # of the newly created cursor: s.span.startAddr = endAddr -proc delayFixedSizeWrite*(s: OutputStream, size: Natural): WriteCursor = +proc delayFixedSizeWrite*(s: OutputStream, cursorSize: Natural): WriteCursor = let runway = s.span.len - if size <= runway: - result = createCursor(s, size) + if cursorSize <= runway: + result = createCursor(s, cursorSize) + elif runway == 0: + # This is a special case of requesting a cursor right at the page boundary. + # We can safely create a non-split cursor on a new page: + let + nextPageSize = nextAlignedSize(cursorSize, s.buffers.pageSize) + nextPage = s.buffers.addWritablePage(nextPageSize) + nextPageSpan = nextPage.fullSpan + cursorEndAddr = offset(nextPageSpan.startAddr, cursorSize) + + inc s.extCursorsCount + result = WriteCursor(stream: s, + span: PageSpan(startAddr: nextPageSpan.startAddr, + endAddr: cursorEndAddr)) + + s.span = PageSpan(startAddr: cursorEndAddr, endAddr: nextPageSpan.endAddr) + s.spanEndPos += nextPageSize else: result = createCursor(s, runway) let - runwayDeficit = size - runway + runwayDeficit = cursorSize - runway nextPageSize = nextAlignedSize(runwayDeficit, s.buffers.pageSize) nextPage = s.buffers.addWritablePage(nextPageSize) nextPageSpan = nextPage.fullSpan @@ -591,7 +607,6 @@ template memCopyToBytes(value: auto): untyped = makeOpenArray(cast[ptr byte](valueAddr), sizeof(T)) proc writeMemCopy*(s: OutputStream, value: auto) = - bind write write s, memCopyToBytes(value) proc writeBytesAsyncImpl(sp: OutputStream, @@ -664,7 +679,6 @@ proc write*(c: var WriteCursor, chars: openarray[char]) {.inline.} = writeBytesToCursor(c, makeOpenArray(cast[ptr byte](charsStart), chars.len)) proc writeMemCopy*[T](c: var WriteCursor, value: T) = - bind writeBytesToCursor writeBytesToCursor(c, memCopyToBytes(value)) proc write*(c: var WriteCursor, str: string) =