]> git.saurik.com Git - cycript.git/blob - Pooling.hpp
854113e86bd972f1f96256064032c8c4020cbd1e
[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 struct Cleaner {
40 Cleaner *next_;
41 void (*code_)(void *);
42 void *data_;
43
44 Cleaner(Cleaner *next, void (*code)(void *), void *data) :
45 next_(next),
46 code_(code),
47 data_(data)
48 {
49 }
50 } *cleaner_;
51
52 public:
53 CYPool() :
54 cleaner_(NULL)
55 {
56 _aprcall(apr_pool_create(&pool_, NULL));
57 }
58
59 ~CYPool() {
60 for (Cleaner *cleaner(cleaner_); cleaner_ != NULL; cleaner_ = cleaner_->next_)
61 (*cleaner->code_)(cleaner->data_);
62 apr_pool_destroy(pool_);
63 }
64
65 void Clear() {
66 apr_pool_clear(pool_);
67 }
68
69 operator apr_pool_t *() const {
70 return pool_;
71 }
72
73 void *operator()(size_t size) const {
74 return apr_palloc(pool_, size);
75 }
76
77 char *strdup(const char *data) const {
78 return apr_pstrdup(pool_, data);
79 }
80
81 char *strndup(const char *data, size_t size) const {
82 return apr_pstrndup(pool_, data, size);
83 }
84
85 char *strmemdup(const char *data, size_t size) const {
86 return apr_pstrmemdup(pool_, data, size);
87 }
88
89 char *sprintf(const char *format, ...) const {
90 va_list args;
91 va_start(args, format);
92 char *data(vsprintf(format, args));
93 va_end(args);
94 return data;
95 }
96
97 char *vsprintf(const char *format, va_list args) const {
98 return apr_pvsprintf(pool_, format, args);
99 }
100
101 void atexit(void (*code)(void *), void *data = NULL);
102 };
103
104 _finline void *operator new(size_t size, CYPool &pool) {
105 return pool(size);
106 }
107
108 _finline void *operator new [](size_t size, CYPool &pool) {
109 return pool(size);
110 }
111
112 _finline void CYPool::atexit(void (*code)(void *), void *data) {
113 cleaner_ = new(*this) Cleaner(cleaner_, code, data);
114 }
115
116 struct CYData {
117 CYPool *pool_;
118 unsigned count_;
119
120 CYData() :
121 count_(1)
122 {
123 }
124
125 CYData(CYPool &pool) :
126 pool_(&pool),
127 count_(_not(unsigned))
128 {
129 }
130
131 virtual ~CYData() {
132 }
133
134 static void *operator new(size_t size, CYPool &pool) {
135 void *data(pool(size));
136 reinterpret_cast<CYData *>(data)->pool_ = &pool;
137 return data;
138 }
139
140 static void *operator new(size_t size) {
141 return operator new(size, *new CYPool());
142 }
143
144 static void operator delete(void *data) {
145 delete reinterpret_cast<CYData *>(data)->pool_;
146 }
147 };
148
149 template <typename Type_>
150 struct CYPoolAllocator {
151 CYPool *pool_;
152
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;
160
161 CYPoolAllocator() :
162 pool_(NULL)
163 {
164 }
165
166 template <typename Right_>
167 CYPoolAllocator(const CYPoolAllocator<Right_> &rhs) :
168 pool_(rhs.pool_)
169 {
170 }
171
172 pointer allocate(size_type size, const void *hint = 0) {
173 return reinterpret_cast<pointer>((*pool_)(size));
174 }
175
176 void deallocate(pointer data, size_type size) {
177 }
178
179 void construct(pointer address, const Type_ &rhs) {
180 new(address) Type_(rhs);
181 }
182
183 void destroy(pointer address) {
184 address->~Type_();
185 }
186
187 template <typename Right_>
188 inline bool operator==(const CYPoolAllocator<Right_> &rhs) {
189 return pool_ == rhs.pool_;
190 }
191
192 template <typename Right_>
193 inline bool operator!=(const CYPoolAllocator<Right_> &rhs) {
194 return !operator==(rhs);
195 }
196
197 template <typename Right_>
198 struct rebind {
199 typedef CYPoolAllocator<Right_> other;
200 };
201 };
202
203 class CYLocalPool :
204 public CYPool
205 {
206 private:
207 CYLocal<CYPool> local_;
208
209 public:
210 CYLocalPool() :
211 CYPool(),
212 local_(this)
213 {
214 }
215 };
216
217 #define $pool \
218 (*CYLocal<CYPool>::Get())
219
220 #endif/*CYCRIPT_POOLING_HPP*/