]>
Commit | Line | Data |
---|---|---|
b3378a02 | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
c15969fd | 2 | * Copyright (C) 2009-2013 Jay Freeman (saurik) |
4644480a JF |
3 | */ |
4 | ||
c15969fd | 5 | /* GNU General Public License, Version 3 {{{ */ |
4644480a | 6 | /* |
c15969fd JF |
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. | |
4644480a | 11 | * |
c15969fd JF |
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. | |
4644480a | 16 | * |
c15969fd | 17 | * You should have received a copy of the GNU General Public License |
b3378a02 JF |
18 | * along with Cycript. If not, see <http://www.gnu.org/licenses/>. |
19 | **/ | |
4644480a JF |
20 | /* }}} */ |
21 | ||
c5fa2867 JF |
22 | #ifndef CYCRIPT_POOLING_HPP |
23 | #define CYCRIPT_POOLING_HPP | |
5999c315 | 24 | |
b799113b JF |
25 | #include <cstdarg> |
26 | #include <cstdlib> | |
27 | ||
9185d5ef JF |
28 | #include <apr_pools.h> |
29 | #include <apr_strings.h> | |
5999c315 | 30 | |
37954781 | 31 | #include "Exception.hpp" |
2eb8215d | 32 | #include "Local.hpp" |
37954781 | 33 | #include "Standard.hpp" |
5999c315 | 34 | |
5999c315 JF |
35 | class CYPool { |
36 | private: | |
37 | apr_pool_t *pool_; | |
38 | ||
39 | public: | |
fe3189de JF |
40 | CYPool() { |
41 | _aprcall(apr_pool_create(&pool_, NULL)); | |
5999c315 JF |
42 | } |
43 | ||
b799113b JF |
44 | CYPool(apr_pool_t *pool) : |
45 | pool_(pool) | |
46 | { | |
47 | } | |
48 | ||
5999c315 JF |
49 | ~CYPool() { |
50 | apr_pool_destroy(pool_); | |
51 | } | |
52 | ||
b1ff2d78 JF |
53 | void Clear() { |
54 | apr_pool_clear(pool_); | |
55 | } | |
56 | ||
5999c315 JF |
57 | operator apr_pool_t *() const { |
58 | return pool_; | |
59 | } | |
60 | ||
b799113b JF |
61 | void *operator()(size_t size) const { |
62 | return apr_palloc(pool_, size); | |
63 | } | |
64 | ||
65 | char *strdup(const char *data) const { | |
5999c315 JF |
66 | return apr_pstrdup(pool_, data); |
67 | } | |
68 | ||
b799113b | 69 | char *strndup(const char *data, size_t size) const { |
5999c315 JF |
70 | return apr_pstrndup(pool_, data, size); |
71 | } | |
b799113b JF |
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 | } | |
5999c315 JF |
88 | }; |
89 | ||
b799113b JF |
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 | ||
1ef7d061 | 98 | struct CYData { |
b799113b | 99 | CYPool *pool_; |
1850a470 JF |
100 | unsigned count_; |
101 | ||
102 | CYData() : | |
103 | count_(1) | |
104 | { | |
105 | } | |
1ef7d061 | 106 | |
b799113b JF |
107 | CYData(CYPool &pool) : |
108 | pool_(&pool), | |
109 | count_(_not(unsigned)) | |
110 | { | |
111 | } | |
112 | ||
1ef7d061 JF |
113 | virtual ~CYData() { |
114 | } | |
115 | ||
b799113b JF |
116 | static void *operator new(size_t size, CYPool &pool) { |
117 | void *data(pool(size)); | |
118 | reinterpret_cast<CYData *>(data)->pool_ = &pool; | |
1ef7d061 JF |
119 | return data; |
120 | } | |
121 | ||
122 | static void *operator new(size_t size) { | |
b799113b | 123 | return operator new(size, *new CYPool()); |
1ef7d061 JF |
124 | } |
125 | ||
126 | static void operator delete(void *data) { | |
b799113b | 127 | delete reinterpret_cast<CYData *>(data)->pool_; |
1ef7d061 | 128 | } |
1ef7d061 JF |
129 | }; |
130 | ||
a846a8cd JF |
131 | template <typename Type_> |
132 | struct CYPoolAllocator { | |
b799113b | 133 | CYPool *pool_; |
a846a8cd JF |
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) { | |
b799113b | 155 | return reinterpret_cast<pointer>((*pool_)(size)); |
a846a8cd JF |
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 | ||
2eb8215d JF |
185 | class CYLocalPool : |
186 | public CYPool | |
187 | { | |
188 | private: | |
b799113b | 189 | CYLocal<CYPool> local_; |
2eb8215d JF |
190 | |
191 | public: | |
192 | CYLocalPool() : | |
193 | CYPool(), | |
b799113b | 194 | local_(this) |
2eb8215d JF |
195 | { |
196 | } | |
197 | }; | |
198 | ||
199 | #define $pool \ | |
b799113b | 200 | (*CYLocal<CYPool>::Get()) |
2eb8215d | 201 | |
c5fa2867 | 202 | #endif/*CYCRIPT_POOLING_HPP*/ |