aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorMike Pall <mike>2011-02-28 16:48:13 +0100
committerMike Pall <mike>2011-02-28 16:48:13 +0100
commit83a37aeca74724ef76dee7c8246bdbb88132940d (patch)
tree9dca0cd0aa13cf911ad26c3655533a72bb6790a9 /doc
parentcead25f928ac606fc1a13882b818913aab3635a9 (diff)
downloadluajit-83a37aeca74724ef76dee7c8246bdbb88132940d.tar.gz
luajit-83a37aeca74724ef76dee7c8246bdbb88132940d.tar.bz2
luajit-83a37aeca74724ef76dee7c8246bdbb88132940d.zip
FFI: Add ffi.gc() function for finalization of cdata objects.
Diffstat (limited to 'doc')
-rw-r--r--doc/ext_ffi_api.html28
1 files changed, 28 insertions, 0 deletions
diff --git a/doc/ext_ffi_api.html b/doc/ext_ffi_api.html
index b8c52fb6..5bd4b80c 100644
--- a/doc/ext_ffi_api.html
+++ b/doc/ext_ffi_api.html
@@ -238,6 +238,34 @@ This functions is mainly useful to override the pointer compatibility
238checks or to convert pointers to addresses or vice versa. 238checks or to convert pointers to addresses or vice versa.
239</p> 239</p>
240 240
241<h3 id="ffi_gc"><tt>cdata = ffi.gc(cdata, finalizer)</tt></h3>
242<p>
243Associates a finalizer with a pointer or aggregate cdata object. The
244cdata object is returned unchanged.
245</p>
246<p>
247This function allows safe integration of unmanaged resources into the
248automatic memory management of the LuaJIT garbage collector. Typical
249usage:
250</p>
251<pre class="code">
252local p = ffi.gc(ffi.C.malloc(n), ffi.C.free)
253...
254p = nil -- Last reference to p is gone.
255-- GC will eventually run finalizer: ffi.C.free(p)
256</pre>
257<p>
258A cdata finalizer works like the <tt>__gc</tt> metamethod for userdata
259objects: when the last reference to a cdata object is gone, the
260associated finalizer is called with the cdata object as an argument. The
261finalizer can be a Lua function or a cdata function or cdata function
262pointer. An existing finalizer can be removed by setting a <tt>nil</tt>
263finalizer, e.g. right before explicitly deleting a resource:
264</p>
265<pre class="code">
266ffi.C.free(ffi.gc(p, nil)) -- Manually free the memory.
267</pre>
268
241<h2 id="info">C&nbsp;Type Information</h2> 269<h2 id="info">C&nbsp;Type Information</h2>
242<p> 270<p>
243The following API functions return information about C&nbsp;types. 271The following API functions return information about C&nbsp;types.