diff options
author | Araq <rumpf_a@web.de> | 2018-12-12 12:10:17 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2018-12-12 12:10:17 +0100 |
commit | f828e5da491538b35cbd366ab67fdb347f96494e (patch) | |
tree | 7af9c4de0a8e0ffff8791dcff3db5988c9961d15 /lib | |
parent | 77a884f178316155d348808c2e3255a17edef16b (diff) | |
download | Nim-f828e5da491538b35cbd366ab67fdb347f96494e.tar.gz |
clean up the implementation of the new memfiles.resize
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/memfiles.nim | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/lib/pure/memfiles.nim b/lib/pure/memfiles.nim index f04cf8fee..810223d72 100644 --- a/lib/pure/memfiles.nim +++ b/lib/pure/memfiles.nim @@ -282,34 +282,34 @@ proc flush*(f: var MemFile; attempts: Natural = 3) = if lastErr != EBUSY.OSErrorCode: raiseOSError(lastErr, "error flushing mapping") -proc resize*(f: var MemFile, newFileSize: int) = - ## resize and re-map the file underlying an ``allowRemap MemFile``. NOTE: - ## this assumes the entire file is mapped read-write at offset zero. Also, - ## the value of ``.mem`` will probably change. - when defined(windows): - discard #TODO This needs to be implemented. - else: - if f.handle == -1: - raise newException(IOError, - "Cannot resize MemFile opened with allowRemap=false") - if ftruncate(f.handle, newFileSize) == -1: - raiseOSError(osLastError()) - when defined(linux): #Maybe NetBSD, too? - #On Linux this can be over 100 times faster than a munmap,mmap cycle. - proc mremap(old: pointer; oldSize,newSize: csize; flags: cint): pointer {. - importc: "mremap", header: "<sys/mman.h>" .} - let newAddr = mremap(f.mem, csize(f.size), csize(newFileSize), cint(1)) - if newAddr == cast[pointer](MAP_FAILED): +when defined(posix) or defined(nimdoc): + proc resize*(f: var MemFile, newFileSize: int) {.raises: [IOError, OSError].} = + ## resize and re-map the file underlying an ``allowRemap MemFile``. + ## **Note**: this assumes the entire file is mapped read-write at offset zero. + ## Also, the value of ``.mem`` will probably change. + ## **Note**: This is not (yet) avaiable on Windows. + when defined(posix): + if f.handle == -1: + raise newException(IOError, + "Cannot resize MemFile opened with allowRemap=false") + if ftruncate(f.handle, newFileSize) == -1: raiseOSError(osLastError()) - else: - if munmap(f.mem, f.size) != 0: - raiseOSError(osLastError()) - let newAddr = mmap(nil, newFileSize, PROT_READ or PROT_WRITE, - MAP_SHARED or MAP_POPULATE, f.handle, 0) - if newAddr == cast[pointer](MAP_FAILED): - raiseOSError(osLastError()) - f.mem = newAddr - f.size = newFileSize + when defined(linux): #Maybe NetBSD, too? + #On Linux this can be over 100 times faster than a munmap,mmap cycle. + proc mremap(old: pointer; oldSize,newSize: csize; flags: cint): pointer {. + importc: "mremap", header: "<sys/mman.h>" .} + let newAddr = mremap(f.mem, csize(f.size), csize(newFileSize), cint(1)) + if newAddr == cast[pointer](MAP_FAILED): + raiseOSError(osLastError()) + else: + if munmap(f.mem, f.size) != 0: + raiseOSError(osLastError()) + let newAddr = mmap(nil, newFileSize, PROT_READ or PROT_WRITE, + MAP_SHARED or MAP_POPULATE, f.handle, 0) + if newAddr == cast[pointer](MAP_FAILED): + raiseOSError(osLastError()) + f.mem = newAddr + f.size = newFileSize proc close*(f: var MemFile) = ## closes the memory mapped file `f`. All changes are written back to the |