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