Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
page_aligned.h
Go to the documentation of this file.
1/* Copyright (C) 2022 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23#ifndef INCLUDED_ALLOCATORS_PAGE_ALIGNED
24#define INCLUDED_ALLOCATORS_PAGE_ALIGNED
25
26#include "lib/posix/posix_mman.h" // PROT_*
27
28// very thin wrapper on top of sys/mman.h that makes the intent more obvious
29// (its commit/decommit semantics are difficult to tell apart)
30Status mem_Reserve(size_t size, u8** pp);
31Status mem_Release(u8* p, size_t size);
32Status mem_Commit(u8* p, size_t size, int prot);
33Status mem_Decommit(u8* p, size_t size);
34Status mem_Protect(u8* p, size_t size, int prot);
35
36
37/**
38 * allocate memory aligned to the system page size.
39 *
40 * this is useful for file_cache_alloc, which uses this allocator to
41 * get sector-aligned (hopefully; see sys_max_sector_size) IO buffers.
42 *
43 * note that this allocator is stateless and very little error checking
44 * can be performed.
45 *
46 * the memory is initially writable and you can use mprotect to set other
47 * access permissions if desired.
48 *
49 * @param unaligned_size minimum size [bytes] to allocate.
50 * @return page-aligned and -padded memory or 0 on error / out of memory.
51 **/
52void* page_aligned_alloc(size_t unaligned_size);
53
54/**
55 * free a previously allocated page-aligned region.
56 *
57 * @param p Exact value returned from page_aligned_alloc
58 * @param unaligned_size Exact value passed to page_aligned_alloc
59 **/
60void page_aligned_free(void* p, size_t unaligned_size);
61
62#endif // #ifndef INCLUDED_ALLOCATORS_PAGE_ALIGNED
Status mem_Decommit(u8 *p, size_t size)
Definition: page_aligned.cpp:75
Status mem_Reserve(size_t size, u8 **pp)
Definition: page_aligned.cpp:48
Status mem_Protect(u8 *p, size_t size, int prot)
Definition: page_aligned.cpp:82
Status mem_Commit(u8 *p, size_t size, int prot)
Definition: page_aligned.cpp:64
void page_aligned_free(void *p, size_t unaligned_size)
free a previously allocated page-aligned region.
Definition: page_aligned.cpp:104
void * page_aligned_alloc(size_t unaligned_size)
allocate memory aligned to the system page size.
Definition: page_aligned.cpp:94
Status mem_Release(u8 *p, size_t size)
Definition: page_aligned.cpp:56
i64 Status
Error handling system.
Definition: status.h:173
uint8_t u8
Definition: types.h:37