]>
Commit | Line | Data |
---|---|---|
b3378a02 | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
c15969fd | 2 | * Copyright (C) 2009-2013 Jay Freeman (saurik) |
4644480a JF |
3 | */ |
4 | ||
c15969fd | 5 | /* GNU General Public License, Version 3 {{{ */ |
4644480a | 6 | /* |
c15969fd JF |
7 | * Cycript is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published | |
9 | * by the Free Software Foundation, either version 3 of the License, | |
10 | * or (at your option) any later version. | |
4644480a | 11 | * |
c15969fd JF |
12 | * Cycript is distributed in the hope that it will be useful, but |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
4644480a | 16 | * |
c15969fd | 17 | * You should have received a copy of the GNU General Public License |
b3378a02 JF |
18 | * along with Cycript. If not, see <http://www.gnu.org/licenses/>. |
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_; | |
5999c315 | 46 | |
1560b2c8 JF |
47 | struct Cleaner { |
48 | Cleaner *next_; | |
49 | void (*code_)(void *); | |
50 | void *data_; | |
51 | ||
52 | Cleaner(Cleaner *next, void (*code)(void *), void *data) : | |
53 | next_(next), | |
54 | code_(code), | |
55 | data_(data) | |
56 | { | |
57 | } | |
58 | } *cleaner_; | |
5999c315 | 59 | |
0cbeddf8 JF |
60 | static _finline size_t align(size_t size) { |
61 | // XXX: alignment is more complex than this | |
62 | return (size + 7) & ~0x3; | |
63 | } | |
64 | ||
65 | CYPool(const CYPool &); | |
66 | ||
1560b2c8 JF |
67 | public: |
68 | CYPool() : | |
0cbeddf8 JF |
69 | data_(NULL), |
70 | size_(0), | |
1560b2c8 | 71 | cleaner_(NULL) |
b799113b JF |
72 | { |
73 | } | |
74 | ||
5999c315 | 75 | ~CYPool() { |
0cbeddf8 JF |
76 | for (Cleaner *cleaner(cleaner_); cleaner != NULL; ) { |
77 | Cleaner *next(cleaner->next_); | |
1560b2c8 | 78 | (*cleaner->code_)(cleaner->data_); |
0cbeddf8 JF |
79 | cleaner = next; |
80 | } | |
5999c315 JF |
81 | } |
82 | ||
0cbeddf8 JF |
83 | template <typename Type_> |
84 | Type_ *malloc(size_t size) { | |
85 | size = align(size); | |
86 | ||
87 | if (size > size_) { | |
88 | // XXX: is this an optimal malloc size? | |
89 | size_ = std::max<size_t>(size, size + align(sizeof(Cleaner))); | |
90 | data_ = reinterpret_cast<uint8_t *>(::malloc(size_)); | |
91 | atexit(free, data_); | |
92 | _assert(size <= size_); | |
93 | } | |
b1ff2d78 | 94 | |
0cbeddf8 JF |
95 | void *data(data_); |
96 | data_ += size; | |
97 | size_ -= size; | |
98 | return reinterpret_cast<Type_ *>(data); | |
5999c315 JF |
99 | } |
100 | ||
0cbeddf8 JF |
101 | char *strdup(const char *data) { |
102 | return reinterpret_cast<char *>(memdup(data, strlen(data) + 1)); | |
b799113b JF |
103 | } |
104 | ||
0cbeddf8 JF |
105 | void *memdup(const void *data, size_t size) { |
106 | void *copy(malloc<void>(size)); | |
107 | memcpy(copy, data, size); | |
108 | return copy; | |
5999c315 JF |
109 | } |
110 | ||
b799113b | 111 | char *strndup(const char *data, size_t size) const { |
0cbeddf8 | 112 | return strmemdup(data, strnlen(data, size)); |
5999c315 | 113 | } |
b799113b JF |
114 | |
115 | char *strmemdup(const char *data, size_t size) const { | |
0cbeddf8 JF |
116 | char *copy(new char[size + 1]); |
117 | memcpy(copy, data, size); | |
118 | copy[size] = '\0'; | |
119 | return copy; | |
b799113b JF |
120 | } |
121 | ||
0cbeddf8 JF |
122 | // XXX: this could be made much more efficient |
123 | __attribute__((__sentinel__)) | |
124 | char *strcat(const char *data, ...) { | |
125 | size_t size(strlen(data)); { | |
126 | va_list args; | |
127 | va_start(args, data); | |
128 | ||
129 | while (const char *arg = va_arg(args, const char *)) | |
130 | size += strlen(arg); | |
131 | ||
132 | va_end(args); | |
133 | } | |
134 | ||
135 | char *copy(malloc<char>(size + 1)); { | |
136 | va_list args; | |
137 | va_start(args, data); | |
138 | ||
139 | size_t offset(strlen(data)); | |
140 | memcpy(copy, data, offset); | |
141 | ||
142 | while (const char *arg = va_arg(args, const char *)) { | |
143 | size_t size(strlen(arg)); | |
144 | memcpy(copy + offset, arg, size); | |
145 | offset += size; | |
146 | } | |
147 | ||
148 | va_end(args); | |
149 | } | |
150 | ||
151 | copy[size] = '\0'; | |
152 | return copy; | |
153 | } | |
154 | ||
155 | // XXX: most people using this might should use sprintf | |
156 | char *itoa(long value) { | |
157 | return sprintf(16, "%ld", value); | |
158 | } | |
159 | ||
160 | __attribute__((__format__(__printf__, 3, 4))) | |
161 | char *sprintf(size_t size, const char *format, ...) { | |
b799113b JF |
162 | va_list args; |
163 | va_start(args, format); | |
0cbeddf8 | 164 | char *copy(vsprintf(size, format, args)); |
b799113b | 165 | va_end(args); |
0cbeddf8 | 166 | return copy; |
b799113b JF |
167 | } |
168 | ||
0cbeddf8 JF |
169 | char *vsprintf(size_t size, const char *format, va_list args) { |
170 | va_list copy; | |
171 | va_copy(copy, args); | |
172 | char buffer[size]; | |
173 | int writ(vsnprintf(buffer, size, format, copy)); | |
174 | va_end(copy); | |
175 | _assert(writ >= 0); | |
176 | ||
177 | if (size_t(writ) >= size) | |
178 | return vsprintf(writ + 1, format, args); | |
179 | return strmemdup(buffer, writ); | |
b799113b | 180 | } |
1560b2c8 JF |
181 | |
182 | void atexit(void (*code)(void *), void *data = NULL); | |
5999c315 JF |
183 | }; |
184 | ||
b799113b | 185 | _finline void *operator new(size_t size, CYPool &pool) { |
0cbeddf8 | 186 | return pool.malloc<void>(size); |
b799113b JF |
187 | } |
188 | ||
189 | _finline void *operator new [](size_t size, CYPool &pool) { | |
0cbeddf8 | 190 | return pool.malloc<void>(size); |
b799113b JF |
191 | } |
192 | ||
1560b2c8 JF |
193 | _finline void CYPool::atexit(void (*code)(void *), void *data) { |
194 | cleaner_ = new(*this) Cleaner(cleaner_, code, data); | |
195 | } | |
196 | ||
1ef7d061 | 197 | struct CYData { |
b799113b | 198 | CYPool *pool_; |
1850a470 JF |
199 | unsigned count_; |
200 | ||
201 | CYData() : | |
202 | count_(1) | |
203 | { | |
204 | } | |
1ef7d061 | 205 | |
b799113b JF |
206 | CYData(CYPool &pool) : |
207 | pool_(&pool), | |
208 | count_(_not(unsigned)) | |
209 | { | |
210 | } | |
211 | ||
1ef7d061 JF |
212 | virtual ~CYData() { |
213 | } | |
214 | ||
b799113b | 215 | static void *operator new(size_t size, CYPool &pool) { |
0cbeddf8 | 216 | void *data(pool.malloc<void>(size)); |
b799113b | 217 | reinterpret_cast<CYData *>(data)->pool_ = &pool; |
1ef7d061 JF |
218 | return data; |
219 | } | |
220 | ||
221 | static void *operator new(size_t size) { | |
b799113b | 222 | return operator new(size, *new CYPool()); |
1ef7d061 JF |
223 | } |
224 | ||
225 | static void operator delete(void *data) { | |
b799113b | 226 | delete reinterpret_cast<CYData *>(data)->pool_; |
1ef7d061 | 227 | } |
1ef7d061 JF |
228 | }; |
229 | ||
a846a8cd JF |
230 | template <typename Type_> |
231 | struct CYPoolAllocator { | |
b799113b | 232 | CYPool *pool_; |
a846a8cd JF |
233 | |
234 | typedef Type_ value_type; | |
235 | typedef value_type *pointer; | |
236 | typedef const value_type *const_pointer; | |
237 | typedef value_type &reference; | |
238 | typedef const value_type &const_reference; | |
239 | typedef std::size_t size_type; | |
240 | typedef std::ptrdiff_t difference_type; | |
241 | ||
242 | CYPoolAllocator() : | |
243 | pool_(NULL) | |
244 | { | |
245 | } | |
246 | ||
247 | template <typename Right_> | |
248 | CYPoolAllocator(const CYPoolAllocator<Right_> &rhs) : | |
249 | pool_(rhs.pool_) | |
250 | { | |
251 | } | |
252 | ||
253 | pointer allocate(size_type size, const void *hint = 0) { | |
0cbeddf8 | 254 | return pool_->malloc<value_type>(size); |
a846a8cd JF |
255 | } |
256 | ||
257 | void deallocate(pointer data, size_type size) { | |
258 | } | |
259 | ||
260 | void construct(pointer address, const Type_ &rhs) { | |
261 | new(address) Type_(rhs); | |
262 | } | |
263 | ||
264 | void destroy(pointer address) { | |
265 | address->~Type_(); | |
266 | } | |
267 | ||
268 | template <typename Right_> | |
269 | inline bool operator==(const CYPoolAllocator<Right_> &rhs) { | |
270 | return pool_ == rhs.pool_; | |
271 | } | |
272 | ||
273 | template <typename Right_> | |
274 | inline bool operator!=(const CYPoolAllocator<Right_> &rhs) { | |
275 | return !operator==(rhs); | |
276 | } | |
277 | ||
278 | template <typename Right_> | |
279 | struct rebind { | |
280 | typedef CYPoolAllocator<Right_> other; | |
281 | }; | |
282 | }; | |
283 | ||
2eb8215d JF |
284 | class CYLocalPool : |
285 | public CYPool | |
286 | { | |
287 | private: | |
b799113b | 288 | CYLocal<CYPool> local_; |
2eb8215d JF |
289 | |
290 | public: | |
291 | CYLocalPool() : | |
292 | CYPool(), | |
b799113b | 293 | local_(this) |
2eb8215d JF |
294 | { |
295 | } | |
296 | }; | |
297 | ||
298 | #define $pool \ | |
b799113b | 299 | (*CYLocal<CYPool>::Get()) |
2eb8215d | 300 | |
c5fa2867 | 301 | #endif/*CYCRIPT_POOLING_HPP*/ |