Skip to content

Commit 95c3f79

Browse files
committed
Add generated list of flags to each flag doc string
1 parent 3c0247a commit 95c3f79

File tree

3 files changed

+161
-114
lines changed

3 files changed

+161
-114
lines changed

src/UnixMmap.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module UnixMmap
22
@static if Sys.isunix()
33
include("staticexpand.jl")
4+
include("consts.jl")
45
include("mmap.jl")
56
end
67
end

src/consts.jl

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
using BitFlags
2+
import .Sys # explicitly imported to allow docs generation to override
3+
4+
@bitflag MmapProtection::Cuint begin
5+
PROT_NONE = 0x00
6+
PROT_READ = 0x01
7+
PROT_WRITE = 0x02
8+
PROT_EXEC = 0x04
9+
end
10+
Base.cconvert(::Type{T}, pf::MmapProtection) where {T <: Integer} = T(pf)
11+
12+
@staticexpand @bitflag MmapFlags::Cuint begin
13+
MAP_FILE = 0x00
14+
MAP_SHARED = 0x01
15+
MAP_PRIVATE = 0x02
16+
MAP_FIXED = 0x0010
17+
MAP_ANONYMOUS = @static Sys.isbsd() ? 0x1000 : 0x0020
18+
@static if Sys.islinux()
19+
MAP_32BIT = 0x0040
20+
MAP_GROWSDOWN = 0x0100
21+
MAP_DENYWRITE = 0x0800
22+
MAP_EXECUTABLE = 0x1000
23+
MAP_LOCKED = 0x2000
24+
MAP_NORESERVE = 0x4000
25+
MAP_POPULATE = 0x8000
26+
MAP_NONBLOCK = 0x1_0000
27+
MAP_STACK = 0x2_0000
28+
MAP_HUGETLB = 0x4_0000
29+
MAP_SYNC = 0x8_0000
30+
MAP_FIXED_NOREPLACE = 0x10_0000
31+
end
32+
@static if Sys.isapple() || (VERSION >= v"1.1" && (Sys.isnetbsd() || Sys.isdragonfly()))
33+
MAP_RENAME = 0x0020
34+
MAP_NORESERVE = 0x0040
35+
MAP_INHERIT = 0x0080
36+
MAP_NOEXTEND = 0x0100
37+
MAP_HASSEMAPHORE = 0x0200
38+
end
39+
@static if VERSION >= v"1.1" && Sys.isfreebsd()
40+
MAP_STACK = 0x0400
41+
MAP_NOSYNC = 0x0800
42+
MAP_GUARD = 0x2000
43+
MAP_EXCL = 0x4000
44+
MAP_NOCORE = 0x4_0000
45+
MAP_32BIT = 0x8_0000
46+
elseif VERSION >= v"1.1" && Sys.isdragonfly()
47+
MAP_STACK = 0x0400
48+
MAP_NOSYNC = 0x0800
49+
MAP_VPAGETABLE = 0x2000
50+
MAP_TRYFIXED = 0x1_0000
51+
MAP_NOCORE = 0x2_0000
52+
MAP_SIZEALIGN = 0x4_0000
53+
elseif VERSION >= v"1.1" && Sys.isopenbsd()
54+
MAP_STACK = 0x4000
55+
MAP_CONCEAL = 0x8000
56+
elseif VERSION >= v"1.1" && Sys.isnetbsd()
57+
MAP_REMAPDUP = 0x0004
58+
MAP_TRYFIXED = 0x0400
59+
MAP_WIRED = 0x0800
60+
MAP_STACK = 0x2000
61+
end
62+
end
63+
Base.cconvert(::Type{T}, mf::MmapFlags) where {T <: Integer} = T(mf)
64+
65+
# Provide this weird mode?
66+
#@static if Sys.islinux()
67+
#const MAP_SHARED_VALIDATE = MmapFlags(0x03)
68+
#end
69+
70+
@staticexpand @enum AdviseFlags::Cint begin
71+
MADV_NORMAL = 0
72+
MADV_RANDOM = 1
73+
MADV_SEQUENTIAL = 2
74+
MADV_WILLNEED = 3
75+
MADV_DONTNEED = 4
76+
@static if Sys.islinux()
77+
MADV_FREE = 8
78+
MADV_REMOVE = 9
79+
MADV_DONTFORK = 10
80+
MADV_DOFORK = 11
81+
MADV_MERGEABLE = 12
82+
MADV_UNMERGEABLE = 13
83+
MADV_HUGEPAGE = 14
84+
MADV_NOHUGEPAGE = 15
85+
MADV_DONTDUMP = 16
86+
MADV_DODUMP = 17
87+
MADV_WIPEONFORK = 18
88+
MADV_KEEPONFORK = 19
89+
MADV_COLD = 20
90+
MADV_PAGEOUT = 21
91+
MADV_HWPOISON = 100
92+
MADV_SOFT_OFFLINE = 101
93+
elseif Sys.isapple()
94+
MADV_FREE = 5
95+
elseif VERSION >= v"1.1" && (Sys.isfreebsd() || Sys.isdragonfly())
96+
MADV_FREE = 5
97+
MADV_NOSYNC = 6
98+
MADV_AUTOSYNC = 7
99+
MADV_NOCORE = 8
100+
MADV_CORE = 9
101+
@static if Sys.isfreebsd()
102+
MADV_PROTECT = 10
103+
else
104+
MADV_INVAL = 10
105+
MADV_SETMAP = 11
106+
end
107+
elseif VERSION >= v"1.1" && (Sys.isopenbsd() || Sys.isnetbsd())
108+
MADV_SPACEAVAIL = 5
109+
MADV_FREE = 6
110+
end
111+
end
112+
Base.cconvert(::Type{T}, af::AdviseFlags) where {T <: Integer} = T(af)
113+
114+
# documentation requires the types to be defined first, so do after the fact
115+
let _flag_docs
116+
function _flag_docs(T)
117+
buf = IOBuffer()
118+
for c in instances(T)
119+
println(buf, c)
120+
end
121+
return rstrip(String(take!(buf)))
122+
end
123+
124+
@doc """
125+
@bitflag UnixMmap.MmapProtection
126+
127+
Set of bit flags which control the memory protections applied to the memory mapped
128+
region. The flag should be either `PROT_NONE` or some bitwise-or combination of
129+
the remaining flags.
130+
131+
The flags available on $(Sys.KERNEL) are:
132+
```julia
133+
$(_flag_docs(MmapProtection))
134+
```
135+
""" MmapProtection
136+
137+
@doc """
138+
@bitflag UnixMmap.MmapFlags
139+
140+
Set of bit flags which control the handling of the memory mappged region. The flag
141+
should be a bitwise-or combination of flags.
142+
143+
The flags available on $(Sys.KERNEL) are:
144+
```julia
145+
$(_flag_docs(MmapFlags))
146+
```
147+
""" MmapFlags
148+
149+
@doc """
150+
@enum UnixMmap.AdviseFlags
151+
152+
Set of flags which advise the kernel on handling of the memory mapped region.
153+
154+
The flags available on $(Sys.KERNEL) are:
155+
```julia
156+
$(_flag_docs(AdviseFlags))
157+
```
158+
""" AdviseFlags
159+
end

