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 MarkStack_h
27#define MarkStack_h
28
29#if ENABLE(OBJECT_MARK_LOGGING)
30#define MARK_LOG_MESSAGE0(message) dataLogF(message)
31#define MARK_LOG_MESSAGE1(message, arg1) dataLogF(message, arg1)
32#define MARK_LOG_MESSAGE2(message, arg1, arg2) dataLogF(message, arg1, arg2)
33#define MARK_LOG_ROOT(visitor, rootName) \
34    dataLogF("\n%s: ", rootName); \
35    (visitor).resetChildCount()
36#define MARK_LOG_PARENT(visitor, parent) \
37    dataLogF("\n%p (%s): ", parent, parent->className() ? parent->className() : "unknown"); \
38    (visitor).resetChildCount()
39#define MARK_LOG_CHILD(visitor, child) \
40    if ((visitor).childCount()) \
41    dataLogFString(", "); \
42    dataLogF("%p", child); \
43    (visitor).incrementChildCount()
44#else
45#define MARK_LOG_MESSAGE0(message) do { } while (false)
46#define MARK_LOG_MESSAGE1(message, arg1) do { } while (false)
47#define MARK_LOG_MESSAGE2(message, arg1, arg2) do { } while (false)
48#define MARK_LOG_ROOT(visitor, rootName) do { } while (false)
49#define MARK_LOG_PARENT(visitor, parent) do { } while (false)
50#define MARK_LOG_CHILD(visitor, child) do { } while (false)
51#endif
52
53#include "HeapBlock.h"
54#include <wtf/StdLibExtras.h>
55
56namespace JSC {
57
58class BlockAllocator;
59class DeadBlock;
60class JSCell;
61
62class MarkStackSegment : public HeapBlock<MarkStackSegment> {
63public:
64    MarkStackSegment(Region* region)
65        : HeapBlock<MarkStackSegment>(region)
66#if !ASSERT_DISABLED
67        , m_top(0)
68#endif
69    {
70    }
71
72    static MarkStackSegment* create(DeadBlock*);
73
74    const JSCell** data()
75    {
76        return bitwise_cast<const JSCell**>(this + 1);
77    }
78
79    static const size_t blockSize = 4 * KB;
80
81#if !ASSERT_DISABLED
82    size_t m_top;
83#endif
84};
85
86class MarkStackArray {
87public:
88    MarkStackArray(BlockAllocator&);
89    ~MarkStackArray();
90
91    void append(const JSCell*);
92
93    bool canRemoveLast();
94    const JSCell* removeLast();
95    bool refill();
96
97    void donateSomeCellsTo(MarkStackArray& other);
98    void stealSomeCellsFrom(MarkStackArray& other, size_t idleThreadCount);
99
100    size_t size();
101    bool isEmpty();
102
103private:
104    template <size_t size> struct CapacityFromSize {
105        static const size_t value = (size - sizeof(MarkStackSegment)) / sizeof(const JSCell*);
106    };
107
108    JS_EXPORT_PRIVATE void expand();
109
110    size_t postIncTop();
111    size_t preDecTop();
112    void setTopForFullSegment();
113    void setTopForEmptySegment();
114    size_t top();
115
116    void validatePrevious();
117
118    DoublyLinkedList<MarkStackSegment> m_segments;
119    BlockAllocator& m_blockAllocator;
120
121    JS_EXPORT_PRIVATE static const size_t s_segmentCapacity = CapacityFromSize<MarkStackSegment::blockSize>::value;
122    size_t m_top;
123    size_t m_numberOfSegments;
124
125};
126
127} // namespace JSC
128
129#endif
130