Skip to content

Commit 1bd99b5

Browse files
committed
Add binding for msync and associated constants
1 parent 492457c commit 1bd99b5

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

docs/src/lib/public.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The following functions are available on all Unix systems.
1111
UnixMmap.mmap
1212
UnixMmap.mincore
1313
UnixMmap.madvise!
14+
UnixMmap.msync!
1415
```
1516

1617
OS-specific constants for use with [`mmap`](@ref) and [`madvise!`](@ref) are defined at
@@ -30,6 +31,7 @@ import Main._flag_docs # hide
3031
<tr><td><code>MmapProtection</code></td>
3132
<td><code>MmapFlags</code></td>
3233
<td><code>AdviseFlags</code></td>
34+
<td><code>SyncFlags</code></td>
3335
</tr>
3436
</thead>
3537
<tbody>
@@ -53,6 +55,13 @@ _flag_docs(UnixMmap.MmapFlags) # hide
5355
```@example Linux
5456
_flag_docs(UnixMmap.AdviseFlags) # hide
5557
```
58+
```@raw html
59+
</td>
60+
<td>
61+
```
62+
```@example Linux
63+
_flag_docs(UnixMmap.SyncFlags) # hide
64+
```
5665
```@raw html
5766
</td>
5867
</tr>
@@ -72,6 +81,7 @@ import Main._flag_docs # hide
7281
<tr><td><code>MmapProtection</code></td>
7382
<td><code>MmapFlags</code></td>
7483
<td><code>AdviseFlags</code></td>
84+
<td><code>SyncFlags</code></td>
7585
</tr>
7686
</thead>
7787
<tbody>
@@ -95,6 +105,13 @@ _flag_docs(UnixMmap.MmapFlags) # hide
95105
```@example Apple
96106
_flag_docs(UnixMmap.AdviseFlags) # hide
97107
```
108+
```@raw html
109+
</td>
110+
<td>
111+
```
112+
```@example Linux
113+
_flag_docs(UnixMmap.SyncFlags) # hide
114+
```
98115
```@raw html
99116
</td>
100117
</tr>
@@ -114,6 +131,7 @@ import Main._flag_docs # hide
114131
<tr><td><code>MmapProtection</code></td>
115132
<td><code>MmapFlags</code></td>
116133
<td><code>AdviseFlags</code></td>
134+
<td><code>SyncFlags</code></td>
117135
</tr>
118136
</thead>
119137
<tbody>
@@ -137,6 +155,13 @@ _flag_docs(UnixMmap.MmapFlags) # hide
137155
```@example DragonFly
138156
_flag_docs(UnixMmap.AdviseFlags) # hide
139157
```
158+
```@raw html
159+
</td>
160+
<td>
161+
```
162+
```@example Linux
163+
_flag_docs(UnixMmap.SyncFlags) # hide
164+
```
140165
```@raw html
141166
</td>
142167
</tr>
@@ -156,6 +181,7 @@ import Main._flag_docs # hide
156181
<tr><td><code>MmapProtection</code></td>
157182
<td><code>MmapFlags</code></td>
158183
<td><code>AdviseFlags</code></td>
184+
<td><code>SyncFlags</code></td>
159185
</tr>
160186
</thead>
161187
<tbody>
@@ -179,6 +205,13 @@ _flag_docs(UnixMmap.MmapFlags) # hide
179205
```@example FreeBSD
180206
_flag_docs(UnixMmap.AdviseFlags) # hide
181207
```
208+
```@raw html
209+
</td>
210+
<td>
211+
```
212+
```@example Linux
213+
_flag_docs(UnixMmap.SyncFlags) # hide
214+
```
182215
```@raw html
183216
</td>
184217
</tr>
@@ -198,6 +231,7 @@ import Main._flag_docs # hide
198231
<tr><td><code>MmapProtection</code></td>
199232
<td><code>MmapFlags</code></td>
200233
<td><code>AdviseFlags</code></td>
234+
<td><code>SyncFlags</code></td>
201235
</tr>
202236
</thead>
203237
<tbody>
@@ -221,6 +255,13 @@ _flag_docs(UnixMmap.MmapFlags) # hide
221255
```@example NetBSD
222256
_flag_docs(UnixMmap.AdviseFlags) # hide
223257
```
258+
```@raw html
259+
</td>
260+
<td>
261+
```
262+
```@example Linux
263+
_flag_docs(UnixMmap.SyncFlags) # hide
264+
```
224265
```@raw html
225266
</td>
226267
</tr>
@@ -240,6 +281,7 @@ import Main._flag_docs # hide
240281
<tr><td><code>MmapProtection</code></td>
241282
<td><code>MmapFlags</code></td>
242283
<td><code>AdviseFlags</code></td>
284+
<td><code>SyncFlags</code></td>
243285
</tr>
244286
</thead>
245287
<tbody>
@@ -263,6 +305,13 @@ _flag_docs(UnixMmap.MmapFlags) # hide
263305
```@example OpenBSD
264306
_flag_docs(UnixMmap.AdviseFlags) # hide
265307
```
308+
```@raw html
309+
</td>
310+
<td>
311+
```
312+
```@example Linux
313+
_flag_docs(UnixMmap.SyncFlags) # hide
314+
```
266315
```@raw html
267316
</td>
268317
</tr>

src/consts.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import .Sys # explicitly imported to allow docs generation to override
99
end
1010
Base.cconvert(::Type{T}, pf::MmapProtection) where {T <: Integer} = T(pf)
1111

12+
@bitflag SyncFlags::Cuint begin
13+
MS_ASYNC = 0x1
14+
MS_INVALIDATE = 0x2
15+
MS_SYNC = 0x4
16+
end
17+
Base.cconvert(::Type{T}, sf::SyncFlags) where {T <: Integer} = T(sf)
18+
1219
@staticexpand @bitflag MmapFlags::Cuint begin
1320
MAP_FILE = 0x00
1421
MAP_SHARED = 0x01
@@ -134,10 +141,22 @@ let _flag_docs
134141
```
135142
""" MmapProtection
136143

144+
@doc """
145+
@bitflag UnixMmap.SyncFlags
146+
147+
Set of bit flags which control the memory synchronization behavior.
148+
149+
The flags available on $(Sys.KERNEL) are:
150+
```julia
151+
$(_flag_docs(SyncFlags))
152+
```
153+
""" SyncFlags
154+
155+
137156
@doc """
138157
@bitflag UnixMmap.MmapFlags
139158
140-
Set of bit flags which control the handling of the memory mappged region. The flag
159+
Set of bit flags which control the handling of the memory mapped region. The flag
141160
should be a bitwise-or combination of flags.
142161
143162
The flags available on $(Sys.KERNEL) are:

