1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CallbackStack_h
6#define CallbackStack_h
7
8#include "platform/heap/ThreadState.h"
9
10namespace blink {
11
12// The CallbackStack contains all the visitor callbacks used to trace and mark
13// objects. A specific CallbackStack instance contains at most bufferSize elements.
14// If more space is needed a new CallbackStack instance is created and chained
15// together with the former instance. I.e. a logical CallbackStack can be made of
16// multiple chained CallbackStack object instances.
17class CallbackStack {
18public:
19    class Item {
20    public:
21        Item() { }
22        Item(void* object, VisitorCallback callback)
23            : m_object(object)
24            , m_callback(callback)
25        {
26        }
27        void* object() { return m_object; }
28        VisitorCallback callback() { return m_callback; }
29        void call(Visitor* visitor) { m_callback(visitor, m_object); }
30
31    private:
32        void* m_object;
33        VisitorCallback m_callback;
34    };
35
36    CallbackStack();
37    ~CallbackStack();
38
39    void clear();
40
41    Item* allocateEntry();
42    Item* pop();
43
44    bool isEmpty() const;
45    bool sizeExceeds(size_t) const;
46
47    void append(CallbackStack*);
48    void takeBlockFrom(CallbackStack*);
49
50    void invokeEphemeronCallbacks(Visitor*);
51
52#if ENABLE(ASSERT)
53    bool hasCallbackForObject(const void*);
54#endif
55
56    static const size_t blockSize = 200;
57
58private:
59    class Block;
60
61    void invokeOldestCallbacks(Block*, Block*, Visitor*);
62    bool hasJustOneBlock() const;
63    void swap(CallbackStack* other);
64
65    Block* m_first;
66    Block* m_last;
67};
68
69}
70
71#endif // CallbackStack_h
72