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 Menes_Pooling_HPP
23 #define Menes_Pooling_HPP
35 _finline void *operator new(size_t size, CYPool &pool);
36 _finline void *operator new [](size_t size, CYPool &pool);
45 void (*code_)(void *);
48 Cleaner(Cleaner *next, void (*code)(void *), void *data) :
56 static _finline size_t align(size_t size) {
57 // XXX: alignment is more complex than this
58 return (size + 7) & ~0x3;
61 template <typename Type_>
62 static void delete_(void *data) {
63 reinterpret_cast<Type_ *>(data)->~Type_();
66 CYPool(const CYPool &);
77 for (Cleaner *cleaner(cleaner_); cleaner != NULL; ) {
78 Cleaner *next(cleaner->next_);
79 (*cleaner->code_)(cleaner->data_);
84 template <typename Type_>
85 Type_ *malloc(size_t size) {
89 // XXX: is this an optimal malloc size?
90 size_ = std::max<size_t>(size, size + align(sizeof(Cleaner)));
91 data_ = reinterpret_cast<uint8_t *>(::malloc(size_));
93 _assert(size <= size_);
99 return reinterpret_cast<Type_ *>(data);
102 void atexit(void (*code)(void *), void *data = NULL);
105 _finline void *operator new(size_t size, CYPool &pool) {
106 return pool.malloc<void>(size);
109 _finline void *operator new [](size_t size, CYPool &pool) {
110 return pool.malloc<void>(size);
113 _finline void CYPool::atexit(void (*code)(void *), void *data) {
114 cleaner_ = new(*this) Cleaner(cleaner_, code, data);
117 #endif/*Menes_Pooling_HPP*/