Fix gcc and clang builds.
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_
29 // Allocates memory in chunks that are not freed until the entire allocator
30 // is freed. This is intended for use with large, temporary data structures.
33 friend class PoolAllocationScope;
42 return size_t(end - base);
46 static const size_t kDefaultPoolSize = 8 * 1024;
47 static const size_t kMaxReserveSize = 64 * 1024;
54 void unwind(char *pos);
55 void *slowAllocate(size_t actualBytes);
61 void *rawAllocate(size_t bytes) {
62 // Guarantee malloc alignment.
63 size_t actualBytes = Align(bytes, kMallocAlignment);
64 if (!last_ || (size_t(last_->end - last_->ptr) < actualBytes))
65 return slowAllocate(actualBytes);
66 char *ptr = last_->ptr;
67 last_->ptr += actualBytes;
72 T *alloc(size_t count = 1) {
73 void *ptr = rawAllocate(count * sizeof(T));
77 return reinterpret_cast<T *>(ptr);
81 void leave(char *position);
90 PoolScope(PoolAllocator &allocator)
92 position_(allocator.enter())
97 pool_.leave(position_);
104 void *operator new(size_t size, PoolAllocator &pool) {
105 return pool.rawAllocate(size);
107 void *operator new [](size_t size, PoolAllocator &pool) {
108 return pool.rawAllocate(size);
111 // Using delete on pool-allocated objects is illegal.
112 void operator delete(void *ptr, PoolAllocator &pool) {
115 void operator delete [](void *ptr, PoolAllocator &pool) {
120 class PoolAllocationPolicy
123 void reportAllocationOverflow();
124 void reportOutOfMemory();
127 void *malloc(size_t bytes);
128 void free(void *ptr);
131 template <typename T>
133 : public Vector<T, PoolAllocationPolicy>,
138 : Vector<T, PoolAllocationPolicy>(PoolAllocationPolicy())
145 #endif // _include_jitcraft_pool_allocator_h_