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