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 g_return_if_fail(_anchor);
76 if (!--_anchor->refcount) {
77 _free_anchor(_anchor);
78 _anchor = NULL;
79 }
80 }
82 }
84 }
86 /*
87 Local Variables:
88 mode:c++
89 c-file-style:"stroustrup"
90 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
91 indent-tabs-mode:nil
92 fill-column:99
93 End:
94 */
95 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :