mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
* Add general purpose `setFileSize` (unexported for now). Use to simplify `memfiles.open` as well as make robust (via hard allocation, not merely `ftruncate` address space allocation) on systems with `posix_fallocate`. As part of this, fix a bad `closeHandle` return check bug on Windows and add `MemFile.resize` for Windows now that setFileSize makes that easier. * Adapt existing test to exercise newly portable `MemFile.resize`. * Since Apple has never provided `posix_fallocate`, provide a fallback. This is presently written in terms of `ftruncate`, but it can be improved to use `F_PREALLOCATE` instead, as mentioned in a comment.
44 lines
985 B
Nim
44 lines
985 B
Nim
discard """
|
|
disabled: "Windows"
|
|
output: '''Full read size: 20
|
|
Half read size: 10 Data: Hello'''
|
|
"""
|
|
import memfiles, os
|
|
import std/syncio
|
|
|
|
|
|
const
|
|
fn = "test.mmap"
|
|
var
|
|
mm, mm_full, mm_half: MemFile
|
|
p: pointer
|
|
|
|
if fileExists(fn): removeFile(fn)
|
|
|
|
# Create a new file, data all zeros, starting at size 10
|
|
mm = memfiles.open(fn, mode = fmReadWrite, newFileSize = 10, allowRemap=true)
|
|
mm.resize 20 # resize up to 20
|
|
mm.close()
|
|
|
|
# read, change
|
|
mm_full = memfiles.open(fn, mode = fmWrite, mappedSize = -1, allowRemap = true)
|
|
let size = mm_full.size
|
|
p = mm_full.mapMem(fmReadWrite, 20, 0)
|
|
echo "Full read size: ", size
|
|
var p2 = cast[cstring](p)
|
|
p2[0] = 'H'
|
|
p2[1] = 'e'
|
|
p2[2] = 'l'
|
|
p2[3] = 'l'
|
|
p2[4] = 'o'
|
|
p2[5] = '\0'
|
|
mm_full.unmapMem(p, 20)
|
|
mm_full.close()
|
|
|
|
# read half, and verify data change
|
|
mm_half = memfiles.open(fn, mode = fmRead, mappedSize = 10)
|
|
echo "Half read size: ", mm_half.size, " Data: ", cast[cstring](mm_half.mem)
|
|
mm_half.close()
|
|
|
|
if fileExists(fn): removeFile(fn)
|