]>
git.saurik.com Git - apple/javascriptcore.git/blob - heap/CopiedAllocator.h
2 * Copyright (C) 2011 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 CopiedAllocator_h
27 #define CopiedAllocator_h
29 #include "CopiedBlock.h"
33 class CopiedAllocator
{
37 void* allocate(size_t);
38 bool fitsInCurrentBlock(size_t);
39 bool wasLastAllocation(void*, size_t);
40 void startedCopying();
41 void resetCurrentBlock(CopiedBlock
*);
42 size_t currentCapacity();
45 CopiedBlock
* currentBlock() { return m_currentBlock
; }
47 char* m_currentOffset
;
48 CopiedBlock
* m_currentBlock
;
51 inline CopiedAllocator::CopiedAllocator()
57 inline void* CopiedAllocator::allocate(size_t bytes
)
59 ASSERT(m_currentOffset
);
60 ASSERT(is8ByteAligned(reinterpret_cast<void*>(bytes
)));
61 ASSERT(fitsInCurrentBlock(bytes
));
62 void* ptr
= static_cast<void*>(m_currentOffset
);
63 m_currentOffset
+= bytes
;
64 ASSERT(is8ByteAligned(ptr
));
68 inline bool CopiedAllocator::fitsInCurrentBlock(size_t bytes
)
70 return m_currentOffset
+ bytes
< reinterpret_cast<char*>(m_currentBlock
) + HeapBlock::s_blockSize
&& m_currentOffset
+ bytes
> m_currentOffset
;
73 inline bool CopiedAllocator::wasLastAllocation(void* ptr
, size_t size
)
75 return static_cast<char*>(ptr
) + size
== m_currentOffset
&& ptr
> m_currentBlock
&& ptr
< reinterpret_cast<char*>(m_currentBlock
) + HeapBlock::s_blockSize
;
78 inline void CopiedAllocator::startedCopying()
81 m_currentBlock
->m_offset
= static_cast<void*>(m_currentOffset
);
86 inline void CopiedAllocator::resetCurrentBlock(CopiedBlock
* newBlock
)
89 m_currentBlock
->m_offset
= static_cast<void*>(m_currentOffset
);
90 m_currentBlock
= newBlock
;
91 m_currentOffset
= static_cast<char*>(newBlock
->m_offset
);
94 inline size_t CopiedAllocator::currentCapacity()
96 return m_currentBlock
->capacity();