summaryrefslogtreecommitdiffstats
path: root/src/gc-anchored.cpp
blob: 3829e54ddb08e3f0ae1405a0e72a2f431ee43014 (plain)
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
/*
 * Inkscape::GC::Anchored - base class for anchored GC-managed objects
 *
 * Authors:
 *   MenTaLguY <mental@rydia.net>
 *
 * Copyright (C) 2004 MenTaLguY
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include <typeinfo>
#include "gc-anchored.h"
#include "debug/event-tracker.h"
#include "debug/event.h"
#include "util/share.h"
#include "util/format.h"

namespace Inkscape {

namespace GC {

class AnchorEvent : public Debug::Event {
public:
    enum Type { ANCHOR, RELEASE };

    AnchorEvent(Anchored const *object, Type type)
    : _base(Util::format("%p", Core::base(const_cast<Anchored *>(object)))),
      _object(Util::format("%p", object)),
      _class_name(Util::share_static_string(typeid(*object).name())),
      _refcount(Util::format("%d", ( type == ANCHOR ? object->_anchored_refcount() + 1 : object->_anchored_refcount() - 1 ))),
      _type(type)
    {}

    static Category category() { return REFCOUNT; }

    Util::ptr_shared<char> name() const {
        if ( _type == ANCHOR ) {
            return Util::share_static_string("gc-anchor");
        } else {
            return Util::share_static_string("gc-release");
        }
    }
    unsigned propertyCount() const { return 4; }
    PropertyPair property(unsigned index) const {
        switch (index) {
            case 0:
                return PropertyPair("base", _base);
            case 1:
                return PropertyPair("pointer", _object);
            case 2:
                return PropertyPair("class", _class_name);
            case 3:
                return PropertyPair("new-refcount", _refcount);
            default:
                return PropertyPair();
        }
    }

private:
    Util::ptr_shared<char> _base;
    Util::ptr_shared<char> _object;
    Util::ptr_shared<char> _class_name;
    Util::ptr_shared<char> _refcount;
    Type _type;
};

Anchored::Anchor *Anchored::_new_anchor() const {
    return new Anchor(this);
}

void Anchored::_free_anchor(Anchored::Anchor *anchor) const {
    delete anchor;
}

void Anchored::anchor() const {
    Debug::EventTracker<AnchorEvent> tracker(this, AnchorEvent::ANCHOR);
    if (!_anchor) {
        _anchor = _new_anchor();
    }
    _anchor->refcount++;
}

void Anchored::release() const {
    Debug::EventTracker<AnchorEvent> tracker(this, AnchorEvent::RELEASE);
    if (!--_anchor->refcount) {
        _free_anchor(_anchor);
        _anchor = NULL;
    }
}

}

}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :