Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions docs/src/lib/public.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The following functions are available on all Unix systems.
UnixMmap.mmap
UnixMmap.mincore
UnixMmap.madvise!
UnixMmap.msync!
```

OS-specific constants for use with [`mmap`](@ref) and [`madvise!`](@ref) are defined at
Expand All @@ -30,6 +31,7 @@ import Main._flag_docs # hide
<tr><td><code>MmapProtection</code></td>
<td><code>MmapFlags</code></td>
<td><code>AdviseFlags</code></td>
<td><code>SyncFlags</code></td>
</tr>
</thead>
<tbody>
Expand All @@ -53,6 +55,13 @@ _flag_docs(UnixMmap.MmapFlags) # hide
```@example Linux
_flag_docs(UnixMmap.AdviseFlags) # hide
```
```@raw html
</td>
<td>
```
```@example Linux
_flag_docs(UnixMmap.SyncFlags) # hide
```
```@raw html
</td>
</tr>
Expand All @@ -72,6 +81,7 @@ import Main._flag_docs # hide
<tr><td><code>MmapProtection</code></td>
<td><code>MmapFlags</code></td>
<td><code>AdviseFlags</code></td>
<td><code>SyncFlags</code></td>
</tr>
</thead>
<tbody>
Expand All @@ -95,6 +105,13 @@ _flag_docs(UnixMmap.MmapFlags) # hide
```@example Apple
_flag_docs(UnixMmap.AdviseFlags) # hide
```
```@raw html
</td>
<td>
```
```@example Linux
_flag_docs(UnixMmap.SyncFlags) # hide
```
```@raw html
</td>
</tr>
Expand All @@ -114,6 +131,7 @@ import Main._flag_docs # hide
<tr><td><code>MmapProtection</code></td>
<td><code>MmapFlags</code></td>
<td><code>AdviseFlags</code></td>
<td><code>SyncFlags</code></td>
</tr>
</thead>
<tbody>
Expand All @@ -137,6 +155,13 @@ _flag_docs(UnixMmap.MmapFlags) # hide
```@example DragonFly
_flag_docs(UnixMmap.AdviseFlags) # hide
```
```@raw html
</td>
<td>
```
```@example Linux
_flag_docs(UnixMmap.SyncFlags) # hide
```
```@raw html
</td>
</tr>
Expand All @@ -156,6 +181,7 @@ import Main._flag_docs # hide
<tr><td><code>MmapProtection</code></td>
<td><code>MmapFlags</code></td>
<td><code>AdviseFlags</code></td>
<td><code>SyncFlags</code></td>
</tr>
</thead>
<tbody>
Expand All @@ -179,6 +205,13 @@ _flag_docs(UnixMmap.MmapFlags) # hide
```@example FreeBSD
_flag_docs(UnixMmap.AdviseFlags) # hide
```
```@raw html
</td>
<td>
```
```@example Linux
_flag_docs(UnixMmap.SyncFlags) # hide
```
```@raw html
</td>
</tr>
Expand All @@ -198,6 +231,7 @@ import Main._flag_docs # hide
<tr><td><code>MmapProtection</code></td>
<td><code>MmapFlags</code></td>
<td><code>AdviseFlags</code></td>
<td><code>SyncFlags</code></td>
</tr>
</thead>
<tbody>
Expand All @@ -221,6 +255,13 @@ _flag_docs(UnixMmap.MmapFlags) # hide
```@example NetBSD
_flag_docs(UnixMmap.AdviseFlags) # hide
```
```@raw html
</td>
<td>
```
```@example Linux
_flag_docs(UnixMmap.SyncFlags) # hide
```
```@raw html
</td>
</tr>
Expand All @@ -240,6 +281,7 @@ import Main._flag_docs # hide
<tr><td><code>MmapProtection</code></td>
<td><code>MmapFlags</code></td>
<td><code>AdviseFlags</code></td>
<td><code>SyncFlags</code></td>
</tr>
</thead>
<tbody>
Expand All @@ -263,6 +305,13 @@ _flag_docs(UnixMmap.MmapFlags) # hide
```@example OpenBSD
_flag_docs(UnixMmap.AdviseFlags) # hide
```
```@raw html
</td>
<td>
```
```@example Linux
_flag_docs(UnixMmap.SyncFlags) # hide
```
```@raw html
</td>
</tr>
Expand Down
21 changes: 20 additions & 1 deletion src/consts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import .Sys # explicitly imported to allow docs generation to override
end
Base.cconvert(::Type{T}, pf::MmapProtection) where {T <: Integer} = T(pf)

@bitflag SyncFlags::Cuint begin
MS_ASYNC = 0x1
MS_INVALIDATE = 0x2
MS_SYNC = 0x4
end
Base.cconvert(::Type{T}, sf::SyncFlags) where {T <: Integer} = T(sf)

@staticexpand @bitflag MmapFlags::Cuint begin
MAP_FILE = 0x00
MAP_SHARED = 0x01
Expand Down Expand Up @@ -134,10 +141,22 @@ let _flag_docs
```
""" MmapProtection

@doc """
@bitflag UnixMmap.SyncFlags

Set of bit flags which control the memory synchronization behavior.

The flags available on $(Sys.KERNEL) are:
```julia
$(_flag_docs(SyncFlags))
```
""" SyncFlags


@doc """
@bitflag UnixMmap.MmapFlags

Set of bit flags which control the handling of the memory mappged region. The flag
Set of bit flags which control the handling of the memory mapped region. The flag
should be a bitwise-or combination of flags.

The flags available on $(Sys.KERNEL) are:
Expand Down
15 changes: 15 additions & 0 deletions src/mmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,18 @@ function madvise!(array::Array, flag::AdviseFlags = MADV_NORMAL)
end
return array
end

"""
msync!(array, flag::SyncFlags = MS_SYNC)

Synchronizes the memory-mapped `array` and its backing file on disk.
"""
function msync!(array::Array, flag::SyncFlags = MS_SYNC)
GC.@preserve array begin
ptr, off, len = pagepointer(array)
Base.systemerror("msync",
ccall(:msync, Cint, (Ptr{Cvoid}, Csize_t, Cint),
ptr, len, flag) != 0)
end
return array
end
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,13 @@ end
A = mmap(Array{Int}, 500)
@test A == UnixMmap.madvise!(A, UnixMmap.MADV_WILLNEED)
end

@testset "Synchronizing" begin
# It's hard to test behavior of the sync flags --- just check for not erroring
mktemp() do path, io
A = mmap(io, Array{Int}, 500)
@test A == UnixMmap.msync!(A, UnixMmap.MS_SYNC | UnixMmap.MS_INVALIDATE)
A[:] .= 1.0
@test A == UnixMmap.msync!(A, UnixMmap.MS_ASYNC)
end
end