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 _aprcall(apr_pool_create(&pool_, NULL));
44 CYPool(apr_pool_t *pool) :
50 apr_pool_destroy(pool_);
54 apr_pool_clear(pool_);
57 operator apr_pool_t *() const {
61 void *operator()(size_t size) const {
62 return apr_palloc(pool_, size);
65 char *strdup(const char *data) const {
66 return apr_pstrdup(pool_, data);
69 char *strndup(const char *data, size_t size) const {
70 return apr_pstrndup(pool_, data, size);
73 char *strmemdup(const char *data, size_t size) const {
74 return apr_pstrmemdup(pool_, data, size);
77 char *sprintf(const char *format, ...) const {
79 va_start(args, format);
80 char *data(vsprintf(format, args));
85 char *vsprintf(const char *format, va_list args) const {
86 return apr_pvsprintf(pool_, format, args);
90 _finline void *operator new(size_t size, CYPool &pool) {
94 _finline void *operator new [](size_t size, CYPool &pool) {
107 CYData(CYPool &pool) :
109 count_(_not(unsigned))
116 static void *operator new(size_t size, CYPool &pool) {
117 void *data(pool(size));
118 reinterpret_cast<CYData *>(data)->pool_ = &pool;
122 static void *operator new(size_t size) {
123 return operator new(size, *new CYPool());
126 static void operator delete(void *data) {
127 delete reinterpret_cast<CYData *>(data)->pool_;
131 template <typename Type_>
132 struct CYPoolAllocator {
135 typedef Type_ value_type;
136 typedef value_type *pointer;
137 typedef const value_type *const_pointer;
138 typedef value_type &reference;
139 typedef const value_type &const_reference;
140 typedef std::size_t size_type;
141 typedef std::ptrdiff_t difference_type;
148 template <typename Right_>
149 CYPoolAllocator(const CYPoolAllocator<Right_> &rhs) :
154 pointer allocate(size_type size, const void *hint = 0) {
155 return reinterpret_cast<pointer>((*pool_)(size));
158 void deallocate(pointer data, size_type size) {
161 void construct(pointer address, const Type_ &rhs) {
162 new(address) Type_(rhs);
165 void destroy(pointer address) {
169 template <typename Right_>
170 inline bool operator==(const CYPoolAllocator<Right_> &rhs) {
171 return pool_ == rhs.pool_;
174 template <typename Right_>
175 inline bool operator!=(const CYPoolAllocator<Right_> &rhs) {
176 return !operator==(rhs);
179 template <typename Right_>
181 typedef CYPoolAllocator<Right_> other;
189 CYLocal<CYPool> local_;
200 (*CYLocal<CYPool>::Get())
202 #endif/*CYCRIPT_POOLING_HPP*/