1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
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.
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.
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/>.
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);
50 void (*code_)(void *);
53 Cleaner(Cleaner *next, void (*code)(void *), void *data) :
61 static _finline size_t align(size_t size) {
62 // XXX: alignment is more complex than this
63 return (size + 7) & ~0x3;
66 template <typename Type_>
67 static void delete_(void *data) {
68 reinterpret_cast<Type_ *>(data)->~Type_();
71 CYPool(const CYPool &);
74 CYPool(size_t next = 64) :
83 for (Cleaner *cleaner(cleaner_); cleaner != NULL; ) {
84 Cleaner *next(cleaner->next_);
85 (*cleaner->code_)(cleaner->data_);
90 template <typename Type_>
91 Type_ *malloc(size_t size) {
95 size_ = std::max<size_t>(next_, size + align(sizeof(Cleaner)));
97 data_ = reinterpret_cast<uint8_t *>(::malloc(size_));
99 _assert(size <= size_);
105 return reinterpret_cast<Type_ *>(data);
108 char *strdup(const char *data) {
111 return reinterpret_cast<char *>(memdup(data, strlen(data) + 1));
114 template <typename Type_>
115 Type_ *memdup(const Type_ *data, size_t size) {
116 Type_ *copy(malloc<Type_>(size));
117 memcpy(copy, data, size);
121 char *strndup(const char *data, size_t size) {
122 return strmemdup(data, strnlen(data, size));
125 char *strmemdup(const char *data, size_t size) {
126 char *copy(malloc<char>(size + 1));
127 memcpy(copy, data, size);
132 // XXX: this could be made much more efficient
133 __attribute__((__sentinel__))
134 char *strcat(const char *data, ...) {
135 size_t size(strlen(data)); {
137 va_start(args, data);
139 while (const char *arg = va_arg(args, const char *))
145 char *copy(malloc<char>(size + 1)); {
147 va_start(args, data);
149 size_t offset(strlen(data));
150 memcpy(copy, data, offset);
152 while (const char *arg = va_arg(args, const char *)) {
153 size_t size(strlen(arg));
154 memcpy(copy + offset, arg, size);
165 // XXX: most people using this might should use sprintf
166 char *itoa(long value) {
167 return sprintf(16, "%ld", value);
170 __attribute__((__format__(__printf__, 3, 4)))
171 char *sprintf(size_t size, const char *format, ...) {
173 va_start(args, format);
174 char *copy(vsprintf(size, format, args));
179 char *vsprintf(size_t size, const char *format, va_list args) {
183 int writ(vsnprintf(buffer, size, format, copy));
187 if (size_t(writ) >= size)
188 return vsprintf(writ + 1, format, args);
189 return strmemdup(buffer, writ);
192 void atexit(void (*code)(void *), void *data = NULL);
194 template <typename Type_>
196 Type_ *value(new(*this) Type_());
197 atexit(&delete_<Type_>, value);
202 _finline void *operator new(size_t size, CYPool &pool) {
203 return pool.malloc<void>(size);
206 _finline void *operator new [](size_t size, CYPool &pool) {
207 return pool.malloc<void>(size);
210 _finline void CYPool::atexit(void (*code)(void *), void *data) {
211 cleaner_ = new(*this) Cleaner(cleaner_, code, data);
223 CYData(CYPool &pool) :
225 count_(_not(unsigned))
232 static void *operator new(size_t size, CYPool &pool) {
233 void *data(pool.malloc<void>(size));
234 reinterpret_cast<CYData *>(data)->pool_ = &pool;
238 static void *operator new(size_t size) {
239 return operator new(size, *new CYPool());
242 static void operator delete(void *data) {
243 delete reinterpret_cast<CYData *>(data)->pool_;
247 template <typename Type_>
248 struct CYPoolAllocator {
251 typedef Type_ value_type;
252 typedef value_type *pointer;
253 typedef const value_type *const_pointer;
254 typedef value_type &reference;
255 typedef const value_type &const_reference;
256 typedef std::size_t size_type;
257 typedef std::ptrdiff_t difference_type;
264 template <typename Right_>
265 CYPoolAllocator(const CYPoolAllocator<Right_> &rhs) :
270 pointer allocate(size_type size, const void *hint = 0) {
271 return pool_->malloc<value_type>(size);
274 void deallocate(pointer data, size_type size) {
277 void construct(pointer address, const Type_ &rhs) {
278 new(address) Type_(rhs);
281 void destroy(pointer address) {
285 template <typename Right_>
286 inline bool operator==(const CYPoolAllocator<Right_> &rhs) {
287 return pool_ == rhs.pool_;
290 template <typename Right_>
291 inline bool operator!=(const CYPoolAllocator<Right_> &rhs) {
292 return !operator==(rhs);
295 template <typename Right_>
297 typedef CYPoolAllocator<Right_> other;
305 CYLocal<CYPool> local_;
316 (*CYLocal<CYPool>::Get())
319 ::pthread_key_t CYLocal<CYPool>::key_;
321 #endif/*CYCRIPT_POOLING_HPP*/