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