summaryrefslogtreecommitdiff
path: root/utils
diff options
context:
space:
mode:
authorchatty1994-07-06 10:59:37 +0000
committerchatty1994-07-06 10:59:37 +0000
commitba67eaab578e52af90f5284fc701db086879b867 (patch)
treef4b9d051fecaa6b6245ded9dc021022aad21b031 /utils
parenta9c02e6c84692d85cc0c90b32161885834940be0 (diff)
downloadivy-league-ba67eaab578e52af90f5284fc701db086879b867.zip
ivy-league-ba67eaab578e52af90f5284fc701db086879b867.tar.gz
ivy-league-ba67eaab578e52af90f5284fc701db086879b867.tar.bz2
ivy-league-ba67eaab578e52af90f5284fc701db086879b867.tar.xz
----State -> State -= 2
New technique for finding dynamic objects
Diffstat (limited to 'utils')
-rw-r--r--utils/SmartPointer.cc44
1 files changed, 43 insertions, 1 deletions
diff --git a/utils/SmartPointer.cc b/utils/SmartPointer.cc
index 9775b49..d83dbbc 100644
--- a/utils/SmartPointer.cc
+++ b/utils/SmartPointer.cc
@@ -17,9 +17,14 @@
#include <stdio.h>
#include "SmartPointer.h"
+#include "List.h"
CcuSmartData::check_type CcuSmartData::check = doWarn;
+#ifdef OLD
int CcuSmartData::NextCreatedIsDynamic = 0;
+#else
+CcuList* CcuSmartData::LastDynamics;
+#endif
/*?class CcuSmartData
The class \typ{CcuSmartData} is the base class for objects that you want to reference through smart pointers.
@@ -43,20 +48,47 @@ want to define your own \fun{new} and \fun{delete} in derived classes. If you do
should take care that \fun{new} sets the flag \var{CcuSmartData::NextCreatedIsDynamic} to a non-null value.
?*/
+/*!
+One big problem with smart pointers lies in making a distinction between
+objects created in the stack (that we should not delete) and objects created
+dynamically, that we should delete when no more referenced. There's no way
+to make that distinction in constructors, sowe try to use operator new. This
+works well only if the constructor is called right after operator new, which is not
+always the case...
+!*/
void*
CcuSmartData :: operator new (unsigned long size)
{
+#ifdef OLD
NextCreatedIsDynamic = 1;
return ::operator new (size);
+#else
+ int sz = (int) size;
+ void* p = ::operator new (sz);
+ if (!LastDynamics)
+ LastDynamics = new CcuList;
+ LastDynamics->Prepend (p);
+ return p;
+#endif
+
}
/*?
Create a data, ie. initialize its reference count to 0.
?*/
CcuSmartData :: CcuSmartData ()
+#ifdef OLD
: State (NextCreatedIsDynamic ? 0 : 1)
+#else
+: State (1)
+#endif
{
+#ifdef OLD
NextCreatedIsDynamic = 0;
+#else
+ if (LastDynamics && LastDynamics->Remove (this))
+ State = 0;
+#endif
}
/*?
@@ -66,9 +98,18 @@ all derived class will have a copy constructor defined implicitly,
unless you specify one explicitely.
?*/
CcuSmartData :: CcuSmartData (const CcuSmartData&)
+#ifdef OLD
: State (NextCreatedIsDynamic ? 0 : 1)
+#else
+: State (1)
+#endif
{
+#ifdef OLD
NextCreatedIsDynamic = 0;
+#else
+ if (LastDynamics && LastDynamics->Remove (this))
+ State = 0;
+#endif
}
/*?
@@ -99,7 +140,8 @@ CcuSmartData :: ~CcuSmartData ()
void
CcuSmartData :: DecrRef ()
{
- if (----State == 0)
+ State -=2;
+ if (State == 0)
delete this;
}