1 | /**************************************************************************/ |
2 | /* pool_allocator.h */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #ifndef POOL_ALLOCATOR_H |
32 | #define POOL_ALLOCATOR_H |
33 | |
34 | #include "core/typedefs.h" |
35 | |
36 | /** |
37 | * Generic Pool Allocator. |
38 | * This is a generic memory pool allocator, with locking, compacting and alignment. (@TODO alignment) |
39 | * It used as a standard way to manage allocation in a specific region of memory, such as texture memory, |
40 | * audio sample memory, or just any kind of memory overall. |
41 | * (@TODO) abstraction should be greater, because in many platforms, you need to manage a nonreachable memory. |
42 | */ |
43 | |
44 | enum { |
45 | POOL_ALLOCATOR_INVALID_ID = -1 ///< default invalid value. use INVALID_ID( id ) to test |
46 | }; |
47 | |
48 | class PoolAllocator { |
49 | public: |
50 | typedef int ID; |
51 | |
52 | private: |
53 | enum { |
54 | CHECK_BITS = 8, |
55 | CHECK_LEN = (1 << CHECK_BITS), |
56 | CHECK_MASK = CHECK_LEN - 1 |
57 | |
58 | }; |
59 | |
60 | struct Entry { |
61 | unsigned int pos = 0; |
62 | unsigned int len = 0; |
63 | unsigned int lock = 0; |
64 | unsigned int check = 0; |
65 | |
66 | inline void clear() { |
67 | pos = 0; |
68 | len = 0; |
69 | lock = 0; |
70 | check = 0; |
71 | } |
72 | Entry() {} |
73 | }; |
74 | |
75 | typedef int EntryArrayPos; |
76 | typedef int EntryIndicesPos; |
77 | |
78 | Entry *entry_array = nullptr; |
79 | int *entry_indices = nullptr; |
80 | int entry_max = 0; |
81 | int entry_count = 0; |
82 | |
83 | uint8_t *pool = nullptr; |
84 | void *mem_ptr = nullptr; |
85 | int pool_size = 0; |
86 | |
87 | int free_mem = 0; |
88 | int free_mem_peak = 0; |
89 | |
90 | unsigned int check_count = 0; |
91 | int align = 1; |
92 | |
93 | bool needs_locking = false; |
94 | |
95 | inline int entry_end(const Entry &p_entry) const { |
96 | return p_entry.pos + aligned(p_entry.len); |
97 | } |
98 | inline int aligned(int p_size) const { |
99 | int rem = p_size % align; |
100 | if (rem) { |
101 | p_size += align - rem; |
102 | } |
103 | |
104 | return p_size; |
105 | } |
106 | |
107 | void compact(int p_up_to = -1); |
108 | void compact_up(int p_from = 0); |
109 | bool get_free_entry(EntryArrayPos *p_pos); |
110 | bool find_hole(EntryArrayPos *p_pos, int p_for_size); |
111 | bool find_entry_index(EntryIndicesPos *p_map_pos, const Entry *p_entry); |
112 | Entry *get_entry(ID p_mem); |
113 | const Entry *get_entry(ID p_mem) const; |
114 | |
115 | void create_pool(void *p_mem, int p_size, int p_max_entries); |
116 | |
117 | protected: |
118 | virtual void mt_lock() const; ///< Reimplement for custom mt locking |
119 | virtual void mt_unlock() const; ///< Reimplement for custom mt locking |
120 | |
121 | public: |
122 | enum { |
123 | DEFAULT_MAX_ALLOCS = 4096, |
124 | }; |
125 | |
126 | ID alloc(int p_size); ///< Alloc memory, get an ID on success, POOL_ALOCATOR_INVALID_ID on failure |
127 | void free(ID p_mem); ///< Free allocated memory |
128 | Error resize(ID p_mem, int p_new_size); ///< resize a memory chunk |
129 | int get_size(ID p_mem) const; |
130 | |
131 | int get_free_mem(); ///< get free memory |
132 | int get_used_mem() const; |
133 | int get_free_peak(); ///< get free memory |
134 | |
135 | Error lock(ID p_mem); //@todo move this out |
136 | void *get(ID p_mem); |
137 | const void *get(ID p_mem) const; |
138 | void unlock(ID p_mem); |
139 | bool is_locked(ID p_mem) const; |
140 | |
141 | PoolAllocator(int p_size, bool p_needs_locking = false, int p_max_entries = DEFAULT_MAX_ALLOCS); |
142 | PoolAllocator(void *p_mem, int p_size, int p_align = 1, bool p_needs_locking = false, int p_max_entries = DEFAULT_MAX_ALLOCS); |
143 | PoolAllocator(int p_align, int p_size, bool p_needs_locking = false, int p_max_entries = DEFAULT_MAX_ALLOCS); |
144 | |
145 | virtual ~PoolAllocator(); |
146 | }; |
147 | |
148 | #endif // POOL_ALLOCATOR_H |
149 | |