]>
Commit | Line | Data |
---|---|---|
b3378a02 | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
c1d3e52e | 2 | * Copyright (C) 2009-2015 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 | |
0cbeddf8 JF |
38 | class CYPool; |
39 | _finline void *operator new(size_t size, CYPool &pool); | |
40 | _finline void *operator new [](size_t size, CYPool &pool); | |
41 | ||
5999c315 JF |
42 | class CYPool { |
43 | private: | |
0cbeddf8 JF |
44 | uint8_t *data_; |
45 | size_t size_; | |
caab7369 | 46 | size_t next_; |
5999c315 | 47 | |
1560b2c8 JF |
48 | struct Cleaner { |
49 | Cleaner *next_; | |
50 | void (*code_)(void *); | |
51 | void *data_; | |
52 | ||
53 | Cleaner(Cleaner *next, void (*code)(void *), void *data) : | |
54 | next_(next), | |
55 | code_(code), | |
56 | data_(data) | |
57 | { | |
58 | } | |
59 | } *cleaner_; | |
5999c315 | 60 | |
0cbeddf8 JF |
61 | static _finline size_t align(size_t size) { |
62 | // XXX: alignment is more complex than this | |
63 | return (size + 7) & ~0x3; | |
64 | } | |
65 | ||
57930129 JF |
66 | template <typename Type_> |
67 | static void delete_(void *data) { | |
68 | reinterpret_cast<Type_ *>(data)->~Type_(); | |
69 | } | |
70 | ||
0cbeddf8 JF |
71 | CYPool(const CYPool &); |
72 | ||
1560b2c8 | 73 | public: |
caab7369 | 74 | CYPool(size_t next = 64) : |
0cbeddf8 JF |
75 | data_(NULL), |
76 | size_(0), | |
caab7369 | 77 | next_(next), |
1560b2c8 | 78 | cleaner_(NULL) |
b799113b JF |
79 | { |
80 | } | |
81 | ||
5999c315 | 82 | ~CYPool() { |
0cbeddf8 JF |
83 | for (Cleaner *cleaner(cleaner_); cleaner != NULL; ) { |
84 | Cleaner *next(cleaner->next_); | |
1560b2c8 | 85 | (*cleaner->code_)(cleaner->data_); |
0cbeddf8 JF |
86 | cleaner = next; |
87 | } | |
5999c315 JF |
88 | } |
89 | ||
0cbeddf8 JF |
90 | template <typename Type_> |
91 | Type_ *malloc(size_t size) { | |
92 | size = align(size); | |
93 | ||
94 | if (size > size_) { | |
caab7369 JF |
95 | size_ = std::max<size_t>(next_, size + align(sizeof(Cleaner))); |
96 | next_ *= 2; | |
0cbeddf8 JF |
97 | data_ = reinterpret_cast<uint8_t *>(::malloc(size_)); |
98 | atexit(free, data_); | |
99 | _assert(size <= size_); | |
100 | } | |
b1ff2d78 | 101 | |
0cbeddf8 JF |
102 | void *data(data_); |
103 | data_ += size; | |
104 | size_ -= size; | |
105 | return reinterpret_cast<Type_ *>(data); | |
5999c315 JF |
106 | } |
107 | ||
0cbeddf8 | 108 | char *strdup(const char *data) { |
5963ec5b JF |
109 | if (data == NULL) |
110 | return NULL; | |
0cbeddf8 | 111 | return reinterpret_cast<char *>(memdup(data, strlen(data) + 1)); |
b799113b JF |
112 | } |
113 | ||
35cad40b JF |
114 | template <typename Type_> |
115 | Type_ *memdup(const Type_ *data, size_t size) { | |
116 | Type_ *copy(malloc<Type_>(size)); | |
0cbeddf8 JF |
117 | memcpy(copy, data, size); |
118 | return copy; | |
5999c315 JF |
119 | } |
120 | ||
5d5d70c0 | 121 | char *strndup(const char *data, size_t size) { |
0cbeddf8 | 122 | return strmemdup(data, strnlen(data, size)); |
5999c315 | 123 | } |
b799113b | 124 | |
5d5d70c0 JF |
125 | char *strmemdup(const char *data, size_t size) { |
126 | char *copy(malloc<char>(size + 1)); | |
0cbeddf8 JF |
127 | memcpy(copy, data, size); |
128 | copy[size] = '\0'; | |
129 | return copy; | |
b799113b JF |
130 | } |
131 | ||
0cbeddf8 JF |
132 | // XXX: this could be made much more efficient |
133 | __attribute__((__sentinel__)) | |
134 | char *strcat(const char *data, ...) { | |
135 | size_t size(strlen(data)); { | |
136 | va_list args; | |
137 | va_start(args, data); | |
138 | ||
139 | while (const char *arg = va_arg(args, const char *)) | |
140 | size += strlen(arg); | |
141 | ||
142 | va_end(args); | |
143 | } | |
144 | ||
145 | char *copy(malloc<char>(size + 1)); { | |
146 | va_list args; | |
147 | va_start(args, data); | |
148 | ||
149 | size_t offset(strlen(data)); | |
150 | memcpy(copy, data, offset); | |
151 | ||
152 | while (const char *arg = va_arg(args, const char *)) { | |
153 | size_t size(strlen(arg)); | |
154 | memcpy(copy + offset, arg, size); | |
155 | offset += size; | |
156 | } | |
157 | ||
158 | va_end(args); | |
159 | } | |
160 | ||
161 | copy[size] = '\0'; | |
162 | return copy; | |
163 | } | |
164 | ||
165 | // XXX: most people using this might should use sprintf | |
166 | char *itoa(long value) { | |
167 | return sprintf(16, "%ld", value); | |
168 | } | |
169 | ||
170 | __attribute__((__format__(__printf__, 3, 4))) | |
171 | char *sprintf(size_t size, const char *format, ...) { | |
b799113b JF |
172 | va_list args; |
173 | va_start(args, format); | |
0cbeddf8 | 174 | char *copy(vsprintf(size, format, args)); |
b799113b | 175 | va_end(args); |
0cbeddf8 | 176 | return copy; |
b799113b JF |
177 | } |
178 | ||
0cbeddf8 JF |
179 | char *vsprintf(size_t size, const char *format, va_list args) { |
180 | va_list copy; | |
181 | va_copy(copy, args); | |
182 | char buffer[size]; | |
183 | int writ(vsnprintf(buffer, size, format, copy)); | |
184 | va_end(copy); | |
185 | _assert(writ >= 0); | |
186 | ||
187 | if (size_t(writ) >= size) | |
188 | return vsprintf(writ + 1, format, args); | |
189 | return strmemdup(buffer, writ); | |
b799113b | 190 | } |
1560b2c8 JF |
191 | |
192 | void atexit(void (*code)(void *), void *data = NULL); | |
57930129 JF |
193 | |
194 | template <typename Type_> | |
195 | Type_ &object() { | |
196 | Type_ *value(new(*this) Type_()); | |
197 | atexit(&delete_<Type_>, value); | |
198 | return *value; | |
199 | } | |
5999c315 JF |
200 | }; |
201 | ||
b799113b | 202 | _finline void *operator new(size_t size, CYPool &pool) { |
0cbeddf8 | 203 | return pool.malloc<void>(size); |
b799113b JF |
204 | } |
205 | ||
206 | _finline void *operator new [](size_t size, CYPool &pool) { | |
0cbeddf8 | 207 | return pool.malloc<void>(size); |
b799113b JF |
208 | } |
209 | ||
1560b2c8 JF |
210 | _finline void CYPool::atexit(void (*code)(void *), void *data) { |
211 | cleaner_ = new(*this) Cleaner(cleaner_, code, data); | |
212 | } | |
213 | ||
1ef7d061 | 214 | struct CYData { |
b799113b | 215 | CYPool *pool_; |
1850a470 JF |
216 | unsigned count_; |
217 | ||
218 | CYData() : | |
219 | count_(1) | |
220 | { | |
221 | } | |
1ef7d061 | 222 | |
b799113b JF |
223 | CYData(CYPool &pool) : |
224 | pool_(&pool), | |
225 | count_(_not(unsigned)) | |
226 | { | |
227 | } | |
228 | ||
1ef7d061 JF |
229 | virtual ~CYData() { |
230 | } | |
231 | ||
b799113b | 232 | static void *operator new(size_t size, CYPool &pool) { |
0cbeddf8 | 233 | void *data(pool.malloc<void>(size)); |
b799113b | 234 | reinterpret_cast<CYData *>(data)->pool_ = &pool; |
1ef7d061 JF |
235 | return data; |
236 | } | |
237 | ||
238 | static void *operator new(size_t size) { | |
b799113b | 239 | return operator new(size, *new CYPool()); |
1ef7d061 JF |
240 | } |
241 | ||
242 | static void operator delete(void *data) { | |
b799113b | 243 | delete reinterpret_cast<CYData *>(data)->pool_; |
1ef7d061 | 244 | } |
1ef7d061 JF |
245 | }; |
246 | ||
a846a8cd JF |
247 | template <typename Type_> |
248 | struct CYPoolAllocator { | |
b799113b | 249 | CYPool *pool_; |
a846a8cd JF |
250 | |
251 | typedef Type_ value_type; | |
252 | typedef value_type *pointer; | |
253 | typedef const value_type *const_pointer; | |
254 | typedef value_type &reference; | |
255 | typedef const value_type &const_reference; | |
256 | typedef std::size_t size_type; | |
257 | typedef std::ptrdiff_t difference_type; | |
258 | ||
259 | CYPoolAllocator() : | |
260 | pool_(NULL) | |
261 | { | |
262 | } | |
263 | ||
264 | template <typename Right_> | |
265 | CYPoolAllocator(const CYPoolAllocator<Right_> &rhs) : | |
266 | pool_(rhs.pool_) | |
267 | { | |
268 | } | |
269 | ||
270 | pointer allocate(size_type size, const void *hint = 0) { | |
0cbeddf8 | 271 | return pool_->malloc<value_type>(size); |
a846a8cd JF |
272 | } |
273 | ||
274 | void deallocate(pointer data, size_type size) { | |
275 | } | |
276 | ||
277 | void construct(pointer address, const Type_ &rhs) { | |
278 | new(address) Type_(rhs); | |
279 | } | |
280 | ||
281 | void destroy(pointer address) { | |
282 | address->~Type_(); | |
283 | } | |
284 | ||
285 | template <typename Right_> | |
286 | inline bool operator==(const CYPoolAllocator<Right_> &rhs) { | |
287 | return pool_ == rhs.pool_; | |
288 | } | |
289 | ||
290 | template <typename Right_> | |
291 | inline bool operator!=(const CYPoolAllocator<Right_> &rhs) { | |
292 | return !operator==(rhs); | |
293 | } | |
294 | ||
295 | template <typename Right_> | |
296 | struct rebind { | |
297 | typedef CYPoolAllocator<Right_> other; | |
298 | }; | |
299 | }; | |
300 | ||
2eb8215d JF |
301 | class CYLocalPool : |
302 | public CYPool | |
303 | { | |
304 | private: | |
b799113b | 305 | CYLocal<CYPool> local_; |
2eb8215d JF |
306 | |
307 | public: | |
308 | CYLocalPool() : | |
309 | CYPool(), | |
b799113b | 310 | local_(this) |
2eb8215d JF |
311 | { |
312 | } | |
313 | }; | |
314 | ||
315 | #define $pool \ | |
b799113b | 316 | (*CYLocal<CYPool>::Get()) |
2eb8215d | 317 | |
fa3c5be8 JF |
318 | template <> |
319 | ::pthread_key_t CYLocal<CYPool>::key_; | |
320 | ||
c5fa2867 | 321 | #endif/*CYCRIPT_POOLING_HPP*/ |