aboutsummaryrefslogtreecommitdiffstats
path: root/vespamalloc/src/vespamalloc/util/callgraph.h
blob: 2d66fc8b717ec97d5ebc67ac34e1814c510dc27a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <stdio.h>
#include <vespamalloc/util/stream.h>
#include <memory>

namespace vespamalloc {

template<typename T, typename AddSub>
class CallGraphNode
{
public:
    CallGraphNode() : _callers(nullptr), _next(nullptr), _content(), _count(0) { }
    const CallGraphNode *next()    const { return _next; }
    const CallGraphNode *callers() const { return _callers; }
    const T & content()            const { return _content; }
    CallGraphNode *next()                { return _next; }
    CallGraphNode *callers()             { return _callers; }
    T & content()                        { return _content; }
    size_t count()                 const { return _count; }
    void content(const T & v)            { _content = v; }
    template <typename Store>
    bool addStack(const T * stack, size_t nelem, Store & store);
    template<typename Object>
    void traverseDepth(size_t depth, size_t width, Object func);
    template<typename Object>
    void traverseWidth(size_t depth, size_t width, Object & func);
    friend asciistream & operator << (asciistream & os, const CallGraphNode & v) {
        return os << v._content << '(' << v._count << ')';
    }
private:
    CallGraphNode * _callers;
    CallGraphNode * _next;
    T               _content;
    AddSub          _count;
};

template<typename T, typename AddSub>
template <typename Store>
bool CallGraphNode<T, AddSub>::addStack(const T * stack, size_t nelem, Store & store) {
    bool retval(false);
    if (nelem == 0) {
        retval = true;
    } else if (_content == stack[0]) {
        _count++;
        if (nelem > 1) {
            if (_callers == nullptr) {
                _callers = store.alloc();
                if (_callers != nullptr) {
                    _callers->content(stack[1]);
                }
            }
            if (_callers) {
                retval = _callers->addStack(stack+1, nelem-1, store);
            }
        } else {
            retval = true;
        }
    } else {
        if (_next == nullptr) {
            _next = store.alloc();
            if (_next != nullptr) {
                _next->content(stack[0]);
            }
        }
        if (_next) {
            retval = _next->addStack(stack, nelem, store);
        }
    }
    return retval;
}

template<typename T, typename AddSub>
template<typename Object>
void CallGraphNode<T, AddSub>::traverseDepth(size_t depth, size_t width, Object func) {
    Object newFunc(func);
    newFunc.handle(*this);
    if (_callers) {
        _callers->traverseDepth(depth+1, width, newFunc);
    }
    if (_next) {
        _next->traverseDepth(depth, width+1, func);
    }
}

template<typename T, typename AddSub>
template<typename Object>
void CallGraphNode<T, AddSub>::traverseWidth(size_t depth, size_t width, Object & func) {
    Object newFunc(func);
    newFunc.handle(*this);
    if (_next) {
        _next->traverseWidth(depth, width+1, newFunc);
    }
    if (_callers) {
        _callers->traverseWidth(depth+1, width, func);
    }
}

template<typename T, size_t MaxElem, typename AddSub>
class ArrayStore
{
public:
    ArrayStore() : _used(0) { }
    T * alloc() { return (_used < MaxElem) ? &_array[_used++] : nullptr; }
    AddSub size() const { return _used; }
private:
    AddSub _used;
    T      _array[MaxElem];
};

template <typename Content, size_t MaxElems, typename AddSub>
class CallGraph
{
public:
    using Node = CallGraphNode<Content, AddSub>;

    CallGraph() :
        _root(nullptr),
        _nodeStore(std::make_unique<NodeStore>())
    { }
    CallGraph(Content root) :
        _root(nullptr),
        _nodeStore(std::make_unique<NodeStore>())
    {
        checkOrSetRoot(root);
    }
    bool addStack(const Content * stack, size_t nelem) {
        checkOrSetRoot(stack[0]);
        return _root->addStack(stack, nelem, *_nodeStore);
    }
    template<typename Object>
    void traverseDepth(Object func) {
        if (_root) { _root->traverseDepth(0, 0, func); }
    }
    template<typename Object>
    void traverseWidth(Object func) {
        if (_root) {_root->traverseWidth(0, 0, func); }
    }
    size_t size() const { return _nodeStore->size(); }
    bool empty()  const { return size()==0; }
private:
    CallGraph(const CallGraph &);
    CallGraph & operator = (const CallGraph &);
    bool checkOrSetRoot(const Content & root) {
        if (_root == nullptr) {
            _root = _nodeStore->alloc();
            _root->content(root);
        }
        return (_root != nullptr);
    }
    typedef ArrayStore<Node, MaxElems, AddSub> NodeStore;
    Node                       * _root;
    std::unique_ptr<NodeStore>   _nodeStore;
};

}