]>
Commit | Line | Data |
---|---|---|
6fe7ccc8 A |
1 | /* |
2 | * Copyright (C) 2011 Apple Inc. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
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. | |
12 | * | |
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. | |
24 | */ | |
25 | ||
26 | #ifndef CopiedAllocator_h | |
27 | #define CopiedAllocator_h | |
28 | ||
29 | #include "CopiedBlock.h" | |
30 | ||
31 | namespace JSC { | |
32 | ||
33 | class CopiedAllocator { | |
34 | friend class JIT; | |
35 | public: | |
36 | 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(); | |
43 | ||
44 | private: | |
45 | CopiedBlock* currentBlock() { return m_currentBlock; } | |
46 | ||
47 | char* m_currentOffset; | |
48 | CopiedBlock* m_currentBlock; | |
49 | }; | |
50 | ||
51 | inline CopiedAllocator::CopiedAllocator() | |
52 | : m_currentOffset(0) | |
53 | , m_currentBlock(0) | |
54 | { | |
55 | } | |
56 | ||
57 | inline void* CopiedAllocator::allocate(size_t bytes) | |
58 | { | |
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)); | |
65 | return ptr; | |
66 | } | |
67 | ||
68 | inline bool CopiedAllocator::fitsInCurrentBlock(size_t bytes) | |
69 | { | |
70 | return m_currentOffset + bytes < reinterpret_cast<char*>(m_currentBlock) + HeapBlock::s_blockSize && m_currentOffset + bytes > m_currentOffset; | |
71 | } | |
72 | ||
73 | inline bool CopiedAllocator::wasLastAllocation(void* ptr, size_t size) | |
74 | { | |
75 | return static_cast<char*>(ptr) + size == m_currentOffset && ptr > m_currentBlock && ptr < reinterpret_cast<char*>(m_currentBlock) + HeapBlock::s_blockSize; | |
76 | } | |
77 | ||
78 | inline void CopiedAllocator::startedCopying() | |
79 | { | |
80 | if (m_currentBlock) | |
81 | m_currentBlock->m_offset = static_cast<void*>(m_currentOffset); | |
82 | m_currentOffset = 0; | |
83 | m_currentBlock = 0; | |
84 | } | |
85 | ||
86 | inline void CopiedAllocator::resetCurrentBlock(CopiedBlock* newBlock) | |
87 | { | |
88 | if (m_currentBlock) | |
89 | m_currentBlock->m_offset = static_cast<void*>(m_currentOffset); | |
90 | m_currentBlock = newBlock; | |
91 | m_currentOffset = static_cast<char*>(newBlock->m_offset); | |
92 | } | |
93 | ||
94 | inline size_t CopiedAllocator::currentCapacity() | |
95 | { | |
96 | return m_currentBlock->capacity(); | |
97 | } | |
98 | ||
99 | } // namespace JSC | |
100 | ||
101 | #endif |