/xbmc/visualizations/Vortex/angelscript/docs/manual/doc_gc_object.html
http://github.com/xbmc/xbmc · HTML · 100 lines · 92 code · 7 blank · 1 comment · 0 complexity · e42e3b26287bc2ec0ab8da54337a99d9 MD5 · raw file
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
- <title>AngelScript: Garbage collected objects</title>
- <link href="tabs.css" rel="stylesheet" type="text/css">
- <link href="doxygen.css" rel="stylesheet" type="text/css">
- </head><body>
- <!-- Generated by Doxygen 1.5.9 -->
- <div class="contents">
- <h1><a class="anchor" name="doc_gc_object">Garbage collected objects </a></h1>Reference counting as memory management has a drawback in that it is difficult to detect circular references when determining dead objects. AngelScript allows the application to register types with special behaviours to support the garbage collection for detecting circular references. These behaviours make the class a bit more complex, but you should only have to register them for a few types, e.g. generic container classes.<p>
- <div class="fragment"><pre class="fragment"><span class="comment">// Registering the garbage collected reference type</span>
- r = engine-><a class="code" href="classas_i_script_engine.html#29c6c087c8c5b5cdb6271cfd161cc5a6" title="Registers a new object type.">RegisterObjectType</a>(<span class="stringliteral">"ref"</span>, 0, <a class="code" href="angelscript_8h.html#855d86fa9ee15b9f75e553ee376b5c7a9450e038342b36c745858d2e5ae4b861" title="A reference type.">asOBJ_REF</a> | <a class="code" href="angelscript_8h.html#855d86fa9ee15b9f75e553ee376b5c7acc1d835f9c25043cef86026a4aa6a470" title="A garbage collected type. Only valid for reference types.">asOBJ_GC</a>); assert( r >= 0 );
- </pre></div><p>
- The difference between the garbage collected and non-garbage collected types is in the addref and release behaviours, the class constructor, and the extra support behaviours.<p>
- <dl class="see" compact><dt><b>See also:</b></dt><dd>The <a class="el" href="doc_addon_dict.html">dictionary</a> add-on for an example of a garbage collected object</dd></dl>
- <h2><a class="anchor" name="doc_reg_gcref_1">
- GC support behaviours</a></h2>
- The GC determines when objects should be destroyed by counting the references it can follow for each object. If the GC can see all references that points to an object, it knows that the object is part of a circular reference. If all the objects involved in that circular reference have no outside references it means that they should be destroyed.<p>
- The process of determining the dead objects uses the first for of the behaviours below, while the destruction of the objects is done by forcing the release of the object's references.<p>
- <div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> CGCRef::SetGCFlag()
- {
- <span class="comment">// Set the gc flag as the high bit in the reference counter</span>
- refCount |= 0x80000000;
- }
- <span class="keywordtype">bool</span> CGCRef::GetGCFlag()
- {
- <span class="comment">// Return the gc flag</span>
- <span class="keywordflow">return</span> (refCount & 0x80000000) ? <span class="keyword">true</span> : <span class="keyword">false</span>;
- }
- <span class="keywordtype">int</span> CGCRef::GetRefCount()
- {
- <span class="comment">// Return the reference count, without the gc flag</span>
- <span class="keywordflow">return</span> (refCount & 0x7FFFFFFF);
- }
- <span class="keywordtype">void</span> CGCRef::EnumReferences()
- {
- <span class="comment">// Call the engine::GCEnumCallback for all references to other objects held</span>
- engine-><a class="code" href="classas_i_script_engine.html#58ceeafd780dea3543e0ede4106199fd" title="Used by the garbage collector to enumerate all references held by an object.">GCEnumCallback</a>(myref);
- }
- <span class="keywordtype">void</span> CGCRef::ReleaseAllReferences()
- {
- <span class="comment">// When we receive this call, we are as good as dead, but</span>
- <span class="comment">// the garbage collector will still hold a references to us, so we</span>
- <span class="comment">// cannot just delete ourself yet. Just free all references to other</span>
- <span class="comment">// objects that we hold</span>
- <span class="keywordflow">if</span>( myref )
- {
- myref->Release();
- myref = 0;
- }
- }
- <span class="comment">// Register the GC support behaviours</span>
- r = engine-><a class="code" href="classas_i_script_engine.html#7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">"gc"</span>, <a class="code" href="angelscript_8h.html#7e38df5b10ec8cbf2a688f1d114097c5adbad474a338c3a0fe6e90df679bb2e6" title="(GC) Set GC flag">asBEHAVE_SETGCFLAG</a>, <span class="stringliteral">"void f()"</span>, <a class="code" href="angelscript_8h.html#7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method...">asMETHOD</a>(CGCRef,SetGCFlag), <a class="code" href="angelscript_8h.html#3ec92ea3c4762e44c2df788ceccdd1e4ea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r >= 0 );
- r = engine-><a class="code" href="classas_i_script_engine.html#7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">"gc"</span>, <a class="code" href="angelscript_8h.html#7e38df5b10ec8cbf2a688f1d114097c5bfce2539609e667f15b24bbc8551c7b7" title="(GC) Get GC flag">asBEHAVE_GETGCFLAG</a>, <span class="stringliteral">"bool f()"</span>, <a class="code" href="angelscript_8h.html#7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method...">asMETHOD</a>(CGCRef,GetGCFlag), <a class="code" href="angelscript_8h.html#3ec92ea3c4762e44c2df788ceccdd1e4ea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r >= 0 );
- r = engine-><a class="code" href="classas_i_script_engine.html#7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">"gc"</span>, <a class="code" href="angelscript_8h.html#7e38df5b10ec8cbf2a688f1d114097c5f998529f8ea1e54567997b8fb2867640" title="(GC) Get reference count">asBEHAVE_GETREFCOUNT</a>, <span class="stringliteral">"int f()"</span>, <a class="code" href="angelscript_8h.html#7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method...">asMETHOD</a>(CGCRef,GetRefCount), <a class="code" href="angelscript_8h.html#3ec92ea3c4762e44c2df788ceccdd1e4ea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r >= 0 );
- r = engine-><a class="code" href="classas_i_script_engine.html#7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">"gc"</span>, <a class="code" href="angelscript_8h.html#7e38df5b10ec8cbf2a688f1d114097c508ccf78a37567b5dd192ff5d95c6667b" title="(GC) Enumerate held references">asBEHAVE_ENUMREFS</a>, <span class="stringliteral">"void f(int&in)"</span>, <a class="code" href="angelscript_8h.html#7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method...">asMETHOD</a>(CGCRef,EnumReferences), <a class="code" href="angelscript_8h.html#3ec92ea3c4762e44c2df788ceccdd1e4ea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r >= 0 );
- r = engine-><a class="code" href="classas_i_script_engine.html#7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">"gc"</span>, <a class="code" href="angelscript_8h.html#7e38df5b10ec8cbf2a688f1d114097c54275ebe0b4852f2d4a10d4d9db333fe9" title="(GC) Release all references">asBEHAVE_RELEASEREFS</a>, <span class="stringliteral">"void f(int&in)"</span>, <a class="code" href="angelscript_8h.html#7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method...">asMETHOD</a>(CGCRef,ReleaseAllReferences), <a class="code" href="angelscript_8h.html#3ec92ea3c4762e44c2df788ceccdd1e4ea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r >= 0 );
- </pre></div><h2><a class="anchor" name="doc_reg_gcref_2">
- Factory for garbage collection</a></h2>
- Whenever a garbage collected class is created, the garbage collector must be notified of it's existence. The easiest way of doing that is to have the factory behaviour, or the class constructor call the <code>NotifyGarbageCollectorOfNewObject()</code> method on the engine when initializing the class.<p>
- <div class="fragment"><pre class="fragment">CGCRef *GCRef_Factory()
- {
- <span class="comment">// Create the object and then notify the GC of its existence</span>
- CGCRef *obj = <span class="keyword">new</span> CGCRef();
- <span class="keywordtype">int</span> typeId = engine-><a class="code" href="classas_i_script_engine.html#1e8dde38773b0e0d96811f1316964cb0" title="Returns a type id by declaration.">GetTypeIdByDecl</a>(<span class="stringliteral">"gc"</span>);
- engine-><a class="code" href="classas_i_script_engine.html#8ea9a6f3ed9f69a5c4e07c87281117c0" title="Notify the garbage collector of a new object that needs to be managed.">NotifyGarbageCollectorOfNewObject</a>(obj, typeId);
- <span class="keywordflow">return</span> obj;
- }
- </pre></div><p>
- You may want to consider caching the typeId, so that it doesn't have to be looked up through the relatively expensive call to GetTypeIdByDecl every time an object of this type is created.<p>
- Note, if you create objects of this type from the application side, you must also notify the garbage collector of its existence, so it's a good idea to make sure all code use the same way of creating objects of this type.<h2><a class="anchor" name="doc_reg_gcref_3">
- Addref and release for garbage collection</a></h2>
- For garbage collected objects it is important to make sure the AddRef and Release behaviours clear the GC flag. Otherwise it is possible that the GC incorrectly determine that the object should be destroyed.<p>
- <div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> CGCRef::AddRef()
- {
- <span class="comment">// Clear the gc flag and increase the reference counter</span>
- refCount = (refCount&0x7FFFFFFF) + 1;
- }
- <span class="keywordtype">void</span> CGCRef::Release()
- {
- <span class="comment">// Clear the gc flag, decrease ref count and delete if it reaches 0</span>
- refCount &= 0x7FFFFFFF;
- <span class="keywordflow">if</span>( --refCount == 0 )
- <span class="keyword">delete</span> <span class="keyword">this</span>;
- }
- <span class="comment">// Registering the addref/release behaviours</span>
- r = engine-><a class="code" href="classas_i_script_engine.html#7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">"gc"</span>, <a class="code" href="angelscript_8h.html#7e38df5b10ec8cbf2a688f1d114097c51dfa5b72ad69a7bf70636d4fcb1b1d84" title="AddRef.">asBEHAVE_ADDREF</a>, <span class="stringliteral">"void f()"</span>, <a class="code" href="angelscript_8h.html#7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method...">asMETHOD</a>(CGCRef,AddRef), <a class="code" href="angelscript_8h.html#3ec92ea3c4762e44c2df788ceccdd1e4ea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r >= 0 );
- r = engine-><a class="code" href="classas_i_script_engine.html#7ea3c93dea338b0287027de0e4895dcb" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">"gc"</span>, <a class="code" href="angelscript_8h.html#7e38df5b10ec8cbf2a688f1d114097c57134ce13c81967191af401a1e5170a0c" title="Release.">asBEHAVE_RELEASE</a>, <span class="stringliteral">"void f()"</span>, <a class="code" href="angelscript_8h.html#7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method...">asMETHOD</a>(CGCRef,Release), <a class="code" href="angelscript_8h.html#3ec92ea3c4762e44c2df788ceccdd1e4ea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r >= 0 );
- </pre></div> </div>
- <hr size="1"><address style="text-align: right;"><small>Generated on Wed Dec 16 19:34:50 2009 for AngelScript by
- <a href="http://www.doxygen.org/index.html">
- <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.9 </small></address>
- </body>
- </html>