Added support for fused math-assign ops (+=, etc).
2 * Copyright (C) 2012 David Anderson
4 * This file is part of JITCraft.
6 * JITCraft is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * JITCraft. If not, see http://www.gnu.org/licenses/.
18 #ifndef _include_jitcraft_pool_allocator_h_
19 #define _include_jitcraft_pool_allocator_h_
28 // Allocates memory in chunks that are not freed until the entire allocator
29 // is freed. This is intended for use with large, temporary data structures.
32 friend class PoolAllocationScope;
41 return size_t(end - base);
45 static const size_t kDefaultPoolSize = 8 * 1024;
46 static const size_t kMaxReserveSize = 64 * 1024;
53 void unwind(char *pos);
54 void *slowAllocate(size_t actualBytes);
60 void *allocate(size_t bytes) {
61 // Guarantee malloc alignment.
62 size_t actualBytes = Align(bytes, kMallocAlignment);
63 if (!last_ || (size_t(last_->end - last_->ptr) < actualBytes))
64 return slowAllocate(actualBytes);
65 char *ptr = last_->ptr;
66 last_->ptr += actualBytes;
71 void leave(char *position);
80 PoolScope(PoolAllocator &allocator)
82 position_(allocator.enter())
87 pool_.leave(position_);
94 void *operator new(size_t size, PoolAllocator &pool) {
95 return pool.allocate(size);
97 void *operator new [](size_t size, PoolAllocator &pool) {
98 return pool.allocate(size);
101 // Using delete on pool-allocated objects is illegal.
102 void operator delete(void *ptr, PoolAllocator &pool) {
105 void operator delete [](void *ptr, PoolAllocator &pool) {
110 // Simple vector<> lookalike that can use the pool allocator.
111 template <typename T>
112 class PoolList : public PoolObject
119 void ensure(PoolAllocator &pool, size_t n) {
120 if (max_ - length_ >= n)
123 size_t newMax = max_;
126 while (newMax - length_ < n) {
127 // Make sure the request does not overflow.
128 if (newMax >= INT_MAX)
129 throw std::bad_alloc();
133 // Ensure no overflow.
134 if (newMax > UINT_MAX / sizeof(T))
135 throw std::bad_alloc();
137 T *new_ = (T *)pool.allocate(newMax * sizeof(T));
138 for (size_t i = 0; i < length_; i++)
139 new (&new_[i]) T(t_[i]);
154 void append(PoolAllocator &pool, const T &t) {
156 new (&t_[length_]) T(t);
160 size_t length() const {
165 assert(i < length());
172 #endif // _include_jitcraft_pool_allocator_h_