2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef BumpPointerAllocator_h
27 #define BumpPointerAllocator_h
30 #include <wtf/PageAllocation.h>
31 #include <wtf/PageBlock.h>
35 #define MINIMUM_BUMP_POOL_SIZE 0x1000
37 class BumpPointerPool {
39 // ensureCapacity will check whether the current pool has capacity to
40 // allocate 'size' bytes of memory If it does not, it will attempt to
41 // allocate a new pool (which will be added to this one in a chain).
43 // If allocation fails (out of memory) this method will return null.
44 // If the return value is non-null, then callers should update any
45 // references they have to this current (possibly full) BumpPointerPool
46 // to instead point to the newly returned BumpPointerPool.
47 BumpPointerPool* ensureCapacity(size_t size)
49 void* allocationEnd = static_cast<char*>(m_current) + size;
50 ASSERT(allocationEnd > m_current); // check for overflow
51 if (allocationEnd <= static_cast<void*>(this))
53 return ensureCapacityCrossPool(this, size);
56 // alloc should only be called after calling ensureCapacity; as such
57 // alloc will never fail.
58 void* alloc(size_t size)
60 void* current = m_current;
61 void* allocationEnd = static_cast<char*>(current) + size;
62 ASSERT(allocationEnd > current); // check for overflow
63 ASSERT(allocationEnd <= static_cast<void*>(this));
64 m_current = allocationEnd;
68 // The dealloc method releases memory allocated using alloc. Memory
69 // must be released in a LIFO fashion, e.g. if the client calls alloc
70 // four times, returning pointer A, B, C, D, then the only valid order
71 // in which these may be deallocaed is D, C, B, A.
73 // The client may optionally skip some deallocations. In the example
74 // above, it would be valid to only explicitly dealloc C, A (D being
75 // dealloced along with C, B along with A).
77 // If pointer was not allocated from this pool (or pools) then dealloc
78 // will CRASH(). Callers should update any references they have to
79 // this current BumpPointerPool to instead point to the returned
81 BumpPointerPool* dealloc(void* position)
83 if ((position >= m_start) && (position <= static_cast<void*>(this))) {
84 ASSERT(position <= m_current);
88 return deallocCrossPool(this, position);
92 // Placement operator new, returns the last 'size' bytes of allocation for use as this.
93 void* operator new(size_t size, const PageAllocation& allocation)
95 ASSERT(size < allocation.size());
96 return reinterpret_cast<char*>(reinterpret_cast<intptr_t>(allocation.base()) + allocation.size()) - size;
99 BumpPointerPool(const PageAllocation& allocation)
100 : m_current(allocation.base())
101 , m_start(allocation.base())
104 , m_allocation(allocation)
108 static BumpPointerPool* create(size_t minimumCapacity = 0)
110 // Add size of BumpPointerPool object, check for overflow.
111 minimumCapacity += sizeof(BumpPointerPool);
112 if (minimumCapacity < sizeof(BumpPointerPool))
115 size_t poolSize = std::max(static_cast<size_t>(MINIMUM_BUMP_POOL_SIZE), WTF::pageSize());
116 while (poolSize < minimumCapacity) {
118 // The following if check relies on MINIMUM_BUMP_POOL_SIZE being a power of 2!
119 ASSERT(!(MINIMUM_BUMP_POOL_SIZE & (MINIMUM_BUMP_POOL_SIZE - 1)));
124 PageAllocation allocation = PageAllocation::allocate(poolSize);
126 return new (allocation) BumpPointerPool(allocation);
135 BumpPointerPool* nextNext = m_next->m_next;
143 m_allocation.deallocate();
146 static BumpPointerPool* ensureCapacityCrossPool(BumpPointerPool* previousPool, size_t size)
148 // The pool passed should not have capacity, so we'll start with the next one.
149 ASSERT(previousPool);
150 ASSERT((static_cast<char*>(previousPool->m_current) + size) > previousPool->m_current); // check for overflow
151 ASSERT((static_cast<char*>(previousPool->m_current) + size) > static_cast<void*>(previousPool));
152 BumpPointerPool* pool = previousPool->m_next;
156 // We've run to the end; allocate a new pool.
157 pool = BumpPointerPool::create(size);
158 previousPool->m_next = pool;
159 pool->m_previous = previousPool;
164 void* current = pool->m_current;
165 void* allocationEnd = static_cast<char*>(current) + size;
166 ASSERT(allocationEnd > current); // check for overflow
167 if (allocationEnd <= static_cast<void*>(pool))
172 static BumpPointerPool* deallocCrossPool(BumpPointerPool* pool, void* position)
174 // Should only be called if position is not in the current pool.
175 ASSERT((position < pool->m_start) || (position > static_cast<void*>(pool)));
178 // Unwind the current pool to the start, move back in the chain to the previous pool.
179 pool->m_current = pool->m_start;
180 pool = pool->m_previous;
182 // position was nowhere in the chain!
186 if ((position >= pool->m_start) && (position <= static_cast<void*>(pool))) {
187 ASSERT(position <= pool->m_current);
188 pool->m_current = position;
196 BumpPointerPool* m_next;
197 BumpPointerPool* m_previous;
198 PageAllocation m_allocation;
200 friend class BumpPointerAllocator;
203 // A BumpPointerAllocator manages a set of BumpPointerPool objects, which
204 // can be used for LIFO (stack like) allocation.
206 // To begin allocating using this class call startAllocator(). The result
207 // of this method will be null if the initial pool allocation fails, or a
208 // pointer to a BumpPointerPool object that can be used to perform
209 // allocations. Whilst running no memory will be released until
210 // stopAllocator() is called. At this point all allocations made through
211 // this allocator will be reaped, and underlying memory may be freed.
213 // (In practice we will still hold on to the initial pool to allow allocation
214 // to be quickly restared, but aditional pools will be freed).
216 // This allocator is non-renetrant, it is encumbant on the clients to ensure
217 // startAllocator() is not called again until stopAllocator() has been called.
218 class BumpPointerAllocator {
220 BumpPointerAllocator()
225 ~BumpPointerAllocator()
231 BumpPointerPool* startAllocator()
234 m_head = BumpPointerPool::create();
245 BumpPointerPool* m_head;
250 using WTF::BumpPointerAllocator;
252 #endif // BumpPointerAllocator_h