Sortix nightly manual
This manual documents Sortix nightly, a development build that has not been officially released. You can instead view this document in the latest official manual.
PCRESTACK(3) | Library Functions Manual | PCRESTACK(3) |
NAME
PCRE - Perl-compatible regular expressionsPCRE DISCUSSION OF STACK USAGE
When you call pcre[16|32]_exec(), it makes use of an internal function called match(). This calls itself recursively at branch points in the pattern, in order to remember the state of the match so that it can back up and try a different alternative if the first one fails. As matching proceeds deeper and deeper into the tree of possibilities, the recursion depth increases. The match() function is also called in other circumstances, for example, whenever a parenthesized sub-pattern is entered, and in certain cases of repetition.Reducing pcre[16|32]_exec()'s stack usage
Each time that match() is actually called recursively, it uses memory from the process stack. For certain kinds of pattern and data, very large amounts of stack may be needed, despite the recognition of "tail recursion". You can often reduce the amount of recursion, and therefore the amount of stack used, by modifying the pattern that is being matched. Consider, for example, this pattern:([^<]|<(?!inet))+
([^<]++|<(?!inet))+
Compiling PCRE to use heap instead of stack for pcre[16|32]_exec()
In environments where stack memory is constrained, you might want to compile PCRE to use heap memory instead of stack for remembering back-up points when pcre[16|32]_exec() is running. This makes it run a lot more slowly, however. Details of how to do this are given in the pcrebuild documentation. When built in this way, instead of using the stack, PCRE obtains and frees memory by calling the functions that are pointed to by the pcre[16|32]_stack_malloc and pcre[16|32]_stack_free variables. By default, these point to malloc() and free(), but you can replace the pointers to cause PCRE to use your own functions. Since the block sizes are always the same, and are always freed in reverse order, it may be possible to implement customized memory handlers that are more efficient than the standard functions.Limiting pcre[16|32]_exec()'s stack usage
You can set limits on the number of times that match() is called, both in total and recursively. If a limit is exceeded, pcre[16|32]_exec() returns an error code. Setting suitable limits should prevent it from running out of stack. The default values of the limits are very large, and unlikely ever to operate. They can be changed when PCRE is built, and they can also be set when pcre[16|32]_exec() is called. For details of these interfaces, see the pcrebuild documentation and the section on extra data for pcre[16|32]_exec() in the pcreapi documentation.Obtaining an estimate of stack usage
The actual amount of stack used per recursion can vary quite a lot, depending on the compiler that was used to build PCRE and the optimization or debugging options that were set for it. The rule of thumb value of 500 bytes mentioned above may be larger or smaller than what is actually needed. A better approximation can be obtained by running this command:pcretest -m -C
Match recursion uses stack: approximate frame size = 640 bytes
Changing stack size in Unix-like systems
In Unix-like environments, there is not often a problem with the stack unless very long strings are involved, though the default limit on stack size varies from system to system. Values from 8Mb to 64Mb are common. You can find your default limit by running the command:ulimit -s
struct rlimit rlim;
getrlimit(RLIMIT_STACK, &rlim);
rlim.rlim_cur = 100*1024*1024;
setrlimit(RLIMIT_STACK, &rlim);
Changing stack size in Mac OS X
Using setrlimit(), as described above, should also work on Mac OS X. It is also possible to set a stack size when linking a program. There is a discussion about stack sizes in Mac OS X at this web site: http://developer.apple.com/qa/qa2005/qa1419.html.24 June 2012 | PCRE 8.30 |