diff options
| author | MenTaLguY <mental@rydia.net> | 2006-03-15 04:07:41 +0000 |
|---|---|---|
| committer | mental <mental@users.sourceforge.net> | 2006-03-15 04:07:41 +0000 |
| commit | f6f201213db27d2aae22264c4d7770884b8c6ea9 (patch) | |
| tree | fab3322ab8affa723d736ba9bc1ac669d658fb43 /src | |
| parent | shared_ptr -> ptr_shared (diff) | |
| download | inkscape-f6f201213db27d2aae22264c4d7770884b8c6ea9.tar.gz inkscape-f6f201213db27d2aae22264c4d7770884b8c6ea9.zip | |
Replace GC::Managed<>::clearOnceInaccessible with GC::soft_ptr<>
(bzr r241)
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile_insert | 1 | ||||
| -rw-r--r-- | src/display/nr-arena-item.cpp | 7 | ||||
| -rw-r--r-- | src/display/nr-arena-item.h | 9 | ||||
| -rw-r--r-- | src/gc-finalized.h | 17 | ||||
| -rw-r--r-- | src/gc-managed.h | 20 | ||||
| -rw-r--r-- | src/gc-soft-ptr.h | 74 | ||||
| -rw-r--r-- | src/selection.cpp | 1 | ||||
| -rw-r--r-- | src/selection.h | 3 |
8 files changed, 87 insertions, 45 deletions
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::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 <libnr/nr-rect-l.h> #include <libnr/nr-pixblock.h> #include <libnr/nr-object.h> +#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<NRArena> arena; + Inkscape::GC::soft_ptr<NRArenaItem> parent; NRArenaItem *next; - NRArenaItem *prev; + Inkscape::GC::soft_ptr<NRArenaItem> 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 <ScanPolicy default_scan=SCANNED, CollectionPolicy default_collect=AUTO> class Managed { public: - /** @brief Registers a pointer to be cleared when this object becomes - * inaccessible. - */ - template <typename T> - void clearOnceInaccessible(T **p_ptr) { - Core::general_register_disappearing_link( - reinterpret_cast<void **>(p_ptr), Core::base(this) - ); - } - - /** @brief Cancels the registration of a pointer, so it will not be - * cleared when this object becomes inacessible. - */ - template <typename T> - void cancelClearOnceInaccessible(T **p_ptr) { - Core::unregister_disappearing_link( - reinterpret_cast<void **>(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 <mental@rydia.net> + * + * 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 <typename T> +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<SPDesktop> _desktop; guint _flags; guint _idle; |
