Line data Source code
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 :
35 : struct Allocator_Heap
36 : {
37 0 : void* allocate(size_t size)
38 : {
39 0 : return malloc(size);
40 : }
41 :
42 0 : void deallocate(void* p, size_t UNUSED(size))
43 : {
44 0 : return free(p);
45 : }
46 : };
47 :
48 : template<size_t alignment = allocationAlignment>
49 : struct Allocator_Aligned
50 : {
51 0 : void* allocate(size_t size)
52 : {
53 0 : return rtl_AllocateAligned(size, alignment);
54 : }
55 :
56 0 : void deallocate(void* p, size_t UNUSED(size))
57 : {
58 0 : return rtl_FreeAligned(p);
59 : }
60 : };
61 :
62 : template<vm::PageType pageType = vm::kDefault, int prot = PROT_READ|PROT_WRITE>
63 : struct Allocator_VM
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 :
76 : template<size_t commitSize = g_LargePageSize, vm::PageType pageType = vm::kDefault, int prot = PROT_READ|PROT_WRITE>
77 : struct Allocator_AddressSpace
78 : {
79 0 : void* allocate(size_t size)
80 : {
81 0 : return vm::ReserveAddressSpace(size, commitSize, pageType, prot);
82 : }
83 :
84 0 : void deallocate(void* p, size_t size)
85 : {
86 0 : vm::ReleaseAddressSpace(p, size);
87 0 : }
88 : };
89 :
90 : #endif // INCLUDED_STATELESS_ALLOCATORS
|