diff options
| author | Mike Pall <mike> | 2011-02-28 16:48:13 +0100 |
|---|---|---|
| committer | Mike Pall <mike> | 2011-02-28 16:48:13 +0100 |
| commit | 83a37aeca74724ef76dee7c8246bdbb88132940d (patch) | |
| tree | 9dca0cd0aa13cf911ad26c3655533a72bb6790a9 /doc/ext_ffi_api.html | |
| parent | cead25f928ac606fc1a13882b818913aab3635a9 (diff) | |
| download | luajit-83a37aeca74724ef76dee7c8246bdbb88132940d.tar.gz luajit-83a37aeca74724ef76dee7c8246bdbb88132940d.tar.bz2 luajit-83a37aeca74724ef76dee7c8246bdbb88132940d.zip | |
FFI: Add ffi.gc() function for finalization of cdata objects.
Diffstat (limited to 'doc/ext_ffi_api.html')
| -rw-r--r-- | doc/ext_ffi_api.html | 28 |
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 | |||
| 238 | checks or to convert pointers to addresses or vice versa. | 238 | checks 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> | ||
| 243 | Associates a finalizer with a pointer or aggregate cdata object. The | ||
| 244 | cdata object is returned unchanged. | ||
| 245 | </p> | ||
| 246 | <p> | ||
| 247 | This function allows safe integration of unmanaged resources into the | ||
| 248 | automatic memory management of the LuaJIT garbage collector. Typical | ||
| 249 | usage: | ||
| 250 | </p> | ||
| 251 | <pre class="code"> | ||
| 252 | local p = ffi.gc(ffi.C.malloc(n), ffi.C.free) | ||
| 253 | ... | ||
| 254 | p = nil -- Last reference to p is gone. | ||
| 255 | -- GC will eventually run finalizer: ffi.C.free(p) | ||
| 256 | </pre> | ||
| 257 | <p> | ||
| 258 | A cdata finalizer works like the <tt>__gc</tt> metamethod for userdata | ||
| 259 | objects: when the last reference to a cdata object is gone, the | ||
| 260 | associated finalizer is called with the cdata object as an argument. The | ||
| 261 | finalizer can be a Lua function or a cdata function or cdata function | ||
| 262 | pointer. An existing finalizer can be removed by setting a <tt>nil</tt> | ||
| 263 | finalizer, e.g. right before explicitly deleting a resource: | ||
| 264 | </p> | ||
| 265 | <pre class="code"> | ||
| 266 | ffi.C.free(ffi.gc(p, nil)) -- Manually free the memory. | ||
| 267 | </pre> | ||
| 268 | |||
| 241 | <h2 id="info">C Type Information</h2> | 269 | <h2 id="info">C Type Information</h2> |
| 242 | <p> | 270 | <p> |
| 243 | The following API functions return information about C types. | 271 | The following API functions return information about C types. |
