diff --git a/lib/pure/memfiles.nim b/lib/pure/memfiles.nim index 8c404abe84..807f3da433 100644 --- a/lib/pure/memfiles.nim +++ b/lib/pure/memfiles.nim @@ -1,7 +1,7 @@ # # # Nimrod's Runtime Library -# (c) Copyright 2012 Nimrod Contributors +# (c) Copyright 2014 Nimrod Contributors # # See the file "copying.txt", included in this # distribution, for details about the copyright. @@ -34,6 +34,43 @@ type else: handle: cint + +proc mapMem*(m: var TMemFile, mode: TFileMode = fmRead, + mappedSize = -1, offset = 0): pointer = + var readonly = mode == fmRead + when defined(windows): + result = mapViewOfFileEx( + m.mapHandle, + if readonly: FILE_MAP_READ else: FILE_MAP_WRITE, + int32(offset shr 32), + int32(offset and 0xffffffff), + if mappedSize == -1: 0 else: mappedSize, + nil) + if result == nil: + osError(osLastError()) + else: + assert mappedSize > 0 + result = mmap( + nil, + mappedSize, + if readonly: PROT_READ else: PROT_READ or PROT_WRITE, + if readonly: MAP_PRIVATE else: MAP_SHARED, + m.handle, offset) + if result == cast[pointer](MAP_FAILED): + osError(osLastError()) + + +proc unmapMem*(f: var TMemFile, p: pointer, size: int) = + ## unmaps the memory region ``(p,