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