.TH PCRESTACK 3 .SH NAME PCRE - Perl-compatible regular expressions .SH "PCRE DISCUSSION OF STACK USAGE" .rs .sp When you call \fBpcre_exec()\fP, it makes use of an internal function called \fBmatch()\fP. 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. .P Not all calls of \fBmatch()\fP increase the recursion depth; for an item such as a* it may be called several times at the same level, after matching different numbers of a's. Furthermore, in a number of cases where the result of the recursive call would immediately be passed back as the result of the current call (a "tail recursion"), the function is just restarted instead. .P The \fBpcre_dfa_exec()\fP function operates in an entirely different way, and uses recursion only when there is a regular expression recursion or subroutine call in the pattern. This includes the processing of assertion and "once-only" subpatterns, which are handled like subroutine calls. Normally, these are never very deep, and the limit on the complexity of \fBpcre_dfa_exec()\fP is controlled by the amount of workspace it is given. However, it is possible to write patterns with runaway infinite recursions; such patterns will cause \fBpcre_dfa_exec()\fP to run out of stack. At present, there is no protection against this. .P The comments that follow do NOT apply to \fBpcre_dfa_exec()\fP; they are relevant only for \fBpcre_exec()\fP. . . .SS "Reducing \fBpcre_exec()\fP's stack usage" .rs .sp Each time that \fBmatch()\fP 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: .sp ([^<]|<(?!inet))+ .sp It matches from wherever it starts until it encounters " .\" section on extra data for \fBpcre_exec()\fP .\" in the .\" HREF \fBpcreapi\fP .\" documentation. .P As a very rough rule of thumb, you should reckon on about 500 bytes per recursion. Thus, if you want to limit your stack usage to 8Mb, you should set the limit at 16000 recursions. A 64Mb stack, on the other hand, can support around 128000 recursions. .P In Unix-like environments, the \fBpcretest\fP test program has a command line option (\fB-S\fP) that can be used to increase the size of its stack. As long as the stack is large enough, another option (\fB-M\fP) can be used to find the smallest limits that allow a particular pattern to match a given subject string. This is done by calling \fBpcre_exec()\fP repeatedly with different limits. . . .SS "Changing stack size in Unix-like systems" .rs .sp 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: .sp ulimit -s .sp Unfortunately, the effect of running out of stack is often SIGSEGV, though sometimes a more explicit error message is given. You can normally increase the limit on stack size by code such as this: .sp struct rlimit rlim; getrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = 100*1024*1024; setrlimit(RLIMIT_STACK, &rlim); .sp This reads the current limits (soft and hard) using \fBgetrlimit()\fP, then attempts to increase the soft limit to 100Mb using \fBsetrlimit()\fP. You must do this before calling \fBpcre_exec()\fP. . . .SS "Changing stack size in Mac OS X" .rs .sp Using \fBsetrlimit()\fP, 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: .\" HTML .\" http://developer.apple.com/qa/qa2005/qa1419.html. .\" . . .SH AUTHOR .rs .sp .nf Philip Hazel University Computing Service Cambridge CB2 3QH, England. .fi . . .SH REVISION .rs .sp .nf Last updated: 03 January 2010 Copyright (c) 1997-2010 University of Cambridge. .fi