11.10.6 Function Locking

It is sometime desirable to lock a function into memory with the mlock 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 clear does not remove this initialization.

As an example,

function my_function ()
  mlock ();
  …
endfunction

prevents my_function from being removed from memory after it is called, even if clear is called. It is possible to determine if a function is locked into memory with the mislocked, and to unlock a function with munlock, which the following code illustrates.

my_function ();
mislocked ("my_function")
⇒ ans = 1
munlock ("my_function");
mislocked ("my_function")
⇒ ans = 0

A common use of mlock is to prevent persistent variables from being removed from memory, as the following example shows:

function count_calls ()
  mlock ();
  persistent calls = 0;
  printf ("count_calls() has been called %d times\n", ++calls);
endfunction

count_calls ();
-| count_calls() has been called 1 times

clear count_calls
count_calls ();
-| count_calls() has been called 2 times

mlock might also be used to prevent changes to an m-file, such as in an external editor, from having any effect in the current Octave session; A similar effect can be had with the ignore_function_time_stamp function.

: mlock ()

Lock the current function into memory so that it can’t be removed with clear.

See also: munlock, mislocked, persistent, clear.

: munlock ()
: munlock (fcn)

Unlock the named function fcn so that it may be removed from memory with clear.

If no function is named then unlock the current function.

See also: mlock, mislocked, persistent, clear.

: mislocked ()
: mislocked (fcn)

Return true if the named function fcn is locked in memory.

If no function is named then return true if the current function is locked.

See also: mlock, munlock, persistent, clear.

© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v6.3.0/Function-Locking.html