WeakReferenceClass.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* Interpreter */
40 /* */
41 /* Weak reference support for Rexx memory management */
42 /* */
43 /******************************************************************************/
44 
45 #include "RexxCore.h"
46 #include "WeakReferenceClass.hpp"
47 #include "ProtectedObject.hpp"
48 
49 
50 RexxClass *WeakReference::classInstance = OREF_NULL; // singleton class instance
51 
53 /******************************************************************************/
54 /* Function: Create initial bootstrap objects */
55 /******************************************************************************/
56 {
57  CLASS_CREATE(WeakReference, "WeakReference", RexxClass);
58 }
59 
60 void *WeakReference::operator new(size_t size)
61 /******************************************************************************/
62 /* Function: Create a new pointer object */
63 /******************************************************************************/
64 {
65  /* Get new object */
66  // NB: We can't just mark this as having no references. There are operations
67  // where we need to have the marking routines called.
68  return new_object(size, T_WeakReference);
69 }
70 
71 
73 /******************************************************************************/
74 /* Function: Construct a non-notifying weak reference */
75 /******************************************************************************/
76 {
77  // NOTE: We do not use OrefSet here, since we don't want the referenced
78  // objects to be added to the old to new table.
79  referentObject = r;
80  // tell the memory manager that we exist
82 }
83 
84 
86 /******************************************************************************/
87 /* Function: Construct a non-notifying weak reference */
88 /******************************************************************************/
89 {
90  // NOTE: We do not use OrefSet here, since we don't want the referenced
91  // objects to be added to the old to new table.
93  // tell the memory manager that we exist
95 }
96 
97 
98 void WeakReference::live(size_t liveMark)
99 /******************************************************************************/
100 /* Function: Generalized object marking */
101 /******************************************************************************/
102 {
103  // we need to get called, but we don't do any marking of the referent.
104  // we do, however, need to mark the object variables in case this is a subclass.
105  memory_mark(this->objectVariables);
106 }
107 
108 
110 /******************************************************************************/
111 /* Function: Generalized object marking */
112 /******************************************************************************/
113 {
114  // this might be a subclass, so we need to mark the object variables always
115  memory_mark_general(this->objectVariables);
116  // these references are only marked during a save or restore image process.
117  // NOTE: WeakReference objects saved in the Rexx image get removed from the
118  // weak reference list and just become normal objects. Since the weak references
119  // can only contain references to other oldspace objects at that point, those
120  // objects will never get collected.
121 
122  if (reason == SAVINGIMAGE || reason == RESTORINGIMAGE)
123  {
125  }
126 }
127 
129 /******************************************************************************/
130 /* Function: Flatten an object */
131 /******************************************************************************/
132 {
134  // not normally needed, but this might be a subclass
135  flatten_reference(newThis->objectVariables, envelope);
136  flatten_reference(newThis->referentObject, envelope);
137 
138  // make sure the new version has nulled out list pointers
139  newThis->nextReferenceList = OREF_NULL;
140 
142 }
143 
145 /******************************************************************************/
146 /* Function: unflatten an object */
147 /******************************************************************************/
148 {
149  // if we still have a reference to handle, then add this to the
150  // tracking list
151  if (referentObject != OREF_NULL)
152  {
154  }
155  return (RexxObject *)this; /* return ourself. */
156 }
157 
159 /******************************************************************************/
160 /* Function: clear an object reference, and potentially move to notification */
161 /* queue so the notification object can be "tapped" */
162 /******************************************************************************/
163 {
164  // NOTE: We do not use OrefSet here, since we don't want the referenced
165  // objects to be added to the old to new table.
167 }
168 
169 
170 /**
171  * Get the value of the weak reference. If this has been cleared,
172  * it returns .nil. Otherwise, it returns the referenced object.
173  *
174  * @return The referenced object, or .nil if the object has been garbage
175  * collected.
176  */
178 {
179  if (referentObject == OREF_NULL)
180  {
181  return TheNilObject;
182  }
183  return referentObject;
184 }
185 
186 
187 RexxObject *WeakReference::newRexx(RexxObject **init_args, size_t argCount, size_t named_argCount)
188 /******************************************************************************/
189 /* Arguments: Subclass init arguments */
190 /* Function: Create a new string value (used primarily for subclasses) */
191 /******************************************************************************/
192 {
193  RexxObject *refObj; /* string value */
194 
195  /* break up the arguments */
196  RexxClass::processNewArgs(init_args, argCount, &init_args, &argCount, 1, &refObj, NULL);
197  // must have a value
198  requiredArgument(refObj, OREF_positional, ARG_ONE);
199  // create a new weakReference
200  RexxObject *newObj = new WeakReference(refObj);
201  ProtectedObject p(newObj);
202  // override the behaviour in case this is a subclass
203  newObj->setBehaviour(((RexxClass *)this)->getInstanceBehaviour());
204  if (((RexxClass *)this)->hasUninitDefined())
205  {
206  newObj->hasUninit();
207  }
208 
209  /* Initialize the new instance */
210  newObj->sendMessage(OREF_INIT, init_args, argCount, named_argCount);
211  return newObj; /* return the new instance */
212 }
@ T_WeakReference
#define OREF_NULL
Definition: RexxCore.h:60
#define TheNilObject
Definition: RexxCore.h:181
const int ARG_ONE
Definition: RexxCore.h:80
void requiredArgument(RexxObject *object, RexxString *kind, size_t position)
Definition: RexxCore.h:291
RexxMemory memoryObject
Definition: RexxMemory.cpp:85
#define memory_mark(oref)
Definition: RexxMemory.hpp:445
RexxObject * new_object(size_t s)
Definition: RexxMemory.hpp:431
#define flatten_reference(oref, envel)
Definition: RexxMemory.hpp:493
#define CLASS_CREATE(name, id, className)
Definition: RexxMemory.hpp:498
#define memory_mark_general(oref)
Definition: RexxMemory.hpp:446
@ SAVINGIMAGE
Definition: RexxMemory.hpp:117
@ RESTORINGIMAGE
Definition: RexxMemory.hpp:116
#define cleanUpFlatten
Definition: RexxMemory.hpp:479
#define setUpFlatten(type)
Definition: RexxMemory.hpp:473
static void processNewArgs(RexxObject **, size_t, RexxObject ***, size_t *, size_t, RexxObject **, RexxObject **)
void setBehaviour(RexxBehaviour *b)
void addWeakReference(WeakReference *ref)
Definition: RexxMemory.cpp:722
void sendMessage(RexxString *, RexxArray *, RexxDirectory *, ProtectedObject &)
static void createInstance()
RexxObject * newRexx(RexxObject **args, size_t argc, size_t named_argc)
RexxObject * referentObject
void flatten(RexxEnvelope *)
RexxObject * unflatten(RexxEnvelope *)
RexxObject * value()
static RexxClass * classInstance