haiku/headers/posix/sys/mman.h
Michael Lotz b3bd66961a libroot/kernel: Implement MADV_FREE madvise() extension.
It allows an application to signal that it no longer needs the data in
the given address range and the underlying pages can be discarded and
reused elsewhere. This is finer grained than working with full areas
or mappings at a time and enables unmapping sections of partially used
mappings without giving up its address space.

Compared with punching holes into a mapping by "mapping over" with
PROT_NONE and MAP_NORESERVE, this has the obvious advantage of not
producing a lot of unused extra areas and saves the corresponding
resources. It is also a lot "lighter" of an operation than cutting
existing areas.

This introduces madvise() alongside the existing posix_madvise() to
allow for OS specific extensions. The constants for both functions are
aliased, the POSIX_MADV_* being a subset of the MADV_* ones without the
non-POSIX extensions. Internally posix_madvise() simply calls madvise().

MADV_FREE is commonly supported in other OSes with various subtle
semantic differences as to when pages are actually freed/cleared and how
or whether the pages are counted against the memory use of a process.
In the variant implemented here, pages are always immediately discarded
and memory counting is not altered. This behaviour should be considered
an implementation detail and may be altered later. The actual unmap and
discard could for example be delayed until pages are needed elsewhere to
reduce overhead in case of repeated discarding and remapping.

Note that MADV_FREE doesn't really align with the rest of the madvise()
API as it works like a command (i.e. discard these pages) and does not
add an attribute to the pages in the given range (i.e. mark these pages
for quick access from now on). As such, an MADV_FREE does not need to be
undone by setting a different advice later on, unlike how the other
flags work. This discrepancy may be the reason why it is not part of
POSIX.

Change-Id: Icc093379125a43e465dc4409d8f5ae0f64e107e0
Reviewed-on: https://review.haiku-os.org/c/haiku/+/2844
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
2020-08-01 19:23:27 +00:00

71 lines
1.8 KiB
C

/*
* Copyright 2008-2012 Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _SYS_MMAN_H
#define _SYS_MMAN_H
#include <sys/cdefs.h>
#include <sys/types.h>
/* memory protection for mmap() and others; assignment compatible with
B_{READ,WRITE,EXECUTE}_AREA */
#define PROT_READ 0x01
#define PROT_WRITE 0x02
#define PROT_EXEC 0x04
#define PROT_NONE 0x00
/* mmap() flags */
#define MAP_SHARED 0x01 /* changes are seen by others */
#define MAP_PRIVATE 0x02 /* changes are only seen by caller */
#define MAP_FIXED 0x04 /* require mapping to specified addr */
#define MAP_ANONYMOUS 0x08 /* no underlying object */
#define MAP_ANON MAP_ANONYMOUS
#define MAP_NORESERVE 0x10 /* don't commit memory */
/* mmap() error return code */
#define MAP_FAILED ((void*)-1)
/* msync() flags */
#define MS_ASYNC 0x01
#define MS_SYNC 0x02
#define MS_INVALIDATE 0x04
/* madvise() values */
#define MADV_NORMAL 1
#define MADV_SEQUENTIAL 2
#define MADV_RANDOM 3
#define MADV_WILLNEED 4
#define MADV_DONTNEED 5
#define MADV_FREE 6
/* posix_madvise() values */
#define POSIX_MADV_NORMAL MADV_NORMAL
#define POSIX_MADV_SEQUENTIAL MADV_SEQUENTIAL
#define POSIX_MADV_RANDOM MADV_RANDOM
#define POSIX_MADV_WILLNEED MADV_WILLNEED
#define POSIX_MADV_DONTNEED MADV_DONTNEED
__BEGIN_DECLS
void* mmap(void* address, size_t length, int protection, int flags,
int fd, off_t offset);
int munmap(void* address, size_t length);
int mprotect(void* address, size_t length, int protection);
int msync(void* address, size_t length, int flags);
int madvise(void* address, size_t length, int advice);
int posix_madvise(void* address, size_t length, int advice);
int shm_open(const char* name, int openMode, mode_t permissions);
int shm_unlink(const char* name);
__END_DECLS
#endif /* _SYS_MMAN_H */