src/mmap.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,18 @@ function madvise!(array::Array, flag::AdviseFlags = MADV_NORMAL)
281281
end
282282
return array
283283
end
284+
285+
"""
286+
msync!(array, flag::SyncFlags = MS_SYNC)
287+
288+
Synchronizes the memory-mapped `array` and its backing file on disk.
289+
"""
290+
function msync!(array::Array, flag::SyncFlags = MS_SYNC)
291+
GC.@preserve array begin
292+
ptr, off, len = pagepointer(array)
293+
Base.systemerror("msync",
294+
ccall(:msync, Cint, (Ptr{Cvoid}, Csize_t, Cint),
295+
ptr, len, flag) != 0)
296+
end
297+
return array
298+
end

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,13 @@ end
196196
A = mmap(Array{Int}, 500)
197197
@test A == UnixMmap.madvise!(A, UnixMmap.MADV_WILLNEED)
198198
end
199+
200+
@testset "Synchronizing" begin
201+
# It's hard to test behavior of the sync flags --- just check for not erroring
202+
mktemp() do path, io
203+
A = mmap(io, Array{Int}, 500)
204+
@test A == UnixMmap.msync!(A, UnixMmap.MS_SYNC | UnixMmap.MS_INVALIDATE)
205+
A[:] .= 1.0
206+
@test A == UnixMmap.msync!(A, UnixMmap.MS_ASYNC)
207+
end
208+
end

0 commit comments

Comments
 (0)