]>
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 | |
9185d5ef JF |
25 | #include <apr_pools.h> |
26 | #include <apr_strings.h> | |
5999c315 | 27 | |
37954781 | 28 | #include "Exception.hpp" |
2eb8215d | 29 | #include "Local.hpp" |
37954781 | 30 | #include "Standard.hpp" |
5999c315 | 31 | |
a846a8cd JF |
32 | #include <cstdlib> |
33 | ||
5999c315 JF |
34 | _finline void *operator new(size_t size, apr_pool_t *pool) { |
35 | return apr_palloc(pool, size); | |
36 | } | |
37 | ||
38 | _finline void *operator new [](size_t size, apr_pool_t *pool) { | |
39 | return apr_palloc(pool, size); | |
40 | } | |
41 | ||
42 | class CYPool { | |
43 | private: | |
44 | apr_pool_t *pool_; | |
45 | ||
46 | public: | |
fe3189de JF |
47 | CYPool() { |
48 | _aprcall(apr_pool_create(&pool_, NULL)); | |
5999c315 JF |
49 | } |
50 | ||
51 | ~CYPool() { | |
52 | apr_pool_destroy(pool_); | |
53 | } | |
54 | ||
b1ff2d78 JF |
55 | void Clear() { |
56 | apr_pool_clear(pool_); | |
57 | } | |
58 | ||
5999c315 JF |
59 | operator apr_pool_t *() const { |
60 | return pool_; | |
61 | } | |
62 | ||
63 | char *operator ()(const char *data) const { | |
64 | return apr_pstrdup(pool_, data); | |
65 | } | |
66 | ||
67 | char *operator ()(const char *data, size_t size) const { | |
68 | return apr_pstrndup(pool_, data, size); | |
69 | } | |
70 | }; | |
71 | ||
1ef7d061 JF |
72 | struct CYData { |
73 | apr_pool_t *pool_; | |
1850a470 JF |
74 | unsigned count_; |
75 | ||
76 | CYData() : | |
77 | count_(1) | |
78 | { | |
79 | } | |
1ef7d061 JF |
80 | |
81 | virtual ~CYData() { | |
82 | } | |
83 | ||
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; | |
87 | return data; | |
88 | } | |
89 | ||
90 | static void *operator new(size_t size) { | |
91 | apr_pool_t *pool; | |
92 | _aprcall(apr_pool_create(&pool, NULL)); | |
93 | return operator new(size, pool); | |
94 | } | |
95 | ||
96 | static void operator delete(void *data) { | |
97 | apr_pool_destroy(reinterpret_cast<CYData *>(data)->pool_); | |
98 | } | |
1ef7d061 JF |
99 | }; |
100 | ||
a846a8cd JF |
101 | template <typename Type_> |
102 | struct CYPoolAllocator { | |
103 | apr_pool_t *pool_; | |
104 | ||
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; | |
112 | ||
113 | CYPoolAllocator() : | |
114 | pool_(NULL) | |
115 | { | |
116 | } | |
117 | ||
118 | template <typename Right_> | |
119 | CYPoolAllocator(const CYPoolAllocator<Right_> &rhs) : | |
120 | pool_(rhs.pool_) | |
121 | { | |
122 | } | |
123 | ||
124 | pointer allocate(size_type size, const void *hint = 0) { | |
125 | return reinterpret_cast<pointer>(apr_palloc(pool_, size)); | |
126 | } | |
127 | ||
128 | void deallocate(pointer data, size_type size) { | |
129 | } | |
130 | ||
131 | void construct(pointer address, const Type_ &rhs) { | |
132 | new(address) Type_(rhs); | |
133 | } | |
134 | ||
135 | void destroy(pointer address) { | |
136 | address->~Type_(); | |
137 | } | |
138 | ||
139 | template <typename Right_> | |
140 | inline bool operator==(const CYPoolAllocator<Right_> &rhs) { | |
141 | return pool_ == rhs.pool_; | |
142 | } | |
143 | ||
144 | template <typename Right_> | |
145 | inline bool operator!=(const CYPoolAllocator<Right_> &rhs) { | |
146 | return !operator==(rhs); | |
147 | } | |
148 | ||
149 | template <typename Right_> | |
150 | struct rebind { | |
151 | typedef CYPoolAllocator<Right_> other; | |
152 | }; | |
153 | }; | |
154 | ||
2eb8215d JF |
155 | class CYLocalPool : |
156 | public CYPool | |
157 | { | |
158 | private: | |
98b15ef4 | 159 | CYLocal<apr_pool_t> local_; |
2eb8215d JF |
160 | |
161 | public: | |
162 | CYLocalPool() : | |
163 | CYPool(), | |
164 | local_(operator apr_pool_t *()) | |
165 | { | |
166 | } | |
167 | }; | |
168 | ||
169 | #define $pool \ | |
98b15ef4 | 170 | CYLocal<apr_pool_t>::Get() |
2eb8215d | 171 | |
c5fa2867 | 172 | #endif/*CYCRIPT_POOLING_HPP*/ |