1/*
2 * Copyright (C) 2009, 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 MarkStackInlines_h
27#define MarkStackInlines_h
28
29#include "GCThreadSharedData.h"
30#include "MarkStack.h"
31
32namespace JSC {
33
34inline MarkStackSegment* MarkStackSegment::create(DeadBlock* block)
35{
36    return new (NotNull, block) MarkStackSegment(block->region());
37}
38
39inline size_t MarkStackArray::postIncTop()
40{
41    size_t result = m_top++;
42    ASSERT(result == m_segments.head()->m_top++);
43    return result;
44}
45
46inline size_t MarkStackArray::preDecTop()
47{
48    size_t result = --m_top;
49    ASSERT(result == --m_segments.head()->m_top);
50    return result;
51}
52
53inline void MarkStackArray::setTopForFullSegment()
54{
55    ASSERT(m_segments.head()->m_top == s_segmentCapacity);
56    m_top = s_segmentCapacity;
57}
58
59inline void MarkStackArray::setTopForEmptySegment()
60{
61    ASSERT(!m_segments.head()->m_top);
62    m_top = 0;
63}
64
65inline size_t MarkStackArray::top()
66{
67    ASSERT(m_top == m_segments.head()->m_top);
68    return m_top;
69}
70
71#if ASSERT_DISABLED
72inline void MarkStackArray::validatePrevious() { }
73#else
74inline void MarkStackArray::validatePrevious()
75{
76    unsigned count = 0;
77    for (MarkStackSegment* current = m_segments.head(); current; current = current->next())
78        count++;
79    ASSERT(m_segments.size() == m_numberOfSegments);
80}
81#endif
82
83inline void MarkStackArray::append(const JSCell* cell)
84{
85    if (m_top == s_segmentCapacity)
86        expand();
87    m_segments.head()->data()[postIncTop()] = cell;
88}
89
90inline bool MarkStackArray::canRemoveLast()
91{
92    return !!m_top;
93}
94
95inline const JSCell* MarkStackArray::removeLast()
96{
97    return m_segments.head()->data()[preDecTop()];
98}
99
100inline bool MarkStackArray::isEmpty()
101{
102    if (m_top)
103        return false;
104    if (m_segments.head()->next()) {
105        ASSERT(m_segments.head()->next()->m_top == s_segmentCapacity);
106        return false;
107    }
108    return true;
109}
110
111inline size_t MarkStackArray::size()
112{
113    return m_top + s_segmentCapacity * (m_numberOfSegments - 1);
114}
115
116} // namespace JSC
117
118#endif // MarkStackInlines_h
119
120