diff options
| author | Philipp Stephani | 2018-02-11 21:38:22 +0100 |
|---|---|---|
| committer | Philipp Stephani | 2020-01-03 19:24:10 +0100 |
| commit | 48ffef5ef4b34799941a033591ea827d40025939 (patch) | |
| tree | 67b00c1bc546f3c9ef601c10db634da3094f7f57 /doc | |
| parent | 2b6d702e5d2d572640c6bcd43f54138bacbe7ac8 (diff) | |
| download | emacs-48ffef5ef4b34799941a033591ea827d40025939.tar.gz emacs-48ffef5ef4b34799941a033591ea827d40025939.zip | |
Implement finalizers for module functions (Bug#30373)
* src/module-env-28.h: Add new module environment functions to
module environment for Emacs 28.
* src/emacs-module.h.in: Document that 'emacs_finalizer' also works
for function finalizers.
* src/emacs-module.c (CHECK_MODULE_FUNCTION): New function.
(struct Lisp_Module_Function): Add finalizer data member.
(module_make_function): Initialize finalizer.
(module_get_function_finalizer)
(module_set_function_finalizer): New module environment functions.
(module_finalize_function): New function.
(initialize_environment): Initialize new environment functions.
* src/alloc.c (cleanup_vector): Call potential module function
finalizer during garbage collection.
* test/data/emacs-module/mod-test.c (signal_error): New helper
function.
(memory_full): Use it.
(finalizer): New example function finalizer.
(Fmod_test_make_function_with_finalizer)
(Fmod_test_function_finalizer_calls): New test module functions.
(emacs_module_init): Define them.
* test/src/emacs-module-tests.el (module/function-finalizer): New unit
test.
* doc/lispref/internals.texi (Module Functions): Document new
functionality.
(Module Misc): Move description of 'emacs_finalizer' type to 'Module
Functions' node, and add a reference to it.
* etc/NEWS: Mention new functionality.
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/lispref/internals.texi | 55 |
1 files changed, 50 insertions, 5 deletions
diff --git a/doc/lispref/internals.texi b/doc/lispref/internals.texi index d95a3e445cc..c0b3fe5a1b0 100644 --- a/doc/lispref/internals.texi +++ b/doc/lispref/internals.texi | |||
| @@ -1447,6 +1447,54 @@ The Lisp package which goes with your module could then load the | |||
| 1447 | module using the @code{load} primitive (@pxref{Dynamic Modules}) when | 1447 | module using the @code{load} primitive (@pxref{Dynamic Modules}) when |
| 1448 | the package is loaded into Emacs. | 1448 | the package is loaded into Emacs. |
| 1449 | 1449 | ||
| 1450 | @anchor{Module Function Finalizers} | ||
| 1451 | If you want to run some code when a module function object (i.e., an | ||
| 1452 | object returned by @code{make_function}) is garbage-collected, you can | ||
| 1453 | install a @dfn{function finalizer}. Function finalizers are available | ||
| 1454 | since Emacs 28. For example, if you have passed some heap-allocated | ||
| 1455 | structure to the @var{data} argument of @code{make_function}, you can | ||
| 1456 | use the finalizer to deallocate the structure. @xref{Basic | ||
| 1457 | Allocation,,,libc}, and @pxref{Freeing after Malloc,,,libc}. The | ||
| 1458 | finalizer function has the following signature: | ||
| 1459 | |||
| 1460 | @example | ||
| 1461 | void finalizer (void *@var{data}) | ||
| 1462 | @end example | ||
| 1463 | |||
| 1464 | Here, @var{data} receives the value passed to @var{data} when calling | ||
| 1465 | @code{make_function}. Note that the finalizer can't interact with | ||
| 1466 | Emacs in any way. | ||
| 1467 | |||
| 1468 | Directly after calling @code{make_function}, the newly-created | ||
| 1469 | function doesn't have a finalizer. Use @code{set_function_finalizer} | ||
| 1470 | to add one, if desired. | ||
| 1471 | |||
| 1472 | @deftypefun void emacs_finalizer (void *@var{ptr}) | ||
| 1473 | The header @file{emacs-module.h} provides the type | ||
| 1474 | @code{emacs_finalizer} as a type alias for an Emacs finalizer | ||
| 1475 | function. | ||
| 1476 | @end deftypefun | ||
| 1477 | |||
| 1478 | @deftypefun emacs_finalizer get_function_finalizer (emacs_env *@var{env}, emacs_value @var{arg}) | ||
| 1479 | This function, which is available since Emacs 28, returns the function | ||
| 1480 | finalizer associated with the module function represented by | ||
| 1481 | @var{arg}. @var{arg} must refer to a module function, that is, an | ||
| 1482 | object returned by @code{make_function}. If no finalizer is | ||
| 1483 | associated with the function, @code{NULL} is returned. | ||
| 1484 | @end deftypefun | ||
| 1485 | |||
| 1486 | @deftypefun void set_function_finalizer (emacs_env *@var{env}, emacs_value @var{arg}, emacs_finalizer @var{fin}) | ||
| 1487 | This function, which is available since Emacs 28, sets the function | ||
| 1488 | finalizer associated with the module function represented by @var{arg} | ||
| 1489 | to @var{fin}. @var{arg} must refer to a module function, that is, an | ||
| 1490 | object returned by @code{make_function}. @var{fin} can either be | ||
| 1491 | @code{NULL} to clear @var{arg}'s function finalizer, or a pointer to a | ||
| 1492 | function to be called when the object represented by @var{arg} is | ||
| 1493 | garbage-collected. At most one function finalizer can be set per | ||
| 1494 | function; if @var{arg} already has a finalizer, it is replaced by | ||
| 1495 | @var{fin}. | ||
| 1496 | @end deftypefun | ||
| 1497 | |||
| 1450 | @node Module Values | 1498 | @node Module Values |
| 1451 | @subsection Conversion Between Lisp and Module Values | 1499 | @subsection Conversion Between Lisp and Module Values |
| 1452 | @cindex module values, conversion | 1500 | @cindex module values, conversion |
| @@ -1865,11 +1913,8 @@ represented by @var{arg} to be @var{fin}. If @var{fin} is a | |||
| 1865 | finalizer. | 1913 | finalizer. |
| 1866 | @end deftypefn | 1914 | @end deftypefn |
| 1867 | 1915 | ||
| 1868 | @deftypefun void emacs_finalizer (void *@var{ptr}) | 1916 | Note that the @code{emacs_finalizer} type works for both user pointer |
| 1869 | The header @file{emacs-module.h} provides the type | 1917 | an module function finalizers. @xref{Module Function Finalizers}. |
| 1870 | @code{emacs_finalizer} as a type alias for an Emacs finalizer | ||
| 1871 | function. | ||
| 1872 | @end deftypefun | ||
| 1873 | 1918 | ||
| 1874 | @node Module Misc | 1919 | @node Module Misc |
| 1875 | @subsection Miscellaneous Convenience Functions for Modules | 1920 | @subsection Miscellaneous Convenience Functions for Modules |