Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
stateless_allocators.h
Go to the documentation of this file.
1/* Copyright (C) 2025 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_STATELESS_ALLOCATORS
24#define INCLUDED_STATELESS_ALLOCATORS
25
26#include "lib/alignment.h"
28#include "lib/sysdep/rtl.h"
29#include "lib/sysdep/vm.h"
30
31#include <cstdlib>
32
33// NB: STL allocators are parameterized on the object type and indicate
34// the number of elements to [de]allocate. however, these adapters are
35// only used for allocating storage and receive the number of bytes.
36
38{
39 void* allocate(size_t size)
40 {
41 return malloc(size);
42 }
43
44 void deallocate(void* p, size_t)
45 {
46 return free(p);
47 }
48};
49
50template<size_t alignment = allocationAlignment>
52{
53 void* allocate(size_t size)
54 {
55 return rtl_AllocateAligned(size, alignment);
56 }
57
58 void deallocate(void* p, size_t)
59 {
60 return rtl_FreeAligned(p);
61 }
62};
63
64template<vm::PageType pageType = vm::kDefault, int prot = PROT_READ|PROT_WRITE>
66{
67 void* allocate(size_t size)
68 {
69 return vm::Allocate(size, pageType, prot);
70 }
71
72 void deallocate(void* p, size_t size)
73 {
74 vm::Free(p, size);
75 }
76};
77
78template<size_t commitSize = g_LargePageSize, vm::PageType pageType = vm::kDefault, int prot = PROT_READ|PROT_WRITE>
80{
81 void* allocate(size_t size)
82 {
83 return vm::ReserveAddressSpace(size, commitSize, pageType, prot);
84 }
85
86 void deallocate(void* p, size_t size)
87 {
89 }
90};
91
92#endif // INCLUDED_STATELESS_ALLOCATORS
void Free(void *p, size_t size)
decommit memory and release address space.
Definition: uvm.cpp:120
void * Allocate(size_t size, PageType pageType, int prot)
reserve address space and commit memory.
Definition: uvm.cpp:105
void ReleaseAddressSpace(void *p, size_t size)
release address space and decommit any memory.
Definition: uvm.cpp:56
void * ReserveAddressSpace(size_t size, size_t, PageType, int)
reserve address space and set the parameters for any later on-demand commits.
Definition: uvm.cpp:47
void rtl_FreeAligned(void *alignedPointer)
Definition: gcc.cpp:96
void * rtl_AllocateAligned(size_t size, size_t alignment)
Definition: gcc.cpp:69
Definition: stateless_allocators.h:80
void * allocate(size_t size)
Definition: stateless_allocators.h:81
void deallocate(void *p, size_t size)
Definition: stateless_allocators.h:86
Definition: stateless_allocators.h:52
void * allocate(size_t size)
Definition: stateless_allocators.h:53
void deallocate(void *p, size_t)
Definition: stateless_allocators.h:58
Definition: stateless_allocators.h:38
void * allocate(size_t size)
Definition: stateless_allocators.h:39
void deallocate(void *p, size_t)
Definition: stateless_allocators.h:44
Definition: stateless_allocators.h:66
void deallocate(void *p, size_t size)
Definition: stateless_allocators.h:72
void * allocate(size_t size)
Definition: stateless_allocators.h:67