Commit 482b1a10 authored by Kirill Smelkov's avatar Kirill Smelkov

wcfs/internal/mm: Complete the package

Add two functions, that were developed during wendelin.core 2 α, to the
package for completeness:

- map_zero_into_ro complements map_zero_ro, but mmaps into user-provided buffer.
- sync calls msync on the provided memory.
parent a49d737e
...@@ -182,6 +182,21 @@ def map_zero_ro(size_t size): ...@@ -182,6 +182,21 @@ def map_zero_ro(size_t size):
return <unsigned char[:size:1]>addr return <unsigned char[:size:1]>addr
# map_zero_into_ro is similar to map_zero_ro, but mmaps zeros into mem's memory.
def map_zero_into_ro(unsigned char[::1] mem not None):
cdef void *addr = &mem[0]
cdef size_t size = mem.shape[0]
f = open("/dev/zero", "rb")
addr = mman.mmap(addr, size, mman.PROT_READ, mman.MAP_FIXED |
mman.MAP_SHARED | mman.MAP_NORESERVE, f.fileno(), 0)
f.close()
if addr == mman.MAP_FAILED:
PyErr_SetFromErrno(OSError)
return
return
# advise advises kernel about use of mem's memory. # advise advises kernel about use of mem's memory.
# #
...@@ -209,3 +224,17 @@ def protect(const unsigned char[::1] mem not None, int prot): ...@@ -209,3 +224,17 @@ def protect(const unsigned char[::1] mem not None, int prot):
PyErr_SetFromErrno(OSError) PyErr_SetFromErrno(OSError)
return return
# sync asks the kernel to synchronize the file with a memory map.
#
# see msync(2) for details.
def sync(const unsigned char[::1] mem not None, int flags):
cdef const void *addr = &mem[0]
cdef size_t size = mem.shape[0]
cdef err = mman.msync(<void *>addr, size, flags)
if err:
PyErr_SetFromErrno(OSError)
return
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment