]> git.saurik.com Git - cycript.git/blob - Pooling.hpp
-[NSCFType cy$toJSON:inContext:] 64-bit encoding.
[cycript.git] / Pooling.hpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
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.
11 *
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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. 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
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_;
59
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
67 public:
68 CYPool() :
69 data_(NULL),
70 size_(0),
71 cleaner_(NULL)
72 {
73 }
74
75 ~CYPool() {
76 for (Cleaner *cleaner(cleaner_); cleaner != NULL; ) {
77 Cleaner *next(cleaner->next_);
78 (*cleaner->code_)(cleaner->data_);
79 cleaner = next;
80 }
81 }
82
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 }
94
95 void *data(data_);
96 data_ += size;
97 size_ -= size;
98 return reinterpret_cast<Type_ *>(data);
99 }
100
101 char *strdup(const char *data) {
102 if (data == NULL)
103 return NULL;
104 return reinterpret_cast<char *>(memdup(data, strlen(data) + 1));
105 }
106
107 void *memdup(const void *data, size_t size) {
108 void *copy(malloc<void>(size));
109 memcpy(copy, data, size);
110 return copy;
111 }
112
113 char *strndup(const char *data, size_t size) const {
114 return strmemdup(data, strnlen(data, size));
115 }
116
117 char *strmemdup(const char *data, size_t size) const {
118 char *copy(new char[size + 1]);
119 memcpy(copy, data, size);
120 copy[size] = '\0';
121 return copy;
122 }
123
124 // XXX: this could be made much more efficient
125 __attribute__((__sentinel__))
126 char *strcat(const char *data, ...) {
127 size_t size(strlen(data)); {
128 va_list args;
129 va_start(args, data);
130
131 while (const char *arg = va_arg(args, const char *))
132 size += strlen(arg);
133
134 va_end(args);
135 }
136
137 char *copy(malloc<char>(size + 1)); {
138 va_list args;
139 va_start(args, data);
140
141 size_t offset(strlen(data));
142 memcpy(copy, data, offset);
143
144 while (const char *arg = va_arg(args, const char *)) {
145 size_t size(strlen(arg));
146 memcpy(copy + offset, arg, size);
147 offset += size;
148 }
149
150 va_end(args);
151 }
152
153 copy[size] = '\0';
154 return copy;
155 }
156
157 // XXX: most people using this might should use sprintf
158 char *itoa(long value) {
159 return sprintf(16, "%ld", value);
160 }
161
162 __attribute__((__format__(__printf__, 3, 4)))
163 char *sprintf(size_t size, const char *format, ...) {
164 va_list args;
165 va_start(args, format);
166 char *copy(vsprintf(size, format, args));
167 va_end(args);
168 return copy;
169 }
170
171 char *vsprintf(size_t size, const char *format, va_list args) {
172 va_list copy;
173 va_copy(copy, args);
174 char buffer[size];
175 int writ(vsnprintf(buffer, size, format, copy));
176 va_end(copy);
177 _assert(writ >= 0);
178
179 if (size_t(writ) >= size)
180 return vsprintf(writ + 1, format, args);
181 return strmemdup(buffer, writ);
182 }
183
184 void atexit(void (*code)(void *), void *data = NULL);
185 };
186
187 _finline void *operator new(size_t size, CYPool &pool) {
188 return pool.malloc<void>(size);
189 }
190
191 _finline void *operator new [](size_t size, CYPool &pool) {
192 return pool.malloc<void>(size);
193 }
194
195 _finline void CYPool::atexit(void (*code)(void *), void *data) {
196 cleaner_ = new(*this) Cleaner(cleaner_, code, data);
197 }
198
199 struct CYData {
200 CYPool *pool_;
201 unsigned count_;
202
203 CYData() :
204 count_(1)
205 {
206 }
207
208 CYData(CYPool &pool) :
209 pool_(&pool),
210 count_(_not(unsigned))
211 {
212 }
213
214 virtual ~CYData() {
215 }
216
217 static void *operator new(size_t size, CYPool &pool) {
218 void *data(pool.malloc<void>(size));
219 reinterpret_cast<CYData *>(data)->pool_ = &pool;
220 return data;
221 }
222
223 static void *operator new(size_t size) {
224 return operator new(size, *new CYPool());
225 }
226
227 static void operator delete(void *data) {
228 delete reinterpret_cast<CYData *>(data)->pool_;
229 }
230 };
231
232 template <typename Type_>
233 struct CYPoolAllocator {
234 CYPool *pool_;
235
236 typedef Type_ value_type;
237 typedef value_type *pointer;
238 typedef const value_type *const_pointer;
239 typedef value_type &reference;
240 typedef const value_type &const_reference;
241 typedef std::size_t size_type;
242 typedef std::ptrdiff_t difference_type;
243
244 CYPoolAllocator() :
245 pool_(NULL)
246 {
247 }
248
249 template <typename Right_>
250 CYPoolAllocator(const CYPoolAllocator<Right_> &rhs) :
251 pool_(rhs.pool_)
252 {
253 }
254
255 pointer allocate(size_type size, const void *hint = 0) {
256 return pool_->malloc<value_type>(size);
257 }
258
259 void deallocate(pointer data, size_type size) {
260 }
261
262 void construct(pointer address, const Type_ &rhs) {
263 new(address) Type_(rhs);
264 }
265
266 void destroy(pointer address) {
267 address->~Type_();
268 }
269
270 template <typename Right_>
271 inline bool operator==(const CYPoolAllocator<Right_> &rhs) {
272 return pool_ == rhs.pool_;
273 }
274
275 template <typename Right_>
276 inline bool operator!=(const CYPoolAllocator<Right_> &rhs) {
277 return !operator==(rhs);
278 }
279
280 template <typename Right_>
281 struct rebind {
282 typedef CYPoolAllocator<Right_> other;
283 };
284 };
285
286 class CYLocalPool :
287 public CYPool
288 {
289 private:
290 CYLocal<CYPool> local_;
291
292 public:
293 CYLocalPool() :
294 CYPool(),
295 local_(this)
296 {
297 }
298 };
299
300 #define $pool \
301 (*CYLocal<CYPool>::Get())
302
303 #endif/*CYCRIPT_POOLING_HPP*/