]> git.saurik.com Git - cycript.git/blob - Pooling.hpp
Use a dummy space to handle the lower-right corner.
[cycript.git] / Pooling.hpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2012 Jay Freeman (saurik)
3 */
4
5 /* GNU Lesser General Public License, Version 3 {{{ */
6 /*
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.
11 *
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.
16 *
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/>.
19 **/
20 /* }}} */
21
22 #ifndef CYCRIPT_POOLING_HPP
23 #define CYCRIPT_POOLING_HPP
24
25 #include <apr_pools.h>
26 #include <apr_strings.h>
27
28 #include "Exception.hpp"
29 #include "Local.hpp"
30 #include "Standard.hpp"
31
32 #include <cstdlib>
33
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:
47 CYPool(apr_pool_t *pool = NULL) {
48 _aprcall(apr_pool_create(&pool_, pool));
49 }
50
51 ~CYPool() {
52 apr_pool_destroy(pool_);
53 }
54
55 void Clear() {
56 apr_pool_clear(pool_);
57 }
58
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
72 struct CYData {
73 apr_pool_t *pool_;
74 unsigned count_;
75
76 CYData() :
77 count_(1)
78 {
79 }
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 }
99 };
100
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
155 class CYLocalPool :
156 public CYPool
157 {
158 private:
159 CYLocal<apr_pool_t> local_;
160
161 public:
162 CYLocalPool() :
163 CYPool(),
164 local_(operator apr_pool_t *())
165 {
166 }
167 };
168
169 #define $pool \
170 CYLocal<apr_pool_t>::Get()
171
172 #endif/*CYCRIPT_POOLING_HPP*/