]> git.saurik.com Git - cydia.git/blob - Menes/Pooling.hpp
Cytore values need to be _packed (I got lucky :D).
[cydia.git] / Menes / Pooling.hpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #ifndef Menes_Pooling_HPP
23 #define Menes_Pooling_HPP
24
25 #include <cstdarg>
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cstring>
29
30 #include <algorithm>
31
32 #include <stdint.h>
33
34 class CYPool;
35 _finline void *operator new(size_t size, CYPool &pool);
36 _finline void *operator new [](size_t size, CYPool &pool);
37
38 class CYPool {
39 private:
40 uint8_t *data_;
41 size_t size_;
42
43 struct Cleaner {
44 Cleaner *next_;
45 void (*code_)(void *);
46 void *data_;
47
48 Cleaner(Cleaner *next, void (*code)(void *), void *data) :
49 next_(next),
50 code_(code),
51 data_(data)
52 {
53 }
54 } *cleaner_;
55
56 static _finline size_t align(size_t size) {
57 // XXX: alignment is more complex than this
58 return (size + 7) & ~0x3;
59 }
60
61 template <typename Type_>
62 static void delete_(void *data) {
63 reinterpret_cast<Type_ *>(data)->~Type_();
64 }
65
66 CYPool(const CYPool &);
67
68 public:
69 CYPool() :
70 data_(NULL),
71 size_(0),
72 cleaner_(NULL)
73 {
74 }
75
76 ~CYPool() {
77 for (Cleaner *cleaner(cleaner_); cleaner != NULL; ) {
78 Cleaner *next(cleaner->next_);
79 (*cleaner->code_)(cleaner->data_);
80 cleaner = next;
81 }
82 }
83
84 template <typename Type_>
85 Type_ *malloc(size_t size) {
86 size = align(size);
87
88 if (size > size_) {
89 // XXX: is this an optimal malloc size?
90 size_ = std::max<size_t>(size, size + align(sizeof(Cleaner)));
91 data_ = reinterpret_cast<uint8_t *>(::malloc(size_));
92 atexit(free, data_);
93 _assert(size <= size_);
94 }
95
96 void *data(data_);
97 data_ += size;
98 size_ -= size;
99 return reinterpret_cast<Type_ *>(data);
100 }
101
102 void atexit(void (*code)(void *), void *data = NULL);
103 };
104
105 _finline void *operator new(size_t size, CYPool &pool) {
106 return pool.malloc<void>(size);
107 }
108
109 _finline void *operator new [](size_t size, CYPool &pool) {
110 return pool.malloc<void>(size);
111 }
112
113 _finline void CYPool::atexit(void (*code)(void *), void *data) {
114 cleaner_ = new(*this) Cleaner(cleaner_, code, data);
115 }
116
117 #endif/*Menes_Pooling_HPP*/