Adjusting the size of Prolog stacks

### 2.3 Adjusting the size of Prolog stacks

GNU Prolog uses several stacks to execute a Prolog program. Each stack has a static size and cannot be dynamically increased during the execution. For each stack there is a default size but the user can define a new size by setting an environment variable. When a GNU Prolog program is run it first consults these variables and if they are not defined uses the default sizes. The following table presents each stack of GNU Prolog with its default size and the name of its associated environment variable:

Stack | Default | Environment | Description
name | size (Kb) | variable |
`local` | 2048 | `LOCALSZ` | control stack (environments and choice-points)
`global` | 4096 | `GLOBALSZ` | heap (compound terms)
`trail` | 2048 | `TRAILSZ` | conditional bindings (bindings to undo at backtracking)
`cstr` | 2048 | `CSTRSZ` | finite domain constraint stack (FD variables and constraints)

If the size of a stack is too small an overflow will occur during the execution. In that case GNU Prolog emits the following error message before stopping:

  ``S`` `stack overflow (size:` ``N`` `Kb, environment variable used:` ``E```)`

where `S` is the name of the stack, `N` is the current stack size in Kb and `E` the name of the associated environment variable. When such a message occurs it is possible to (re)define the variable `E` with the new size. For instance to allocate 4096 Kb to the local stack under a Unix shell use:

  `LOCALSZ=4096; export LOCALS` |  | (under `sh` or `bash`)
  `setenv LOCALSZ 4096` |  | (under `csh` or `tcsh`)

This method allows the user to adjust the size of Prolog stacks. However, in some cases it is preferable not to allow the user to modify these sizes. For instance, when providing a stand alone executable whose behavior should be independent of the environment in which it is run. In that case the program should not consult environment variables and the programmer should be able to define new default stack sizes. The GNU Prolog compiler offers this facilities via several command-line options such as `--local-size` or `--fixed-sizes` (section 2.4.3).
Finally note that GNU Prolog stacks are virtually allocated (i.e. use virtual memory). This means that a physical memory page is allocated only when needed (i.e. when an attempt to read/write it occurs). Thus it is possible to define very large stacks. At the execution, only the needed amount of space will be physically allocated.

Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.
More about the copyright