Fix closeHandle bug, add setFileSize, make resize work on Windows (#21375)

* 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.

(cherry picked from commit c91ef1a09f)
This commit is contained in:
c-blake
2023-02-15 11:41:28 -05:00
committed by narimiran
parent fca6a0bd6a
commit d070ccbc03
3 changed files with 66 additions and 20 deletions

View File

@@ -12,8 +12,9 @@ var
if fileExists(fn): removeFile(fn)
# Create a new file, data all zeros
mm = memfiles.open(fn, mode = fmReadWrite, newFileSize = 20)
# 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