alloc_unix.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //go:build unix
  2. package alloc
  3. import (
  4. "math"
  5. "github.com/tetratelabs/wazero/experimental"
  6. "golang.org/x/sys/unix"
  7. )
  8. func NewMemory(_, max uint64) experimental.LinearMemory {
  9. // Round up to the page size.
  10. rnd := uint64(unix.Getpagesize() - 1)
  11. max = (max + rnd) &^ rnd
  12. if max > math.MaxInt {
  13. // This ensures int(max) overflows to a negative value,
  14. // and unix.Mmap returns EINVAL.
  15. max = math.MaxUint64
  16. }
  17. // Reserve max bytes of address space, to ensure we won't need to move it.
  18. // A protected, private, anonymous mapping should not commit memory.
  19. b, err := unix.Mmap(-1, 0, int(max), unix.PROT_NONE, unix.MAP_PRIVATE|unix.MAP_ANON)
  20. if err != nil {
  21. panic(err)
  22. }
  23. return &mmappedMemory{buf: b[:0]}
  24. }
  25. // The slice covers the entire mmapped memory:
  26. // - len(buf) is the already committed memory,
  27. // - cap(buf) is the reserved address space.
  28. type mmappedMemory struct {
  29. buf []byte
  30. }
  31. func (m *mmappedMemory) Reallocate(size uint64) []byte {
  32. com := uint64(len(m.buf))
  33. res := uint64(cap(m.buf))
  34. if com < size && size <= res {
  35. // Round up to the page size.
  36. rnd := uint64(unix.Getpagesize() - 1)
  37. new := (size + rnd) &^ rnd
  38. // Commit additional memory up to new bytes.
  39. err := unix.Mprotect(m.buf[com:new], unix.PROT_READ|unix.PROT_WRITE)
  40. if err != nil {
  41. return nil
  42. }
  43. // Update committed memory.
  44. m.buf = m.buf[:new]
  45. }
  46. // Limit returned capacity because bytes beyond
  47. // len(m.buf) have not yet been committed.
  48. return m.buf[:size:len(m.buf)]
  49. }
  50. func (m *mmappedMemory) Free() {
  51. err := unix.Munmap(m.buf[:cap(m.buf)])
  52. if err != nil {
  53. panic(err)
  54. }
  55. m.buf = nil
  56. }