1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
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.
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.
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/>.
22 #ifndef CYCRIPT_POOLING_HPP
23 #define CYCRIPT_POOLING_HPP
34 #include "Exception.hpp"
36 #include "Standard.hpp"
39 _finline void *operator new(size_t size, CYPool &pool);
40 _finline void *operator new [](size_t size, CYPool &pool);
49 void (*code_)(void *);
52 Cleaner(Cleaner *next, void (*code)(void *), void *data) :
60 static _finline size_t align(size_t size) {
61 // XXX: alignment is more complex than this
62 return (size + 7) & ~0x3;
65 CYPool(const CYPool &);
76 for (Cleaner *cleaner(cleaner_); cleaner != NULL; ) {
77 Cleaner *next(cleaner->next_);
78 (*cleaner->code_)(cleaner->data_);
83 template <typename Type_>
84 Type_ *malloc(size_t 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_));
92 _assert(size <= size_);
98 return reinterpret_cast<Type_ *>(data);
101 char *strdup(const char *data) {
102 return reinterpret_cast<char *>(memdup(data, strlen(data) + 1));
105 void *memdup(const void *data, size_t size) {
106 void *copy(malloc<void>(size));
107 memcpy(copy, data, size);
111 char *strndup(const char *data, size_t size) const {
112 return strmemdup(data, strnlen(data, size));
115 char *strmemdup(const char *data, size_t size) const {
116 char *copy(new char[size + 1]);
117 memcpy(copy, data, size);
122 // XXX: this could be made much more efficient
123 __attribute__((__sentinel__))
124 char *strcat(const char *data, ...) {
125 size_t size(strlen(data)); {
127 va_start(args, data);
129 while (const char *arg = va_arg(args, const char *))
135 char *copy(malloc<char>(size + 1)); {
137 va_start(args, data);
139 size_t offset(strlen(data));
140 memcpy(copy, data, offset);
142 while (const char *arg = va_arg(args, const char *)) {
143 size_t size(strlen(arg));
144 memcpy(copy + offset, arg, size);
155 // XXX: most people using this might should use sprintf
156 char *itoa(long value) {
157 return sprintf(16, "%ld", value);
160 __attribute__((__format__(__printf__, 3, 4)))
161 char *sprintf(size_t size, const char *format, ...) {
163 va_start(args, format);
164 char *copy(vsprintf(size, format, args));
169 char *vsprintf(size_t size, const char *format, va_list args) {
173 int writ(vsnprintf(buffer, size, format, copy));
177 if (size_t(writ) >= size)
178 return vsprintf(writ + 1, format, args);
179 return strmemdup(buffer, writ);
182 void atexit(void (*code)(void *), void *data = NULL);
185 _finline void *operator new(size_t size, CYPool &pool) {
186 return pool.malloc<void>(size);
189 _finline void *operator new [](size_t size, CYPool &pool) {
190 return pool.malloc<void>(size);
193 _finline void CYPool::atexit(void (*code)(void *), void *data) {
194 cleaner_ = new(*this) Cleaner(cleaner_, code, data);
206 CYData(CYPool &pool) :
208 count_(_not(unsigned))
215 static void *operator new(size_t size, CYPool &pool) {
216 void *data(pool.malloc<void>(size));
217 reinterpret_cast<CYData *>(data)->pool_ = &pool;
221 static void *operator new(size_t size) {
222 return operator new(size, *new CYPool());
225 static void operator delete(void *data) {
226 delete reinterpret_cast<CYData *>(data)->pool_;
230 template <typename Type_>
231 struct CYPoolAllocator {
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;
247 template <typename Right_>
248 CYPoolAllocator(const CYPoolAllocator<Right_> &rhs) :
253 pointer allocate(size_type size, const void *hint = 0) {
254 return pool_->malloc<value_type>(size);
257 void deallocate(pointer data, size_type size) {
260 void construct(pointer address, const Type_ &rhs) {
261 new(address) Type_(rhs);
264 void destroy(pointer address) {
268 template <typename Right_>
269 inline bool operator==(const CYPoolAllocator<Right_> &rhs) {
270 return pool_ == rhs.pool_;
273 template <typename Right_>
274 inline bool operator!=(const CYPoolAllocator<Right_> &rhs) {
275 return !operator==(rhs);
278 template <typename Right_>
280 typedef CYPoolAllocator<Right_> other;
288 CYLocal<CYPool> local_;
299 (*CYLocal<CYPool>::Get())
301 #endif/*CYCRIPT_POOLING_HPP*/