#ifndef	_SYS_MMAN_H
#define	_SYS_MMAN_H

#include <sys/types.h>

/* Protections are chosen from these bits, OR'd together.  The
   implementation does not necessarily support PROT_EXEC or PROT_WRITE
   without PROT_READ.  The only guarantees are that no writing will be
   allowed without PROT_WRITE and no access will be allowed for PROT_NONE. */

#define PROT_READ	0x1		/* Page can be read.  */
#define PROT_WRITE	0x2		/* Page can be written.  */
#define PROT_EXEC	0x4		/* Page can be executed.  */
#define PROT_NONE	0x0		/* Page can not be accessed.  */

/* Sharing types (must choose one and only one of these).  */
#define MAP_SHARED	0x01		/* Share changes.  */
#define MAP_PRIVATE	0x02		/* Changes are private.  */
#define MAP_TYPE	0x0f		/* Mask for type of mapping.  */

/* Other flags.  */
#define MAP_FIXED	0x100		/* Interpret addr exactly.  */
#define MAP_FILE	0x00
#define MAP_ANONYMOUS	0x10		/* Don't use a file.  */
#define MAP_ANON	MAP_ANONYMOUS
#define MAP_RENAME	MAP_ANONYMOUS

/* These are Linux-specific.  */
#ifdef __linux__
# define MAP_GROWSDOWN	0x1000		/* Stack-like segment.  */
# define MAP_DENYWRITE	0x0800		/* ETXTBSY */
# define MAP_EXECUTABLE	0x4000		/* Mark it as an executable.  */
# define MAP_LOCKED     0x8000		/* Lock the mapping.  */
# define MAP_NORESERVE	0x10000		/* Don't check for reservations.  */
#endif

/* Flags to `msync'.  */
#define MS_ASYNC	1		/* Sync memory asynchronously.  */
#define MS_SYNC		4		/* Synchronous memory sync.  */
#define MS_INVALIDATE	2		/* Invalidate the caches.  */

/* Return value of `mmap' in case of an error.  */
#define MAP_FAILED	((void *) -1)

/* Map addresses starting near ADDR and extending for LEN bytes.  from
   OFFSET into the file FD describes according to PROT and FLAGS.  If ADDR
   is nonzero, it is the desired mapping address.  If the MAP_FIXED bit is
   set in FLAGS, the mapping will be at ADDR exactly (which must be
   page-aligned); otherwise the system chooses a convenient nearby address.
   The return value is the actual mapping address chosen or MAP_FAILED
   for errors (in which case `errno' is set).  A successful `mmap' call
   deallocates any previous mapping for the affected region.  */

extern void *mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset);

#ifdef __linux__
/* Remap pages mapped by the range [ADDR,ADDR+OLD_LEN) to new length
   NEW_LEN.  If MAY_MOVE is MREMAP_MAYMOVE the returned address may
   differ from ADDR.  */
extern void *mremap (void *addr, size_t old_len, size_t new_len, int may_move);
#endif

/* Deallocate any mapping for the region starting at ADDR and extending LEN
   bytes.  Returns 0 if successful, -1 for errors (and sets errno).  */
extern int munmap (void *addr, size_t len);

/* Change the memory protection of the region starting at ADDR and
   extending LEN bytes to PROT.  Returns 0 if successful, -1 for errors
   (and sets errno).  */
extern int mprotect (void *addr, size_t len, int prot);

/* Synchronize the region starting at ADDR and extending LEN bytes with the
   file it maps.  Filesystem operations on a file being mapped are
   unpredictable before this is done.  Flags are from the MS_* set.  */
extern int msync (void *addr, size_t len, int flags);

#endif	/* _SYS_MMAN_H */

