tmemfiles2.nim 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. discard """
  2. disabled: "Windows"
  3. output: '''Full read size: 20
  4. Half read size: 10 Data: Hello'''
  5. """
  6. import memfiles, os
  7. import std/syncio
  8. const
  9. fn = "test.mmap"
  10. var
  11. mm, mm_full, mm_half: MemFile
  12. p: pointer
  13. if fileExists(fn): removeFile(fn)
  14. # Create a new file, data all zeros, starting at size 10
  15. mm = memfiles.open(fn, mode = fmReadWrite, newFileSize = 10, allowRemap=true)
  16. mm.resize 20 # resize up to 20
  17. mm.close()
  18. # read, change
  19. mm_full = memfiles.open(fn, mode = fmWrite, mappedSize = -1, allowRemap = true)
  20. let size = mm_full.size
  21. p = mm_full.mapMem(fmReadWrite, 20, 0)
  22. echo "Full read size: ", size
  23. var p2 = cast[cstring](p)
  24. p2[0] = 'H'
  25. p2[1] = 'e'
  26. p2[2] = 'l'
  27. p2[3] = 'l'
  28. p2[4] = 'o'
  29. p2[5] = '\0'
  30. mm_full.unmapMem(p, 20)
  31. mm_full.close()
  32. # read half, and verify data change
  33. mm_half = memfiles.open(fn, mode = fmRead, mappedSize = 10)
  34. echo "Half read size: ", mm_half.size, " Data: ", cast[cstring](mm_half.mem)
  35. mm_half.close()
  36. if fileExists(fn): removeFile(fn)