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
28 #include <apr_pools.h>
29 #include <apr_strings.h>
31 #include "Exception.hpp"
33 #include "Standard.hpp"
41 void (*code_)(void *);
44 Cleaner(Cleaner *next, void (*code)(void *), void *data) :
56 _aprcall(apr_pool_create(&pool_, NULL));
60 for (Cleaner *cleaner(cleaner_); cleaner_ != NULL; cleaner_ = cleaner_->next_)
61 (*cleaner->code_)(cleaner->data_);
62 apr_pool_destroy(pool_);
66 apr_pool_clear(pool_);
69 operator apr_pool_t *() const {
73 void *operator()(size_t size) const {
74 return apr_palloc(pool_, size);
77 char *strdup(const char *data) const {
78 return apr_pstrdup(pool_, data);
81 char *strndup(const char *data, size_t size) const {
82 return apr_pstrndup(pool_, data, size);
85 char *strmemdup(const char *data, size_t size) const {
86 return apr_pstrmemdup(pool_, data, size);
89 char *sprintf(const char *format, ...) const {
91 va_start(args, format);
92 char *data(vsprintf(format, args));
97 char *vsprintf(const char *format, va_list args) const {
98 return apr_pvsprintf(pool_, format, args);
101 void atexit(void (*code)(void *), void *data = NULL);
104 _finline void *operator new(size_t size, CYPool &pool) {
108 _finline void *operator new [](size_t size, CYPool &pool) {
112 _finline void CYPool::atexit(void (*code)(void *), void *data) {
113 cleaner_ = new(*this) Cleaner(cleaner_, code, data);
125 CYData(CYPool &pool) :
127 count_(_not(unsigned))
134 static void *operator new(size_t size, CYPool &pool) {
135 void *data(pool(size));
136 reinterpret_cast<CYData *>(data)->pool_ = &pool;
140 static void *operator new(size_t size) {
141 return operator new(size, *new CYPool());
144 static void operator delete(void *data) {
145 delete reinterpret_cast<CYData *>(data)->pool_;
149 template <typename Type_>
150 struct CYPoolAllocator {
153 typedef Type_ value_type;
154 typedef value_type *pointer;
155 typedef const value_type *const_pointer;
156 typedef value_type &reference;
157 typedef const value_type &const_reference;
158 typedef std::size_t size_type;
159 typedef std::ptrdiff_t difference_type;
166 template <typename Right_>
167 CYPoolAllocator(const CYPoolAllocator<Right_> &rhs) :
172 pointer allocate(size_type size, const void *hint = 0) {
173 return reinterpret_cast<pointer>((*pool_)(size));
176 void deallocate(pointer data, size_type size) {
179 void construct(pointer address, const Type_ &rhs) {
180 new(address) Type_(rhs);
183 void destroy(pointer address) {
187 template <typename Right_>
188 inline bool operator==(const CYPoolAllocator<Right_> &rhs) {
189 return pool_ == rhs.pool_;
192 template <typename Right_>
193 inline bool operator!=(const CYPoolAllocator<Right_> &rhs) {
194 return !operator==(rhs);
197 template <typename Right_>
199 typedef CYPoolAllocator<Right_> other;
207 CYLocal<CYPool> local_;
218 (*CYLocal<CYPool>::Get())
220 #endif/*CYCRIPT_POOLING_HPP*/