vmem.h (2720B)
1 #pragma once 2 3 #include <stdbool.h> 4 5 #include "def.h" 6 7 /* Get the system page size. Crash if the page size could not be determined or 8 * is not a power of two. */ 9 usize r_vmem_page_size(void); 10 11 /* Allocate a page-aligned block of size bytes with read and write permission 12 * and return a pointer to them. If p is not NULL, place the mapping there (so 13 * the return value is p on success) (e.g., p could come from r_vmem_reserve). 14 * On failure, set errno and return NULL. */ 15 void *r_vmem_alloc(void *p, usize size); 16 17 /* Reserve a page-aligned block of size bytes and return a pointer to them. If 18 * p is not NULL, place the mapping there (so the return value is p on 19 * success). When swap is false, only reserve virtual memory; when swap is 20 * true, also reserve swap space. In either case, never allocate physical 21 * memory; see r_vmem_alloc for this functionality. On failure, set errno and 22 * return NULL. */ 23 void *r_vmem_reserve(void *p, usize size, bool swap); 24 25 /* Open and memory-map the file referred to by path. If p is not NULL, place 26 * the mapping there (so the return value is p on success). If target_size > 0, 27 * make the mapping have that size by ignoring trailing bytes if target_size 28 * is less than the file size or by appending null bytes if target_size is 29 * greater then the file size. If size is not NULL, set *size to the mapping 30 * size (which is always target_size if target_size > 0). The presence of the 31 * following characters in opt (in any order) enable certain boolean options: 32 * - 'c': Create the file if it does not exist. If 'c' is set, then an 33 * additional mode argument is mandatory (like open(2)). 34 * - 'r': Create the mapping with read permission. 35 * - 's': Create a shared mapping instead of a private mapping. 36 * - 't': Truncate the file to length 0. If 't' is set, then target_size must 37 * be positive. 38 * - 'w': Create the mapping with write permission. 39 * - 'x': Ensure that this call creates the file. This option implies the 'c' 40 * option (so, in particular, if 'x' is set, then the mode argument is 41 * mandatory). 42 * (Note that the 'c', 't', and 'x' options basically correspond respectively 43 * to the O_CREAT, O_TRUNC, and O_EXCL flags for open(2). Similarly, 's', 'r', 44 * and 'w' correspond respectively to MAP_SHARED (as opposed to MAP_PRIVATE), 45 * PROT_READ, and PROT_WRITE for mmap(2).) On failure, set errno and return 46 * NULL. */ 47 void *r_vmem_open(usize *size, void *p, char *path, usize target_size, char *opt, ... /* mode_t mode */); 48 49 /* Unmap the mapped region described by p and size, which should have been 50 * obtained by a previous call to r_vmem_{alloc,reserve,open}. */ 51 void r_vmem_free(void *p, usize size);