]> git.saurik.com Git - cycript.git/blob - Pooling.hpp
Replace all apr_pool_t * usages with CYPool &.
[cycript.git] / Pooling.hpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
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.
11 *
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.
16 *
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/>.
19 **/
20 /* }}} */
21
22 #ifndef CYCRIPT_POOLING_HPP
23 #define CYCRIPT_POOLING_HPP
24
25 #include <cstdarg>
26 #include <cstdlib>
27
28 #include <apr_pools.h>
29 #include <apr_strings.h>
30
31 #include "Exception.hpp"
32 #include "Local.hpp"
33 #include "Standard.hpp"
34
35 class CYPool {
36 private:
37 apr_pool_t *pool_;
38
39 public:
40 CYPool() {
41 _aprcall(apr_pool_create(&pool_, NULL));
42 }
43
44 CYPool(apr_pool_t *pool) :
45 pool_(pool)
46 {
47 }
48
49 ~CYPool() {
50 apr_pool_destroy(pool_);
51 }
52
53 void Clear() {
54 apr_pool_clear(pool_);
55 }
56
57 operator apr_pool_t *() const {
58 return pool_;
59 }
60
61 void *operator()(size_t size) const {
62 return apr_palloc(pool_, size);
63 }
64
65 char *strdup(const char *data) const {
66 return apr_pstrdup(pool_, data);
67 }
68
69 char *strndup(const char *data, size_t size) const {
70 return apr_pstrndup(pool_, data, size);
71 }
72
73 char *strmemdup(const char *data, size_t size) const {
74 return apr_pstrmemdup(pool_, data, size);
75 }
76
77 char *sprintf(const char *format, ...) const {
78 va_list args;
79 va_start(args, format);
80 char *data(vsprintf(format, args));
81 va_end(args);
82 return data;
83 }
84
85 char *vsprintf(const char *format, va_list args) const {
86 return apr_pvsprintf(pool_, format, args);
87 }
88 };
89
90 _finline void *operator new(size_t size, CYPool &pool) {
91 return pool(size);
92 }
93
94 _finline void *operator new [](size_t size, CYPool &pool) {
95 return pool(size);
96 }
97
98 struct CYData {
99 CYPool *pool_;
100 unsigned count_;
101
102 CYData() :
103 count_(1)
104 {
105 }
106
107 CYData(CYPool &pool) :
108 pool_(&pool),
109 count_(_not(unsigned))
110 {
111 }
112
113 virtual ~CYData() {
114 }
115
116 static void *operator new(size_t size, CYPool &pool) {
117 void *data(pool(size));
118 reinterpret_cast<CYData *>(data)->pool_ = &pool;
119 return data;
120 }
121
122 static void *operator new(size_t size) {
123 return operator new(size, *new CYPool());
124 }
125
126 static void operator delete(void *data) {
127 delete reinterpret_cast<CYData *>(data)->pool_;
128 }
129 };
130
131 template <typename Type_>
132 struct CYPoolAllocator {
133 CYPool *pool_;
134
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;
142
143 CYPoolAllocator() :
144 pool_(NULL)
145 {
146 }
147
148 template <typename Right_>
149 CYPoolAllocator(const CYPoolAllocator<Right_> &rhs) :
150 pool_(rhs.pool_)
151 {
152 }
153
154 pointer allocate(size_type size, const void *hint = 0) {
155 return reinterpret_cast<pointer>((*pool_)(size));
156 }
157
158 void deallocate(pointer data, size_type size) {
159 }
160
161 void construct(pointer address, const Type_ &rhs) {
162 new(address) Type_(rhs);
163 }
164
165 void destroy(pointer address) {
166 address->~Type_();
167 }
168
169 template <typename Right_>
170 inline bool operator==(const CYPoolAllocator<Right_> &rhs) {
171 return pool_ == rhs.pool_;
172 }
173
174 template <typename Right_>
175 inline bool operator!=(const CYPoolAllocator<Right_> &rhs) {
176 return !operator==(rhs);
177 }
178
179 template <typename Right_>
180 struct rebind {
181 typedef CYPoolAllocator<Right_> other;
182 };
183 };
184
185 class CYLocalPool :
186 public CYPool
187 {
188 private:
189 CYLocal<CYPool> local_;
190
191 public:
192 CYLocalPool() :
193 CYPool(),
194 local_(this)
195 {
196 }
197 };
198
199 #define $pool \
200 (*CYLocal<CYPool>::Get())
201
202 #endif/*CYCRIPT_POOLING_HPP*/