1 /*
2 * Inkscape::GC::Anchored - base class for anchored GC-managed objects
3 *
4 * Authors:
5 * MenTaLguY <mental@rydia.net>
6 *
7 * Copyright (C) 2004 MenTaLguY
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 */
12 #include <typeinfo>
13 #include "gc-anchored.h"
14 #include "debug/event-tracker.h"
15 #include "debug/simple-event.h"
16 #include "debug/demangle.h"
17 #include "util/share.h"
18 #include "util/format.h"
20 namespace Inkscape {
22 namespace GC {
24 namespace {
26 typedef Debug::SimpleEvent<Debug::Event::REFCOUNT> RefCountEvent;
28 class BaseAnchorEvent : public RefCountEvent {
29 public:
30 BaseAnchorEvent(Anchored const *object, int bias,
31 Util::ptr_shared<char> name)
32 : RefCountEvent(name)
33 {
34 _addProperty("base", Util::format("%p", Core::base(const_cast<Anchored *>(object))));
35 _addProperty("pointer", Util::format("%p", object));
36 _addProperty("class", Debug::demangle(typeid(*object).name()));
37 _addProperty("new-refcount", Util::format("%d", object->_anchored_refcount() + bias));
38 }
39 };
41 class AnchorEvent : public BaseAnchorEvent {
42 public:
43 AnchorEvent(Anchored const *object)
44 : BaseAnchorEvent(object, 1, Util::share_static_string("gc-anchor"))
45 {}
46 };
48 class ReleaseEvent : public BaseAnchorEvent {
49 public:
50 ReleaseEvent(Anchored const *object)
51 : BaseAnchorEvent(object, -1, Util::share_static_string("gc-release"))
52 {}
53 };
55 }
57 Anchored::Anchor *Anchored::_new_anchor() const {
58 return new Anchor(this);
59 }
61 void Anchored::_free_anchor(Anchored::Anchor *anchor) const {
62 delete anchor;
63 }
65 void Anchored::anchor() const {
66 Debug::EventTracker<AnchorEvent> tracker(this);
67 if (!_anchor) {
68 _anchor = _new_anchor();
69 }
70 _anchor->refcount++;
71 }
73 void Anchored::release() const {
74 Debug::EventTracker<ReleaseEvent> tracker(this);
75 if (!--_anchor->refcount) {
76 _free_anchor(_anchor);
77 _anchor = NULL;
78 }
79 }
81 }
83 }
85 /*
86 Local Variables:
87 mode:c++
88 c-file-style:"stroustrup"
89 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
90 indent-tabs-mode:nil
91 fill-column:99
92 End:
93 */
94 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :