1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU Lesser 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
25 #include <apr_pools.h>
26 #include <apr_strings.h>
28 #include "Exception.hpp"
30 #include "Standard.hpp"
34 _finline void *operator new(size_t size, apr_pool_t *pool) {
35 return apr_palloc(pool, size);
38 _finline void *operator new [](size_t size, apr_pool_t *pool) {
39 return apr_palloc(pool, size);
47 CYPool(apr_pool_t *pool = NULL) {
48 _aprcall(apr_pool_create(&pool_, pool));
52 apr_pool_destroy(pool_);
56 apr_pool_clear(pool_);
59 operator apr_pool_t *() const {
63 char *operator ()(const char *data) const {
64 return apr_pstrdup(pool_, data);
67 char *operator ()(const char *data, size_t size) const {
68 return apr_pstrndup(pool_, data, size);
84 static void *operator new(size_t size, apr_pool_t *pool) {
85 void *data(apr_palloc(pool, size));
86 reinterpret_cast<CYData *>(data)->pool_ = pool;
90 static void *operator new(size_t size) {
92 _aprcall(apr_pool_create(&pool, NULL));
93 return operator new(size, pool);
96 static void operator delete(void *data) {
97 apr_pool_destroy(reinterpret_cast<CYData *>(data)->pool_);
101 template <typename Type_>
102 struct CYPoolAllocator {
105 typedef Type_ value_type;
106 typedef value_type *pointer;
107 typedef const value_type *const_pointer;
108 typedef value_type &reference;
109 typedef const value_type &const_reference;
110 typedef std::size_t size_type;
111 typedef std::ptrdiff_t difference_type;
118 template <typename Right_>
119 CYPoolAllocator(const CYPoolAllocator<Right_> &rhs) :
124 pointer allocate(size_type size, const void *hint = 0) {
125 return reinterpret_cast<pointer>(apr_palloc(pool_, size));
128 void deallocate(pointer data, size_type size) {
131 void construct(pointer address, const Type_ &rhs) {
132 new(address) Type_(rhs);
135 void destroy(pointer address) {
139 template <typename Right_>
140 inline bool operator==(const CYPoolAllocator<Right_> &rhs) {
141 return pool_ == rhs.pool_;
144 template <typename Right_>
145 inline bool operator!=(const CYPoolAllocator<Right_> &rhs) {
146 return !operator==(rhs);
149 template <typename Right_>
151 typedef CYPoolAllocator<Right_> other;
159 CYLocal<apr_pool_t> local_;
164 local_(operator apr_pool_t *())
170 CYLocal<apr_pool_t>::Get()
172 #endif/*CYCRIPT_POOLING_HPP*/