libcosmos
Linux C++ System Programming Library
All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mman.hxx
Go to the documentation of this file.
1#pragma once
2
3// C++
4#include <optional>
5
6// Linux
7#include <sys/mman.h>
8#include <linux/mman.h> // also pull in kernel header for MAP_UNINITIALIZED
9
10// libcosmos
11#include <cosmos/BitMask.hxx>
12#include <cosmos/fs/FileDescriptor.hxx>
13
20namespace cosmos::mem {
21
23enum class MapType : int {
25 SHARED = MAP_SHARED,
27 SHARED_VALIDATE = MAP_SHARED_VALIDATE,
29 PRIVATE = MAP_PRIVATE
30};
31
32// this constant hasn't made it into userspace headers yet it seems
33#ifndef PROT_SAO
34# define PROT_SAO 0x10
35#endif
36
38enum class AccessFlag : int {
39 EXEC = PROT_EXEC,
40 READ = PROT_READ,
41 WRITE = PROT_WRITE,
42 NONE = PROT_NONE,
43 SEM = PROT_SEM,
44 SAO = PROT_SAO,
45};
46
49
51enum class MapFlag : int {
52#ifdef MAP_32BIT // this is not defined on non-x86 architectures?
54
57 INTO_32BIT = MAP_32BIT,
58#endif
60
63 ANONYMOUS = MAP_ANONYMOUS,
65 FIXED = MAP_FIXED,
67 FIXED_NOREPLACE = MAP_FIXED_NOREPLACE,
69 GROWSDOWN = MAP_GROWSDOWN,
71 HUGETLB = MAP_HUGETLB,
73 LOCKED = MAP_LOCKED,
75 NONBLOCK = MAP_NONBLOCK,
77
80 NORESERVE = MAP_NORESERVE,
82
86 POPULATE = MAP_POPULATE,
88 STACK = MAP_STACK,
90
97 SYNC = MAP_SYNC,
99
103 UNINITIALIZED = MAP_UNINITIALIZED,
104};
105
107class COSMOS_API MapFlags :
108 public BitMask<MapFlag> {
109public:
110 using BitMask<MapFlag>::BitMask;
111
113
117 void setTLBPageSize(size_t page_size);
118};
119
122 MapType type;
123 AccessFlags access;
124 MapFlags flags = {};
125 off_t offset = 0;
127 void *addr = nullptr;
128};
129
131
166COSMOS_API void* map(const size_t length, const MapSettings &settings);
167
169
177COSMOS_API void unmap(void *addr, const size_t length);
178
179
180
182enum class ProtectFlag : int {
183 GROWSUP = PROT_GROWSUP,
184 GROWSDOWN = PROT_GROWSDOWN,
185};
186
189
191
211COSMOS_API void protect(void *addr, const size_t length, const AccessFlags flags, const ProtectFlags extra = {});
212
213
214
216enum class RemapFlag : int {
217 MAYMOVE = MREMAP_MAYMOVE,
218 FIXED = MREMAP_FIXED,
219 DONTUNMAP = MREMAP_DONTUNMAP,
220};
221
222using RemapFlags = BitMask<RemapFlag>;
223
225
271COSMOS_API void* remap(void *old_addr, const size_t old_size, const size_t new_size,
272 const RemapFlags flags = {}, std::optional<void*> new_addr = {});
273
274
275
277
281enum class SyncFlag : int {
282 ASYNC = MS_ASYNC,
283 SYNC = MS_SYNC,
284 INVALIDATE = MS_INVALIDATE
285};
286
287using SyncFlags = BitMask<SyncFlag>;
288
290
307COSMOS_API void sync(void *addr, const size_t length,
308 const SyncFlags flags = SyncFlags{SyncFlag::SYNC});
309
310
311
313enum class LockFlag : unsigned int {
314 LOCK_ON_FAULT = MLOCK_ONFAULT,
315};
316
317using LockFlags = BitMask<LockFlag>;
318
320
350COSMOS_API void lock(void *addr, const size_t length, const LockFlags flags = {});
351
353
362COSMOS_API void unlock(void *addr, const size_t length);
363
364
365
367enum class LockAllFlag : int {
368 CURRENT = MCL_CURRENT,
369 FUTURE = MCL_FUTURE,
370 ONFAULT = MCL_ONFAULT,
371};
372
373using LockAllFlags = BitMask<LockAllFlag>;
374
376
386COSMOS_API void lock_all(const LockAllFlags flags);
387
389
393COSMOS_API void unlock_all();
394
395} // end ns
Thin Wrapper around OS file descriptors.
Flags used in MapSettings.
Definition mman.hxx:108
@ SYNC
Similar to DSYNC, see man page.
@ NONBLOCK
Attempt to open the file in non-blocking mode causing I/O operations not to block.
@ ASYNC
Enable signal driven I/O for certain file types.
ProtectFlag
Extra flags used with mem::protect().
Definition mman.hxx:182
@ GROWSUP
Apply protection settings up to the end of mapping that grows upwards.
LockFlag
Flags used with cosmos::mem::lock().
Definition mman.hxx:313
@ LOCK_ON_FAULT
lock all pages that are already resident, the rest will be locked after a page fault occurs.
RemapFlag
Flags used with cosmos::mem::remap().
Definition mman.hxx:216
@ MAYMOVE
allow to move the mapping to a new starting address.
@ DONTUNMAP
used only together with MAYMOVE; keep the original mapping available for special memory algorithms li...
SyncFlag
Flags used with cosmos::mem::sync().
Definition mman.hxx:281
@ INVALIDATE
invalidate other mappings of the same file, allowing them to be updated with the changed data.
LockAllFlag
Flags passed to cosmos::mem::lockall().
Definition mman.hxx:367
@ ONFAULT
lock all current/future pages, but don't pre-fault them.
@ CURRENT
lock all currently loaded pages in memory.
@ FUTURE
lock all pages loaded in the future in memory.
MapType
The basic type of a memory mapping to be created.
Definition mman.hxx:23
@ SHARED
Creates a shared memory mapping that can be shared with other processes.
@ PRIVATE
A private copy-on-write mapping that isn't shared with other processes.
@ SHARED_VALIDATE
Same as SHARED but the MapFlags will be validated for unknown flags.
MapFlag
Flags that influence properties of memory mappings.
Definition mman.hxx:51
@ FIXED_NOREPLACE
Like FIXED but don't replace existing mappings, fail with EEXISTS instead.
@ STACK
Allocate the mapping at an address suitable for a thread stack (currently has no effect on Linux).
@ NORESERVE
Do not reserve swap space for this mapping.
@ GROWSDOWN
Create a mapping suitable for stacks, including automatic growing via a guard page.
@ POPULATE
Pre-fault page tables for a mapping.
@ ANONYMOUS
Create a mapping that is not backed by a file, contents are initialized to zero.
@ LOCKED
Mark the memory to be locked similar to mem::lock(), but no major faults will be prevented.
@ FIXED
Map memory exactly at the given hint address, replacing already existing mappings at the address.
@ UNINITIALIZED
Don't clear anonymous pages.
@ HUGETLB
Allocate the mapping using hugetlb page sizes, see also MapFlags::setTLBPageSize().
AccessFlag
Different memory page access permissions.
Definition mman.hxx:38
@ SEM
the memory can be used for atomic operations (used with futexes, doesn't currently do anything on any...
@ READ
allow read access
@ SAO
the memory should have strong access ordering (a PowerPC architecture feature)
@ WRITE
allow write access
@ EXEC
allow execute access
Collection of settings used in cosmos::mem::map().
Definition mman.hxx:121
off_t offset
offset into the file object starting from which the mapping will be setup for.
Definition mman.hxx:125
FileDescriptor fd
the file object to be mapped, if MapFlag::ANONYMOUS is set then leave this invalid.
Definition mman.hxx:126
void * addr
a hint where to place the mapping, or the exact address if MapFlag::FIXED is given.
Definition mman.hxx:127