| 1 |
<html><head><title>XLISP *gc-hook*</title> |
| 2 |
|
| 3 |
<link rel="stylesheet" type="text/css" href="reference.css"> |
| 4 |
|
| 5 |
</head> |
| 6 |
|
| 7 |
<body> |
| 8 |
|
| 9 |
<a href="../start.htm">Nyquist / XLISP 2.0</a> - |
| 10 |
<a href="../manual/contents.htm">Contents</a> | |
| 11 |
<a href="../tutorials/tutorials.htm">Tutorials</a> | |
| 12 |
<a href="../examples/examples.htm">Examples</a> | |
| 13 |
<a href="reference-index.htm">Reference</a> |
| 14 |
|
| 15 |
<hr> |
| 16 |
|
| 17 |
<h1>*gc-hook*</h1> |
| 18 |
|
| 19 |
<hr> |
| 20 |
|
| 21 |
<p><table cellpadding="0" cellspacing="0" style="margin-left:10px"><tbody> |
| 22 |
<tr valign="top"> |
| 23 |
<td><nobr>Type:</nobr></td> |
| 24 |
<td><nobr> - </nobr></td> |
| 25 |
<td width="100%"><nobr>system variable</nobr></td> |
| 26 |
</tr> |
| 27 |
<tr valign="top"> |
| 28 |
<td><nobr>Source:</nobr></td> |
| 29 |
<td><nobr> - </nobr></td> |
| 30 |
<td width="100%"><nobr>xldmem.c</nobr></td> |
| 31 |
</tr> |
| 32 |
</tbody></table></p> |
| 33 |
|
| 34 |
<h2>Syntax</h2> |
| 35 |
|
| 36 |
<dl> |
| 37 |
<dt> *gc-hook*</dt> |
| 38 |
</dl> |
| 39 |
|
| 40 |
<h2>Description</h2> |
| 41 |
|
| 42 |
<p>*gc-hook* is a system variable that allows a user function to be |
| 43 |
performed everytime garbage is collected [either explicitly with |
| 44 |
<a href="gc.htm">gc</a> or automatically]. The default value for |
| 45 |
*gc-hook* is <a href="nil.htm">NIL</a>. When *gc-hook* is set to |
| 46 |
a non-<a href="nil.htm">NIL</a> symbol, it is enabled to execute |
| 47 |
the specified user routine. The user routine can be a quoted symbol or a |
| 48 |
closure. There are two parameters to the user routine, the total number of |
| 49 |
nodes and current free nodes after the garbage collection.</p> |
| 50 |
|
| 51 |
<h2>Examples</h2> |
| 52 |
|
| 53 |
<pre class="example"> |
| 54 |
*gc-hook* <font color="#008844">; returns NIL</font> |
| 55 |
(gc) <font color="#008844">; returns NIL</font> |
| 56 |
|
| 57 |
(defun mygchook (&rest stuff) <font color="#008844">; define the hook</font> |
| 58 |
(print stuff) |
| 59 |
(print "my hook")) |
| 60 |
|
| 61 |
(setq *gc-hook* 'mygchook) <font color="#008844">; set up *GC-HOOK*</font> |
| 62 |
|
| 63 |
(gc) <font color="#008844">; prints (2640 232)</font> |
| 64 |
<font color="#008844">; "my hook"</font> |
| 65 |
<font color="#008844">; returns NIL</font> |
| 66 |
|
| 67 |
(setq *gc-flag* T) <font color="#008844">; turn on the system GC message</font> |
| 68 |
|
| 69 |
(gc) <font color="#008844">; prints</font> |
| 70 |
<font color="#008844">; [ gc: total 2640, (2640 241)</font> |
| 71 |
<font color="#008844">; "my hook"</font> |
| 72 |
<font color="#008844">; 236 free ]</font> |
| 73 |
<font color="#008844">; returns NIL</font> |
| 74 |
|
| 75 |
(setq *gc-flag* NIL) <font color="#008844">; turn off GC message</font> |
| 76 |
|
| 77 |
(setq *gc-hook* (lambda (x y) <font color="#008844">; enable user routine</font> |
| 78 |
(princ "\007"))) <font color="#008844">; that beeps at every GC</font> |
| 79 |
|
| 80 |
(gc) <font color="#008844">; beeps</font> |
| 81 |
|
| 82 |
(defun expand-on-gc (total free) <font color="#008844">; define EXPAND-ON-GC</font> |
| 83 |
(if (< (/ free 1.0 total) .1) <font color="#008844">; IF free/total < .10</font> |
| 84 |
(progn (expand 2) <font color="#008844">; THEN expand memory</font> |
| 85 |
(princ "\007")))) <font color="#008844">; and beep</font> |
| 86 |
|
| 87 |
<font color="#008844">; NOTE: XLISP already gets more nodes</font> |
| 88 |
<font color="#008844">; automatically, this is just an example.</font> |
| 89 |
|
| 90 |
(setq *gc-hook* 'expand-on-gc) <font color="#008844">; enable EXPAND-ON-GC</font> |
| 91 |
(gc) <font color="#008844">; beeps when low on nodes</font> |
| 92 |
</pre> |
| 93 |
|
| 94 |
<p><b>Note:</b> The *gc-hook* and <a href="global-gc-flag.htm">*gc-flag*</a> |
| 95 |
facilities can interact. If you do printing in the *gc-hook* user form and |
| 96 |
enable <nobr><a href="global-gc-flag.htm">*gc-flag*</a> ,</nobr> the |
| 97 |
*gc-hook* printing will come out in the middle of the |
| 98 |
<a href="global-gc-flag.htm">*gc-flag*</a> message.</p> |
| 99 |
|
| 100 |
<p><b>Note:</b> The *gc-hook* user form is evaluated after the execution of |
| 101 |
the actual garbage collection code. This means that if the user form causes |
| 102 |
an error, it does not prevent a garbage collection.</p> |
| 103 |
|
| 104 |
<p><b>Note:</b> Since *gc-hook* is set to a symbol, the user defined form |
| 105 |
can be changed by doing another <a href="defun.htm">defun</a> [or |
| 106 |
whatever] to the symbol in *gc-hook*. Note also that you should define the |
| 107 |
symbol first and then set *gc-hook* to the symbol. If you don't, an |
| 108 |
automatic garbage collection might occur before you set *gc-hook*, |
| 109 |
generating an error and stopping your program.</p> |
| 110 |
|
| 111 |
<p>See the |
| 112 |
<a href="../manual/xlisp-man-011.htm#gc-hook">*gc-hook*</a> |
| 113 |
system variable in the <nobr>XLISP 2.0</nobr> manual.</p> |
| 114 |
|
| 115 |
<p><nobr> <a href="#top">Back to Top</nobr></a></p> |
| 116 |
|
| 117 |
<hr> |
| 118 |
|
| 119 |
<a href="../start.htm">Nyquist / XLISP 2.0</a> - |
| 120 |
<a href="../manual/contents.htm">Contents</a> | |
| 121 |
<a href="../tutorials/tutorials.htm">Tutorials</a> | |
| 122 |
<a href="../examples/examples.htm">Examples</a> | |
| 123 |
<a href="reference-index.htm">Reference</a> |
| 124 |
|
| 125 |
</body></html> |