Differences From
Artifact [fc7770c3f7]:
1 1 -- vim: ft=terra
2 2 -- TODO: add support for windows IO calls
3 3 local handle_type = int
4 4 local posix = terralib.includec 'fcntl.h'
5 5 local unistd = terralib.includec 'unistd.h'
6 +local mm = terralib.includec 'sys/mman.h'
6 7
7 8 struct file {
8 9 handle: handle_type
9 10 read: bool
10 11 write: bool
11 12 }
12 13
................................................................................
61 62 elseif wh == [file.seek.eof] then
62 63 whence = unistd.SEEK_END
63 64 else lib.bail('invalid seek mode') end
64 65
65 66 return unistd.lseek(self.handle, ofs, whence)
66 67 end;
67 68 }
69 +
70 +terra file:len(): intptr
71 + var cur = self:seek(0, [file.seek.ofs])
72 + var sz = self:seek(0, [file.seek.eof])
73 + self:seek(cur, [file.seek.abs])
74 + return sz
75 +end
76 +
77 +local struct mapping {
78 + addr: &opaque
79 + sz: intptr
80 +}
81 +terra mapping:unmap()
82 + lib.dbg('releasing file mapping')
83 + return mm.munmap(self.addr, self.sz)
84 +end
85 +-- provide for syncing mechanism?
86 +
87 +terra file:mapin(ofs: intptr, sz: intptr)
88 + var prot = 0
89 + if self.read then prot = mm.PROT_READ end
90 + if self.write then prot = prot or mm.PROT_WRITE end
91 + if sz == 0 then sz = self:len() end
92 + lib.dbg('mapping file into memory')
93 + return mapping {
94 + addr = mm.mmap(nil, sz, prot, mm.MAP_PRIVATE, self.handle, ofs);
95 + sz = sz;
96 + }
97 +end
68 98
69 99 return file