<html lang="en"> <head> <title>Function Locking - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Function-Files.html#Function-Files" title="Function Files"> <link rel="prev" href="Overloading-and-Autoloading.html#Overloading-and-Autoloading" title="Overloading and Autoloading"> <link rel="next" href="Function-Precedence.html#Function-Precedence" title="Function Precedence"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Function-Locking"></a> <p> Next: <a rel="next" accesskey="n" href="Function-Precedence.html#Function-Precedence">Function Precedence</a>, Previous: <a rel="previous" accesskey="p" href="Overloading-and-Autoloading.html#Overloading-and-Autoloading">Overloading and Autoloading</a>, Up: <a rel="up" accesskey="u" href="Function-Files.html#Function-Files">Function Files</a> <hr> </div> <h4 class="subsection">11.8.5 Function Locking</h4> <p>It is sometime desirable to lock a function into memory with the <code>mlock</code> function. This is typically used for dynamically linked functions in Oct-files or mex-files that contain some initialization, and it is desirable that calling <code>clear</code> does not remove this initialization. <p>As an example, <pre class="example"> mlock ("my_function"); </pre> <p class="noindent">prevents <code>my_function</code> from being removed from memory, even if <code>clear</code> is called. It is possible to determine if a function is locked into memory with the <code>mislocked</code>, and to unlock a function with <code>munlock</code>, which the following illustrates. <pre class="example"> mlock ("my_function"); mislocked ("my_function") ⇒ ans = 1 munlock ("my_function"); mislocked ("my_function") ⇒ ans = 0 </pre> <p>A common use of <code>mlock</code> is to prevent persistent variables from being removed from memory, as the following example shows: <pre class="example"> function count_calls() persistent calls = 0; printf ("'count_calls' has been called %d times\n", ++calls); endfunction mlock ("count_calls"); count_calls (); -| 'count_calls' has been called 1 times clear count_calls count_calls (); -| 'count_calls' has been called 2 times </pre> <p class="noindent">It is, however, often inconvenient to lock a function from the prompt, so it is also possible to lock a function from within its body. This is simply done by calling <code>mlock</code> from within the function. <pre class="example"> function count_calls () mlock (); persistent calls = 0; printf ("'count_calls' has been called %d times\n", ++calls); endfunction </pre> <p><code>mlock</code> might equally be used to prevent changes to a function from having effect in Octave, though a similar effect can be had with the <code>ignore_function_time_stamp</code> function. <!-- mlock src/variables.cc --> <p><a name="doc_002dmlock"></a> <div class="defun"> — Built-in Function: <b>mlock</b> ()<var><a name="index-mlock-781"></a></var><br> <blockquote><p>Lock the current function into memory so that it can't be cleared. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmunlock.html#doc_002dmunlock">munlock</a>, <a href="doc_002dmislocked.html#doc_002dmislocked">mislocked</a>, <a href="doc_002dpersistent.html#doc_002dpersistent">persistent</a>. </p></blockquote></div> <!-- munlock src/variables.cc --> <p><a name="doc_002dmunlock"></a> <div class="defun"> — Built-in Function: <b>munlock</b> ()<var><a name="index-munlock-782"></a></var><br> — Built-in Function: <b>munlock</b> (<var>fcn</var>)<var><a name="index-munlock-783"></a></var><br> <blockquote><p>Unlock the named function <var>fcn</var>. If no function is named then unlock the current function. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmlock.html#doc_002dmlock">mlock</a>, <a href="doc_002dmislocked.html#doc_002dmislocked">mislocked</a>, <a href="doc_002dpersistent.html#doc_002dpersistent">persistent</a>. </p></blockquote></div> <!-- mislocked src/variables.cc --> <p><a name="doc_002dmislocked"></a> <div class="defun"> — Built-in Function: <b>mislocked</b> ()<var><a name="index-mislocked-784"></a></var><br> — Built-in Function: <b>mislocked</b> (<var>fcn</var>)<var><a name="index-mislocked-785"></a></var><br> <blockquote><p>Return true if the named function <var>fcn</var> is locked. If no function is named then return true if the current function is locked. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dmlock.html#doc_002dmlock">mlock</a>, <a href="doc_002dmunlock.html#doc_002dmunlock">munlock</a>, <a href="doc_002dpersistent.html#doc_002dpersistent">persistent</a>. </p></blockquote></div> </body></html>