From cb9b008a8a93da19f2d40795774d10f1bca4423b Mon Sep 17 00:00:00 2001 From: mental Date: Wed, 15 Mar 2006 04:07:41 +0000 Subject: [PATCH] Replace GC::Managed<>::clearOnceInaccessible with GC::soft_ptr<> --- ChangeLog | 8 ++++ src/Makefile_insert | 1 + src/display/nr-arena-item.cpp | 7 ---- src/display/nr-arena-item.h | 9 ++--- src/gc-finalized.h | 17 +++----- src/gc-managed.h | 20 ---------- src/gc-soft-ptr.h | 74 +++++++++++++++++++++++++++++++++++ src/selection.cpp | 1 - src/selection.h | 3 +- 9 files changed, 95 insertions(+), 45 deletions(-) create mode 100644 src/gc-soft-ptr.h diff --git a/ChangeLog b/ChangeLog index dbebc8778..d6a4c057f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2006-03-14 MenTaLguY + + * src/Makefile_insert, src/display/nr-arena-item.cpp, + src/display/nr-arena-item.cpp, src/gc-finalized.h, src/gc-managed.h, + src/gc-soft-ptr.h, src/selection.h, src/selection.cpp: + + Replace GC::Managed<>::clearOnceInaccessible with GC::soft_ptr<> + 2006-03-14 MenTaLguY * src/debug/event.h, src/debug/gc-heap.h, src/debug/heap.h, diff --git a/src/Makefile_insert b/src/Makefile_insert index d42c2fa3b..22c1a2209 100644 --- a/src/Makefile_insert +++ b/src/Makefile_insert @@ -228,6 +228,7 @@ libinkpost_a_SOURCES = \ gc-core.h \ gc-finalized.h \ gc-managed.h \ + gc-soft-ptr.h \ gc.cpp \ gradient-chemistry.cpp gradient-chemistry.h \ memeq.h \ diff --git a/src/display/nr-arena-item.cpp b/src/display/nr-arena-item.cpp index ccabe7b28..e816460b9 100644 --- a/src/display/nr-arena-item.cpp +++ b/src/display/nr-arena-item.cpp @@ -63,13 +63,6 @@ nr_arena_item_class_init (NRArenaItemClass *klass) object_class->cpp_ctor = NRObject::invoke_ctor; } -NRArenaItem::NRArenaItem() { - // clear all reverse-pointing pointers before finalization - clearOnceInaccessible(&arena); - clearOnceInaccessible(&parent); - clearOnceInaccessible(&prev); -} - static void nr_arena_item_init (NRArenaItem *item) { diff --git a/src/display/nr-arena-item.h b/src/display/nr-arena-item.h index f43152ff9..737fed879 100644 --- a/src/display/nr-arena-item.h +++ b/src/display/nr-arena-item.h @@ -56,6 +56,7 @@ #include #include #include +#include "gc-soft-ptr.h" #include "nr-arena-forward.h" // My testing shows that disabling cache reduces the amount @@ -70,12 +71,10 @@ struct NRGC { }; struct NRArenaItem : public NRObject { - NRArenaItem(); - - NRArena *arena; - NRArenaItem *parent; + Inkscape::GC::soft_ptr arena; + Inkscape::GC::soft_ptr parent; NRArenaItem *next; - NRArenaItem *prev; + Inkscape::GC::soft_ptr prev; /* Item state */ unsigned int state : 16; diff --git a/src/gc-finalized.h b/src/gc-finalized.h index a31155653..046816b60 100644 --- a/src/gc-finalized.h +++ b/src/gc-finalized.h @@ -40,22 +40,17 @@ namespace GC { * * The best way to limit this effect is to only make "leaf" objects * (i.e. those that don't point to other finalizaable objects) - * finalizable, or if the object also derives from GC::Managed<>, - * use GC::Managed<>::clearOnceInaccessible to register those links - * to be cleared once the object is made inacecssible (and before it's - * finalized). + * finalizable, and otherwise use GC::soft_ptr<> instead of a regular + * pointer for "backreferences" (e.g. parent pointers in a tree + * structure), so that those references can be cleared to break any + * finalization cycles. * - * In a tree structure that has parent links and finalized nodes, - * you will almost always want to do this with the parent links - * if you can't avoid having them. - * - * @see Inkscape::GC::Managed<>::clearOnceInaccessible - * @see Inkscape::GC::Managed<>::cancelClearOnceInacessible + * @see Inkscape::GC::soft_ptr<> * * 2. Because there is no guarantee when the collector will destroy * objects, there is no guarantee when the destructor will get called. * - * It may not get called until the very end of the program, or never. + * It may not get called until the very end of the program, or ever. * * 3. If allocated in arrays, only the first object in the array will * have its destructor called, unless you make other arrangements by diff --git a/src/gc-managed.h b/src/gc-managed.h index fc13b2513..80d9c9411 100644 --- a/src/gc-managed.h +++ b/src/gc-managed.h @@ -28,26 +28,6 @@ template class Managed { public: - /** @brief Registers a pointer to be cleared when this object becomes - * inaccessible. - */ - template - void clearOnceInaccessible(T **p_ptr) { - Core::general_register_disappearing_link( - reinterpret_cast(p_ptr), Core::base(this) - ); - } - - /** @brief Cancels the registration of a pointer, so it will not be - * cleared when this object becomes inacessible. - */ - template - void cancelClearOnceInaccessible(T **p_ptr) { - Core::unregister_disappearing_link( - reinterpret_cast(p_ptr) - ); - } - void *operator new(std::size_t size, ScanPolicy scan=default_scan, CollectionPolicy collect=default_collect) diff --git a/src/gc-soft-ptr.h b/src/gc-soft-ptr.h new file mode 100644 index 000000000..a055ba38c --- /dev/null +++ b/src/gc-soft-ptr.h @@ -0,0 +1,74 @@ +/** \file + * Inkscape::GC::soft_ptr - "soft" pointers to avoid finalization cycles + * + * Copyright 2006 MenTaLguY + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * See the file COPYING for details. + * + */ + +#ifndef SEEN_INKSCAPE_GC_SOFT_PTR_H +#define SEEN_INKSCAPE_GC_SOFT_PTR_H + +#include "gc-core.h" + +namespace Inkscape { + +namespace GC { + +/** @brief A class for pointers which can be automatically cleared to break + * finalization cycles. + */ +template +class soft_ptr { +public: + soft_ptr(T * const &pointer=NULL) : _pointer(pointer) { + _register(); + } + soft_ptr(soft_ptr const &other) : _pointer(other._pointer) { + _register(); + } + + operator T *() const { return _pointer; } + T &operator*() const { return *_pointer; } + T *operator->() const { return _pointer; } + T &operator[](int i) const { return _pointer[i]; } + + soft_ptr &operator=(T * const &pointer) { + _pointer = pointer; + return *this; + } + + // default copy + +private: + void _register() { + void *base=Core::base(this); + if (base) { + Core::general_register_disappearing_link((void **)&_pointer, base); + } + } + + T *_pointer; +}; + +} + +} + +#endif +/* + 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 : diff --git a/src/selection.cpp b/src/selection.cpp index c40b7b546..09f6a18e3 100644 --- a/src/selection.cpp +++ b/src/selection.cpp @@ -39,7 +39,6 @@ Selection::Selection(SPDesktop *desktop) : _flags(0), _idle(0) { - clearOnceInaccessible(&_desktop); } Selection::~Selection() { diff --git a/src/selection.h b/src/selection.h index 407854406..3c2ec5c32 100644 --- a/src/selection.h +++ b/src/selection.h @@ -25,6 +25,7 @@ #include "gc-managed.h" #include "gc-finalized.h" #include "gc-anchored.h" +#include "gc-soft-ptr.h" #include "util/list.h" class SPItem; @@ -324,7 +325,7 @@ private: mutable GSList *_reprs; mutable GSList *_items; - SPDesktop *_desktop; + GC::soft_ptr _desktop; guint _flags; guint _idle; -- 2.30.2