diff options
Diffstat (limited to 'lib/pure/memfiles.nim')
-rw-r--r-- | lib/pure/memfiles.nim | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/lib/pure/memfiles.nim b/lib/pure/memfiles.nim index ffeb0beff..24dbfb6d3 100644 --- a/lib/pure/memfiles.nim +++ b/lib/pure/memfiles.nim @@ -1,7 +1,7 @@ # # -# Nimrod's Runtime Library -# (c) Copyright 2014 Nimrod Contributors +# Nim's Runtime Library +# (c) Copyright 2014 Nim Contributors # # See the file "copying.txt", included in this # distribution, for details about the copyright. @@ -22,7 +22,7 @@ else: import os type - TMemFile* = object {.pure.} ## represents a memory mapped file + MemFile* = object ## represents a memory mapped file mem*: pointer ## a pointer to the memory mapped file. The pointer ## can be used directly to change the contents of the ## file, if it was opened with write access. @@ -34,8 +34,9 @@ type else: handle: cint +{.deprecated: [TMemFile: MemFile].} -proc mapMem*(m: var TMemFile, mode: TFileMode = fmRead, +proc mapMem*(m: var MemFile, mode: FileMode = fmRead, mappedSize = -1, offset = 0): pointer = var readonly = mode == fmRead when defined(windows): @@ -47,7 +48,7 @@ proc mapMem*(m: var TMemFile, mode: TFileMode = fmRead, if mappedSize == -1: 0 else: mappedSize, nil) if result == nil: - osError(osLastError()) + raiseOSError(osLastError()) else: assert mappedSize > 0 result = mmap( @@ -57,30 +58,30 @@ proc mapMem*(m: var TMemFile, mode: TFileMode = fmRead, if readonly: (MAP_PRIVATE or MAP_POPULATE) else: (MAP_SHARED or MAP_POPULATE), m.handle, offset) if result == cast[pointer](MAP_FAILED): - osError(osLastError()) + raiseOSError(osLastError()) -proc unmapMem*(f: var TMemFile, p: pointer, size: int) = +proc unmapMem*(f: var MemFile, p: pointer, size: int) = ## unmaps the memory region ``(p, <p+size)`` of the mapped file `f`. ## All changes are written back to the file system, if `f` was opened ## with write access. ``size`` must be of exactly the size that was requested ## via ``mapMem``. when defined(windows): - if unmapViewOfFile(p) == 0: osError(osLastError()) + if unmapViewOfFile(p) == 0: raiseOSError(osLastError()) else: - if munmap(p, size) != 0: osError(osLastError()) + if munmap(p, size) != 0: raiseOSError(osLastError()) -proc open*(filename: string, mode: TFileMode = fmRead, - mappedSize = -1, offset = 0, newFileSize = -1): TMemFile = +proc open*(filename: string, mode: FileMode = fmRead, + mappedSize = -1, offset = 0, newFileSize = -1): MemFile = ## opens a memory mapped file. If this fails, ``EOS`` is raised. ## `newFileSize` can only be set if the file does not exist and is opened ## with write access (e.g., with fmReadWrite). `mappedSize` and `offset` ## can be used to map only a slice of the file. Example: ## - ## .. code-block:: nimrod + ## .. code-block:: nim ## var - ## mm, mm_full, mm_half: TMemFile + ## mm, mm_full, mm_half: MemFile ## ## mm = memfiles.open("/tmp/test.mmap", mode = fmWrite, newFileSize = 1024) # Create a new file ## mm.close() @@ -100,11 +101,11 @@ proc open*(filename: string, mode: TFileMode = fmRead, result.size = 0 when defined(windows): - template fail(errCode: TOSErrorCode, msg: expr) = + template fail(errCode: OSErrorCode, msg: expr) = rollback() if result.fHandle != 0: discard closeHandle(result.fHandle) if result.mapHandle != 0: discard closeHandle(result.mapHandle) - osError(errCode) + raiseOSError(errCode) # return false #raise newException(EIO, msg) @@ -169,10 +170,10 @@ proc open*(filename: string, mode: TFileMode = fmRead, else: result.size = fileSize.int else: - template fail(errCode: TOSErrorCode, msg: expr) = + template fail(errCode: OSErrorCode, msg: expr) = rollback() if result.handle != 0: discard close(result.handle) - osError(errCode) + raiseOSError(errCode) var flags = if readonly: O_RDONLY else: O_RDWR @@ -214,12 +215,12 @@ proc open*(filename: string, mode: TFileMode = fmRead, if result.mem == cast[pointer](MAP_FAILED): fail(osLastError(), "file mapping failed") -proc close*(f: var TMemFile) = +proc close*(f: var MemFile) = ## closes the memory mapped file `f`. All changes are written back to the ## file system, if `f` was opened with write access. var error = false - var lastErr: TOSErrorCode + var lastErr: OSErrorCode when defined(windows): if f.fHandle != INVALID_HANDLE_VALUE: @@ -242,5 +243,5 @@ proc close*(f: var TMemFile) = else: f.handle = 0 - if error: osError(lastErr) + if error: raiseOSError(lastErr) |