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#include <wtf/CheckedBoolean.h>
31
32namespace JSC {
33
34class CopiedAllocator {
35public:
36 CopiedAllocator();
37
38 bool fastPathShouldSucceed(size_t bytes) const;
39 CheckedBoolean tryAllocate(size_t bytes, void** outPtr);
40 CheckedBoolean tryAllocateDuringCopying(size_t bytes, void** outPtr);
41 CheckedBoolean tryReallocate(void *oldPtr, size_t oldBytes, size_t newBytes);
42 void* forceAllocate(size_t bytes);
43 CopiedBlock* resetCurrentBlock();
44 void setCurrentBlock(CopiedBlock*);
45 size_t currentCapacity();
46
47 bool isValid() const { return !!m_currentBlock; }
48
49 CopiedBlock* currentBlock() { return m_currentBlock; }
50
51 // Yes, these are public. No, that doesn't mean you can play with them.
52 // If I had made them private then I'd have to list off all of the JIT
53 // classes and functions that are entitled to modify these directly, and
54 // that would have been gross.
55 size_t m_currentRemaining;
56 char* m_currentPayloadEnd;
57 CopiedBlock* m_currentBlock;
58};
59
60inline CopiedAllocator::CopiedAllocator()
61 : m_currentRemaining(0)
62 , m_currentPayloadEnd(0)
63 , m_currentBlock(0)
64{
65}
66
67inline bool CopiedAllocator::fastPathShouldSucceed(size_t bytes) const
68{
69 ASSERT(is8ByteAligned(reinterpret_cast<void*>(bytes)));
70
71 return bytes <= m_currentRemaining;
72}
73
74inline CheckedBoolean CopiedAllocator::tryAllocate(size_t bytes, void** outPtr)
75{
76 ASSERT(is8ByteAligned(reinterpret_cast<void*>(bytes)));
77
78 // This code is written in a gratuitously low-level manner, in order to
79 // serve as a kind of template for what the JIT would do. Note that the
80 // way it's written it ought to only require one register, which doubles
81 // as the result, provided that the compiler does a minimal amount of
82 // control flow simplification and the bytes argument is a constant.
83
84 size_t currentRemaining = m_currentRemaining;
85 if (bytes > currentRemaining)
86 return false;
87 currentRemaining -= bytes;
88 m_currentRemaining = currentRemaining;
89 *outPtr = m_currentPayloadEnd - currentRemaining - bytes;
90
91 ASSERT(is8ByteAligned(*outPtr));
92
93 return true;
94}
95
96inline CheckedBoolean CopiedAllocator::tryAllocateDuringCopying(size_t bytes, void** outPtr)
97{
98 if (!tryAllocate(bytes, outPtr))
99 return false;
100 m_currentBlock->reportLiveBytesDuringCopying(bytes);
101 return true;
102}
103
104inline CheckedBoolean CopiedAllocator::tryReallocate(
105 void* oldPtr, size_t oldBytes, size_t newBytes)
106{
107 ASSERT(is8ByteAligned(oldPtr));
108 ASSERT(is8ByteAligned(reinterpret_cast<void*>(oldBytes)));
109 ASSERT(is8ByteAligned(reinterpret_cast<void*>(newBytes)));
110
111 ASSERT(newBytes > oldBytes);
112
113 size_t additionalBytes = newBytes - oldBytes;
114
115 size_t currentRemaining = m_currentRemaining;
116 if (m_currentPayloadEnd - currentRemaining - oldBytes != static_cast<char*>(oldPtr))
117 return false;
118
119 if (additionalBytes > currentRemaining)
120 return false;
121
122 m_currentRemaining = currentRemaining - additionalBytes;
123
124 return true;
125}
126
127inline void* CopiedAllocator::forceAllocate(size_t bytes)
128{
129 void* result = 0; // Needed because compilers don't realize this will always be assigned.
130 CheckedBoolean didSucceed = tryAllocate(bytes, &result);
131 ASSERT(didSucceed);
132 return result;
133}
134
135inline CopiedBlock* CopiedAllocator::resetCurrentBlock()
136{
137 CopiedBlock* result = m_currentBlock;
138 if (result) {
139 result->m_remaining = m_currentRemaining;
140 m_currentBlock = 0;
141 m_currentRemaining = 0;
142 m_currentPayloadEnd = 0;
143 }
144 return result;
145}
146
147inline void CopiedAllocator::setCurrentBlock(CopiedBlock* newBlock)
148{
149 ASSERT(!m_currentBlock);
150 m_currentBlock = newBlock;
151 ASSERT(newBlock);
152 m_currentRemaining = newBlock->m_remaining;
153 m_currentPayloadEnd = newBlock->payloadEnd();
154}
155
156inline size_t CopiedAllocator::currentCapacity()
157{
158 if (!m_currentBlock)
159 return 0;
160 return m_currentBlock->capacity();
161}
162
163} // namespace JSC
164
165#endif
166