]> git.saurik.com Git - cycript.git/blame - Pooling.hpp
Move CYNext, CYSetLast, and CYForEach to List.hpp.
[cycript.git] / Pooling.hpp
CommitLineData
b3378a02
JF
1/* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
4644480a
JF
3*/
4
b3378a02 5/* GNU Lesser General Public License, Version 3 {{{ */
4644480a 6/*
b3378a02
JF
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.
4644480a 11 *
b3378a02
JF
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.
4644480a 16 *
b3378a02
JF
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**/
4644480a
JF
20/* }}} */
21
5999c315
JF
22#ifndef CYPOOLING_HPP
23#define CYPOOLING_HPP
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
42class CYPool {
43 private:
44 apr_pool_t *pool_;
45
46 public:
7e5391fd
JF
47 CYPool(apr_pool_t *pool = NULL) {
48 _aprcall(apr_pool_create(&pool_, pool));
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
72struct 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
101template <typename Type_>
102struct 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
155class 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
5999c315 172#endif/*CYPOOLING_HPP*/