]>
Commit | Line | Data |
---|---|---|
7341eedb JF |
1 | /* Cycript - The Truly Universal Scripting Language |
2 | * Copyright (C) 2009-2016 Jay Freeman (saurik) | |
4644480a JF |
3 | */ |
4 | ||
f95d2598 | 5 | /* GNU Affero General Public License, Version 3 {{{ */ |
4644480a | 6 | /* |
f95d2598 JF |
7 | * This program is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU Affero General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | ||
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
c15969fd | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f95d2598 JF |
15 | * GNU Affero General Public License for more details. |
16 | ||
17 | * You should have received a copy of the GNU Affero General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
b3378a02 | 19 | **/ |
4644480a JF |
20 | /* }}} */ |
21 | ||
c5fa2867 JF |
22 | #ifndef CYCRIPT_POOLING_HPP |
23 | #define CYCRIPT_POOLING_HPP | |
5999c315 | 24 | |
b799113b | 25 | #include <cstdarg> |
0cbeddf8 | 26 | #include <cstdio> |
b799113b | 27 | #include <cstdlib> |
0cbeddf8 | 28 | #include <cstring> |
b799113b | 29 | |
0cbeddf8 JF |
30 | #include <algorithm> |
31 | ||
32 | #include <stdint.h> | |
5999c315 | 33 | |
37954781 | 34 | #include "Exception.hpp" |
2eb8215d | 35 | #include "Local.hpp" |
37954781 | 36 | #include "Standard.hpp" |
5999c315 | 37 | |
91fb60bf JF |
38 | // XXX: std::aligned_storage and alignof |
39 | static const size_t CYAlignment(sizeof(void *)); | |
40 | ||
41 | template <typename Type_> | |
42 | static void CYAlign(Type_ &data, size_t size) { | |
43 | data = reinterpret_cast<Type_>((reinterpret_cast<uintptr_t>(data) + (size - 1)) & ~static_cast<uintptr_t>(size - 1)); | |
44 | } | |
45 | ||
0cbeddf8 JF |
46 | class CYPool; |
47 | _finline void *operator new(size_t size, CYPool &pool); | |
48 | _finline void *operator new [](size_t size, CYPool &pool); | |
49 | ||
5999c315 JF |
50 | class CYPool { |
51 | private: | |
0cbeddf8 JF |
52 | uint8_t *data_; |
53 | size_t size_; | |
caab7369 | 54 | size_t next_; |
5999c315 | 55 | |
1560b2c8 JF |
56 | struct Cleaner { |
57 | Cleaner *next_; | |
58 | void (*code_)(void *); | |
59 | void *data_; | |
60 | ||
61 | Cleaner(Cleaner *next, void (*code)(void *), void *data) : | |
62 | next_(next), | |
63 | code_(code), | |
64 | data_(data) | |
65 | { | |
66 | } | |
67 | } *cleaner_; | |
5999c315 | 68 | |
57930129 JF |
69 | template <typename Type_> |
70 | static void delete_(void *data) { | |
71 | reinterpret_cast<Type_ *>(data)->~Type_(); | |
72 | } | |
73 | ||
0cbeddf8 JF |
74 | CYPool(const CYPool &); |
75 | ||
1560b2c8 | 76 | public: |
caab7369 | 77 | CYPool(size_t next = 64) : |
0cbeddf8 JF |
78 | data_(NULL), |
79 | size_(0), | |
caab7369 | 80 | next_(next), |
1560b2c8 | 81 | cleaner_(NULL) |
b799113b JF |
82 | { |
83 | } | |
84 | ||
5999c315 | 85 | ~CYPool() { |
0cbeddf8 JF |
86 | for (Cleaner *cleaner(cleaner_); cleaner != NULL; ) { |
87 | Cleaner *next(cleaner->next_); | |
1560b2c8 | 88 | (*cleaner->code_)(cleaner->data_); |
0cbeddf8 JF |
89 | cleaner = next; |
90 | } | |
5999c315 JF |
91 | } |
92 | ||
0cbeddf8 | 93 | template <typename Type_> |
91fb60bf JF |
94 | Type_ *malloc(size_t size, size_t alignment = CYAlignment) { |
95 | uint8_t *end(data_); | |
96 | CYAlign(end, alignment); | |
97 | end += size; | |
98 | ||
99 | if (size_t(end - data_) > size_) { | |
100 | size_t need(sizeof(Cleaner)); | |
101 | CYAlign(need, alignment); | |
102 | need += size; | |
103 | size_ = std::max<size_t>(next_, need); | |
caab7369 | 104 | next_ *= 2; |
0cbeddf8 JF |
105 | data_ = reinterpret_cast<uint8_t *>(::malloc(size_)); |
106 | atexit(free, data_); | |
107 | _assert(size <= size_); | |
108 | } | |
b1ff2d78 | 109 | |
91fb60bf JF |
110 | uint8_t *data(data_); |
111 | CYAlign(data, alignment); | |
112 | end = data + size; | |
113 | size_ -= end - data_; | |
114 | data_ = end; | |
0cbeddf8 | 115 | return reinterpret_cast<Type_ *>(data); |
5999c315 JF |
116 | } |
117 | ||
f2c357f9 JF |
118 | template <typename Type_> |
119 | Type_ *calloc(size_t count, size_t size, size_t alignment = CYAlignment) { | |
120 | Type_ *data(malloc<Type_>(count * size, alignment)); | |
121 | memset(data, 0, count * size); | |
122 | return data; | |
123 | } | |
124 | ||
0cbeddf8 | 125 | char *strdup(const char *data) { |
5963ec5b JF |
126 | if (data == NULL) |
127 | return NULL; | |
1f19cfb6 | 128 | return memdup(data, strlen(data) + 1, 1); |
b799113b JF |
129 | } |
130 | ||
35cad40b | 131 | template <typename Type_> |
91fb60bf JF |
132 | Type_ *memdup(const Type_ *data, size_t size, size_t alignment = CYAlignment) { |
133 | Type_ *copy(malloc<Type_>(size, alignment)); | |
0cbeddf8 JF |
134 | memcpy(copy, data, size); |
135 | return copy; | |
5999c315 JF |
136 | } |
137 | ||
5d5d70c0 | 138 | char *strndup(const char *data, size_t size) { |
0cbeddf8 | 139 | return strmemdup(data, strnlen(data, size)); |
5999c315 | 140 | } |
b799113b | 141 | |
5d5d70c0 | 142 | char *strmemdup(const char *data, size_t size) { |
91fb60bf | 143 | char *copy(malloc<char>(size + 1, 1)); |
0cbeddf8 JF |
144 | memcpy(copy, data, size); |
145 | copy[size] = '\0'; | |
146 | return copy; | |
b799113b JF |
147 | } |
148 | ||
0cbeddf8 JF |
149 | // XXX: this could be made much more efficient |
150 | __attribute__((__sentinel__)) | |
151 | char *strcat(const char *data, ...) { | |
152 | size_t size(strlen(data)); { | |
153 | va_list args; | |
154 | va_start(args, data); | |
155 | ||
156 | while (const char *arg = va_arg(args, const char *)) | |
157 | size += strlen(arg); | |
158 | ||
159 | va_end(args); | |
160 | } | |
161 | ||
91fb60bf | 162 | char *copy(malloc<char>(size + 1, 1)); { |
0cbeddf8 JF |
163 | va_list args; |
164 | va_start(args, data); | |
165 | ||
166 | size_t offset(strlen(data)); | |
167 | memcpy(copy, data, offset); | |
168 | ||
169 | while (const char *arg = va_arg(args, const char *)) { | |
170 | size_t size(strlen(arg)); | |
171 | memcpy(copy + offset, arg, size); | |
172 | offset += size; | |
173 | } | |
174 | ||
175 | va_end(args); | |
176 | } | |
177 | ||
178 | copy[size] = '\0'; | |
179 | return copy; | |
180 | } | |
181 | ||
182 | // XXX: most people using this might should use sprintf | |
183 | char *itoa(long value) { | |
184 | return sprintf(16, "%ld", value); | |
185 | } | |
186 | ||
187 | __attribute__((__format__(__printf__, 3, 4))) | |
188 | char *sprintf(size_t size, const char *format, ...) { | |
b799113b JF |
189 | va_list args; |
190 | va_start(args, format); | |
0cbeddf8 | 191 | char *copy(vsprintf(size, format, args)); |
b799113b | 192 | va_end(args); |
0cbeddf8 | 193 | return copy; |
b799113b JF |
194 | } |
195 | ||
0cbeddf8 JF |
196 | char *vsprintf(size_t size, const char *format, va_list args) { |
197 | va_list copy; | |
198 | va_copy(copy, args); | |
199 | char buffer[size]; | |
200 | int writ(vsnprintf(buffer, size, format, copy)); | |
201 | va_end(copy); | |
202 | _assert(writ >= 0); | |
203 | ||
204 | if (size_t(writ) >= size) | |
205 | return vsprintf(writ + 1, format, args); | |
206 | return strmemdup(buffer, writ); | |
b799113b | 207 | } |
1560b2c8 JF |
208 | |
209 | void atexit(void (*code)(void *), void *data = NULL); | |
57930129 JF |
210 | |
211 | template <typename Type_> | |
212 | Type_ &object() { | |
213 | Type_ *value(new(*this) Type_()); | |
214 | atexit(&delete_<Type_>, value); | |
215 | return *value; | |
216 | } | |
5999c315 JF |
217 | }; |
218 | ||
b799113b | 219 | _finline void *operator new(size_t size, CYPool &pool) { |
0cbeddf8 | 220 | return pool.malloc<void>(size); |
b799113b JF |
221 | } |
222 | ||
223 | _finline void *operator new [](size_t size, CYPool &pool) { | |
0cbeddf8 | 224 | return pool.malloc<void>(size); |
b799113b JF |
225 | } |
226 | ||
1560b2c8 JF |
227 | _finline void CYPool::atexit(void (*code)(void *), void *data) { |
228 | cleaner_ = new(*this) Cleaner(cleaner_, code, data); | |
229 | } | |
230 | ||
1ef7d061 | 231 | struct CYData { |
b799113b | 232 | CYPool *pool_; |
1850a470 JF |
233 | unsigned count_; |
234 | ||
235 | CYData() : | |
236 | count_(1) | |
237 | { | |
9e7cf36b | 238 | _assert(pool_ != NULL); |
1850a470 | 239 | } |
1ef7d061 | 240 | |
b799113b JF |
241 | CYData(CYPool &pool) : |
242 | pool_(&pool), | |
243 | count_(_not(unsigned)) | |
244 | { | |
245 | } | |
246 | ||
1ef7d061 JF |
247 | virtual ~CYData() { |
248 | } | |
249 | ||
b799113b | 250 | static void *operator new(size_t size, CYPool &pool) { |
0cbeddf8 | 251 | void *data(pool.malloc<void>(size)); |
b799113b | 252 | reinterpret_cast<CYData *>(data)->pool_ = &pool; |
1ef7d061 JF |
253 | return data; |
254 | } | |
255 | ||
256 | static void *operator new(size_t size) { | |
b799113b | 257 | return operator new(size, *new CYPool()); |
1ef7d061 JF |
258 | } |
259 | ||
260 | static void operator delete(void *data) { | |
b799113b | 261 | delete reinterpret_cast<CYData *>(data)->pool_; |
1ef7d061 | 262 | } |
1ef7d061 JF |
263 | }; |
264 | ||
a846a8cd JF |
265 | template <typename Type_> |
266 | struct CYPoolAllocator { | |
b799113b | 267 | CYPool *pool_; |
a846a8cd JF |
268 | |
269 | typedef Type_ value_type; | |
270 | typedef value_type *pointer; | |
271 | typedef const value_type *const_pointer; | |
272 | typedef value_type &reference; | |
273 | typedef const value_type &const_reference; | |
274 | typedef std::size_t size_type; | |
275 | typedef std::ptrdiff_t difference_type; | |
276 | ||
277 | CYPoolAllocator() : | |
278 | pool_(NULL) | |
279 | { | |
280 | } | |
281 | ||
282 | template <typename Right_> | |
283 | CYPoolAllocator(const CYPoolAllocator<Right_> &rhs) : | |
284 | pool_(rhs.pool_) | |
285 | { | |
286 | } | |
287 | ||
288 | pointer allocate(size_type size, const void *hint = 0) { | |
0cbeddf8 | 289 | return pool_->malloc<value_type>(size); |
a846a8cd JF |
290 | } |
291 | ||
292 | void deallocate(pointer data, size_type size) { | |
293 | } | |
294 | ||
295 | void construct(pointer address, const Type_ &rhs) { | |
296 | new(address) Type_(rhs); | |
297 | } | |
298 | ||
299 | void destroy(pointer address) { | |
300 | address->~Type_(); | |
301 | } | |
302 | ||
303 | template <typename Right_> | |
304 | inline bool operator==(const CYPoolAllocator<Right_> &rhs) { | |
305 | return pool_ == rhs.pool_; | |
306 | } | |
307 | ||
308 | template <typename Right_> | |
309 | inline bool operator!=(const CYPoolAllocator<Right_> &rhs) { | |
310 | return !operator==(rhs); | |
311 | } | |
312 | ||
313 | template <typename Right_> | |
314 | struct rebind { | |
315 | typedef CYPoolAllocator<Right_> other; | |
316 | }; | |
317 | }; | |
318 | ||
2eb8215d JF |
319 | class CYLocalPool : |
320 | public CYPool | |
321 | { | |
322 | private: | |
b799113b | 323 | CYLocal<CYPool> local_; |
2eb8215d JF |
324 | |
325 | public: | |
326 | CYLocalPool() : | |
327 | CYPool(), | |
b799113b | 328 | local_(this) |
2eb8215d JF |
329 | { |
330 | } | |
331 | }; | |
332 | ||
333 | #define $pool \ | |
b799113b | 334 | (*CYLocal<CYPool>::Get()) |
2eb8215d | 335 | |
fa3c5be8 JF |
336 | template <> |
337 | ::pthread_key_t CYLocal<CYPool>::key_; | |
338 | ||
c5fa2867 | 339 | #endif/*CYCRIPT_POOLING_HPP*/ |