Implement getPrimitiveForTag.
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 *allocate(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 void leave(char *position);
81 PoolScope(PoolAllocator &allocator)
83 position_(allocator.enter())
88 pool_.leave(position_);
95 void *operator new(size_t size, PoolAllocator &pool) {
96 return pool.allocate(size);
98 void *operator new [](size_t size, PoolAllocator &pool) {
99 return pool.allocate(size);
102 // Using delete on pool-allocated objects is illegal.
103 void operator delete(void *ptr, PoolAllocator &pool) {
106 void operator delete [](void *ptr, PoolAllocator &pool) {
111 class PoolAllocationPolicy
114 void reportAllocationOverflow();
115 void reportOutOfMemory();
118 void *malloc(size_t bytes);
119 void free(void *ptr);
122 template <typename T>
124 : public Vector<T, PoolAllocationPolicy>,
129 : Vector<T, PoolAllocationPolicy>(PoolAllocationPolicy())
136 #endif // _include_jitcraft_pool_allocator_h_