1 | /**************************************************************************/ |
2 | /* memory.cpp */ |
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 | #include "memory.h" |
32 | |
33 | #include "core/error/error_macros.h" |
34 | #include "core/templates/safe_refcount.h" |
35 | |
36 | #include <stdio.h> |
37 | #include <stdlib.h> |
38 | |
39 | void *operator new(size_t p_size, const char *p_description) { |
40 | return Memory::alloc_static(p_size, false); |
41 | } |
42 | |
43 | void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)) { |
44 | return p_allocfunc(p_size); |
45 | } |
46 | |
47 | #ifdef _MSC_VER |
48 | void operator delete(void *p_mem, const char *p_description) { |
49 | CRASH_NOW_MSG("Call to placement delete should not happen." ); |
50 | } |
51 | |
52 | void operator delete(void *p_mem, void *(*p_allocfunc)(size_t p_size)) { |
53 | CRASH_NOW_MSG("Call to placement delete should not happen." ); |
54 | } |
55 | |
56 | void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_description) { |
57 | CRASH_NOW_MSG("Call to placement delete should not happen." ); |
58 | } |
59 | #endif |
60 | |
61 | #ifdef DEBUG_ENABLED |
62 | SafeNumeric<uint64_t> Memory::mem_usage; |
63 | SafeNumeric<uint64_t> Memory::max_usage; |
64 | #endif |
65 | |
66 | SafeNumeric<uint64_t> Memory::alloc_count; |
67 | |
68 | void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) { |
69 | #ifdef DEBUG_ENABLED |
70 | bool prepad = true; |
71 | #else |
72 | bool prepad = p_pad_align; |
73 | #endif |
74 | |
75 | void *mem = malloc(p_bytes + (prepad ? PAD_ALIGN : 0)); |
76 | |
77 | ERR_FAIL_NULL_V(mem, nullptr); |
78 | |
79 | alloc_count.increment(); |
80 | |
81 | if (prepad) { |
82 | uint64_t *s = (uint64_t *)mem; |
83 | *s = p_bytes; |
84 | |
85 | uint8_t *s8 = (uint8_t *)mem; |
86 | |
87 | #ifdef DEBUG_ENABLED |
88 | uint64_t new_mem_usage = mem_usage.add(p_bytes); |
89 | max_usage.exchange_if_greater(new_mem_usage); |
90 | #endif |
91 | return s8 + PAD_ALIGN; |
92 | } else { |
93 | return mem; |
94 | } |
95 | } |
96 | |
97 | void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) { |
98 | if (p_memory == nullptr) { |
99 | return alloc_static(p_bytes, p_pad_align); |
100 | } |
101 | |
102 | uint8_t *mem = (uint8_t *)p_memory; |
103 | |
104 | #ifdef DEBUG_ENABLED |
105 | bool prepad = true; |
106 | #else |
107 | bool prepad = p_pad_align; |
108 | #endif |
109 | |
110 | if (prepad) { |
111 | mem -= PAD_ALIGN; |
112 | uint64_t *s = (uint64_t *)mem; |
113 | |
114 | #ifdef DEBUG_ENABLED |
115 | if (p_bytes > *s) { |
116 | uint64_t new_mem_usage = mem_usage.add(p_bytes - *s); |
117 | max_usage.exchange_if_greater(new_mem_usage); |
118 | } else { |
119 | mem_usage.sub(*s - p_bytes); |
120 | } |
121 | #endif |
122 | |
123 | if (p_bytes == 0) { |
124 | free(mem); |
125 | return nullptr; |
126 | } else { |
127 | *s = p_bytes; |
128 | |
129 | mem = (uint8_t *)realloc(mem, p_bytes + PAD_ALIGN); |
130 | ERR_FAIL_NULL_V(mem, nullptr); |
131 | |
132 | s = (uint64_t *)mem; |
133 | |
134 | *s = p_bytes; |
135 | |
136 | return mem + PAD_ALIGN; |
137 | } |
138 | } else { |
139 | mem = (uint8_t *)realloc(mem, p_bytes); |
140 | |
141 | ERR_FAIL_COND_V(mem == nullptr && p_bytes > 0, nullptr); |
142 | |
143 | return mem; |
144 | } |
145 | } |
146 | |
147 | void Memory::free_static(void *p_ptr, bool p_pad_align) { |
148 | ERR_FAIL_NULL(p_ptr); |
149 | |
150 | uint8_t *mem = (uint8_t *)p_ptr; |
151 | |
152 | #ifdef DEBUG_ENABLED |
153 | bool prepad = true; |
154 | #else |
155 | bool prepad = p_pad_align; |
156 | #endif |
157 | |
158 | alloc_count.decrement(); |
159 | |
160 | if (prepad) { |
161 | mem -= PAD_ALIGN; |
162 | |
163 | #ifdef DEBUG_ENABLED |
164 | uint64_t *s = (uint64_t *)mem; |
165 | mem_usage.sub(*s); |
166 | #endif |
167 | |
168 | free(mem); |
169 | } else { |
170 | free(mem); |
171 | } |
172 | } |
173 | |
174 | uint64_t Memory::get_mem_available() { |
175 | return -1; // 0xFFFF... |
176 | } |
177 | |
178 | uint64_t Memory::get_mem_usage() { |
179 | #ifdef DEBUG_ENABLED |
180 | return mem_usage.get(); |
181 | #else |
182 | return 0; |
183 | #endif |
184 | } |
185 | |
186 | uint64_t Memory::get_mem_max_usage() { |
187 | #ifdef DEBUG_ENABLED |
188 | return max_usage.get(); |
189 | #else |
190 | return 0; |
191 | #endif |
192 | } |
193 | |
194 | _GlobalNil::_GlobalNil() { |
195 | left = this; |
196 | right = this; |
197 | parent = this; |
198 | } |
199 | |
200 | _GlobalNil _GlobalNilClass::_nil; |
201 | |