src/mmap.jl

Lines changed: 1 addition & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,7 @@
1-
using BitFlags
2-
31
import Base: INVALID_OS_HANDLE
42
import Mmap
5-
const PAGESIZE = Mmap.PAGESIZE
63

7-
### Flags
8-
9-
@bitflag MmapProtection::Cuint begin
10-
PROT_NONE = 0x00
11-
PROT_READ = 0x01
12-
PROT_WRITE = 0x02
13-
PROT_EXEC = 0x04
14-
end
15-
Base.cconvert(::Type{T}, pf::MmapProtection) where {T <: Integer} = T(pf)
16-
17-
@staticexpand @bitflag MmapFlags::Cuint begin
18-
MAP_FILE = 0x00
19-
MAP_SHARED = 0x01
20-
MAP_PRIVATE = 0x02
21-
MAP_FIXED = 0x0010
22-
MAP_ANONYMOUS = @static Sys.isbsd() ? 0x1000 : 0x0020
23-
@static if Sys.islinux()
24-
MAP_32BIT = 0x0040
25-
MAP_GROWSDOWN = 0x0100
26-
MAP_DENYWRITE = 0x0800
27-
MAP_EXECUTABLE = 0x1000
28-
MAP_LOCKED = 0x2000
29-
MAP_NORESERVE = 0x4000
30-
MAP_POPULATE = 0x8000
31-
MAP_NONBLOCK = 0x1_0000
32-
MAP_STACK = 0x2_0000
33-
MAP_HUGETLB = 0x4_0000
34-
MAP_SYNC = 0x8_0000
35-
MAP_FIXED_NOREPLACE = 0x10_0000
36-
end
37-
@static if Sys.isapple() || (VERSION >= v"1.1" && (Sys.isnetbsd() || Sys.isdragonfly()))
38-
MAP_RENAME = 0x0020
39-
MAP_NORESERVE = 0x0040
40-
MAP_INHERIT = 0x0080
41-
MAP_NOEXTEND = 0x0100
42-
MAP_HASSEMAPHORE = 0x0200
43-
end
44-
@static if VERSION >= v"1.1" && Sys.isfreebsd()
45-
MAP_STACK = 0x0400
46-
MAP_NOSYNC = 0x0800
47-
MAP_GUARD = 0x2000
48-
MAP_EXCL = 0x4000
49-
MAP_NOCORE = 0x4_0000
50-
MAP_32BIT = 0x8_0000
51-
elseif VERSION >= v"1.1" && Sys.isdragonfly()
52-
MAP_STACK = 0x0400
53-
MAP_NOSYNC = 0x0800
54-
MAP_VPAGETABLE = 0x2000
55-
MAP_TRYFIXED = 0x1_0000
56-
MAP_NOCORE = 0x2_0000
57-
MAP_SIZEALIGN = 0x4_0000
58-
elseif VERSION >= v"1.1" && Sys.isopenbsd()
59-
MAP_STACK = 0x4000
60-
MAP_CONCEAL = 0x8000
61-
elseif VERSION >= v"1.1" && Sys.isnetbsd()
62-
MAP_REMAPDUP = 0x0004
63-
MAP_TRYFIXED = 0x0400
64-
MAP_WIRED = 0x0800
65-
MAP_STACK = 0x2000
66-
end
67-
end
68-
Base.cconvert(::Type{T}, mf::MmapFlags) where {T <: Integer} = T(mf)
69-
70-
# Provide this weird mode?
71-
#@static if Sys.islinux()
72-
#const MAP_SHARED_VALIDATE = MmapFlags(0x03)
73-
#end
74-
75-
@staticexpand @enum AdviseFlags::Cint begin
76-
MADV_NORMAL = 0
77-
MADV_RANDOM = 1
78-
MADV_SEQUENTIAL = 2
79-
MADV_WILLNEED = 3
80-
MADV_DONTNEED = 4
81-
@static if Sys.islinux()
82-
MADV_FREE = 8
83-
MADV_REMOVE = 9
84-
MADV_DONTFORK = 10
85-
MADV_DOFORK = 11
86-
MADV_MERGEABLE = 12
87-
MADV_UNMERGEABLE = 13
88-
MADV_HUGEPAGE = 14
89-
MADV_NOHUGEPAGE = 15
90-
MADV_DONTDUMP = 16
91-
MADV_DODUMP = 17
92-
MADV_WIPEONFORK = 18
93-
MADV_KEEPONFORK = 19
94-
MADV_COLD = 20
95-
MADV_PAGEOUT = 21
96-
MADV_HWPOISON = 100
97-
MADV_SOFT_OFFLINE = 101
98-
elseif Sys.isapple()
99-
MADV_FREE = 5
100-
elseif VERSION >= v"1.1" && (Sys.isfreebsd() || Sys.isdragonfly())
101-
MADV_FREE = 5
102-
MADV_NOSYNC = 6
103-
MADV_AUTOSYNC = 7
104-
MADV_NOCORE = 8
105-
MADV_CORE = 9
106-
@static if Sys.isfreebsd()
107-
MADV_PROTECT = 10
108-
else
109-
MADV_INVAL = 10
110-
MADV_SETMAP = 11
111-
end
112-
elseif VERSION >= v"1.1" && (Sys.isopenbsd() || Sys.isnetbsd())
113-
MADV_SPACEAVAIL = 5
114-
MADV_FREE = 6
115-
end
116-
end
117-
Base.cconvert(::Type{T}, af::AdviseFlags) where {T <: Integer} = T(af)
4+
const PAGESIZE = Mmap.PAGESIZE
1185

1196
# Helpers to infer default parameters given a file handle/fd
1207
import Mmap: Anonymous, gethandle, grow!

0 commit comments

Comments
 (0)