Initial Import
[profile/ivi/pcre.git] / doc / pcrestack.3
1 .TH PCRESTACK 3
2 .SH NAME
3 PCRE - Perl-compatible regular expressions
4 .SH "PCRE DISCUSSION OF STACK USAGE"
5 .rs
6 .sp
7 When you call \fBpcre_exec()\fP, it makes use of an internal function called
8 \fBmatch()\fP. This calls itself recursively at branch points in the pattern,
9 in order to remember the state of the match so that it can back up and try a
10 different alternative if the first one fails. As matching proceeds deeper and
11 deeper into the tree of possibilities, the recursion depth increases.
12 .P
13 Not all calls of \fBmatch()\fP increase the recursion depth; for an item such
14 as a* it may be called several times at the same level, after matching
15 different numbers of a's. Furthermore, in a number of cases where the result of
16 the recursive call would immediately be passed back as the result of the
17 current call (a "tail recursion"), the function is just restarted instead.
18 .P
19 The \fBpcre_dfa_exec()\fP function operates in an entirely different way, and
20 uses recursion only when there is a regular expression recursion or subroutine
21 call in the pattern. This includes the processing of assertion and "once-only"
22 subpatterns, which are handled like subroutine calls. Normally, these are never
23 very deep, and the limit on the complexity of \fBpcre_dfa_exec()\fP is
24 controlled by the amount of workspace it is given. However, it is possible to
25 write patterns with runaway infinite recursions; such patterns will cause
26 \fBpcre_dfa_exec()\fP to run out of stack. At present, there is no protection
27 against this.
28 .P
29 The comments that follow do NOT apply to \fBpcre_dfa_exec()\fP; they are
30 relevant only for \fBpcre_exec()\fP.
31 .
32 .
33 .SS "Reducing \fBpcre_exec()\fP's stack usage"
34 .rs
35 .sp
36 Each time that \fBmatch()\fP is actually called recursively, it uses memory
37 from the process stack. For certain kinds of pattern and data, very large
38 amounts of stack may be needed, despite the recognition of "tail recursion".
39 You can often reduce the amount of recursion, and therefore the amount of stack
40 used, by modifying the pattern that is being matched. Consider, for example,
41 this pattern:
42 .sp
43   ([^<]|<(?!inet))+
44 .sp
45 It matches from wherever it starts until it encounters "<inet" or the end of
46 the data, and is the kind of pattern that might be used when processing an XML
47 file. Each iteration of the outer parentheses matches either one character that
48 is not "<" or a "<" that is not followed by "inet". However, each time a
49 parenthesis is processed, a recursion occurs, so this formulation uses a stack
50 frame for each matched character. For a long string, a lot of stack is
51 required. Consider now this rewritten pattern, which matches exactly the same
52 strings:
53 .sp
54   ([^<]++|<(?!inet))+
55 .sp
56 This uses very much less stack, because runs of characters that do not contain
57 "<" are "swallowed" in one item inside the parentheses. Recursion happens only
58 when a "<" character that is not followed by "inet" is encountered (and we
59 assume this is relatively rare). A possessive quantifier is used to stop any
60 backtracking into the runs of non-"<" characters, but that is not related to
61 stack usage.
62 .P
63 This example shows that one way of avoiding stack problems when matching long
64 subject strings is to write repeated parenthesized subpatterns to match more
65 than one character whenever possible.
66 .
67 .
68 .SS "Compiling PCRE to use heap instead of stack for \fBpcre_exec()\fP"
69 .rs
70 .sp
71 In environments where stack memory is constrained, you might want to compile
72 PCRE to use heap memory instead of stack for remembering back-up points when
73 \fBpcre_exec()\fP is running. This makes it run a lot more slowly, however.
74 Details of how to do this are given in the
75 .\" HREF
76 \fBpcrebuild\fP
77 .\"
78 documentation. When built in this way, instead of using the stack, PCRE obtains
79 and frees memory by calling the functions that are pointed to by the
80 \fBpcre_stack_malloc\fP and \fBpcre_stack_free\fP variables. By default, these
81 point to \fBmalloc()\fP and \fBfree()\fP, but you can replace the pointers to
82 cause PCRE to use your own functions. Since the block sizes are always the
83 same, and are always freed in reverse order, it may be possible to implement
84 customized memory handlers that are more efficient than the standard functions.
85 .
86 .
87 .SS "Limiting \fBpcre_exec()\fP's stack usage"
88 .rs
89 .sp
90 You can set limits on the number of times that \fBmatch()\fP is called, both in
91 total and recursively. If a limit is exceeded, \fBpcre_exec()\fP returns an
92 error code. Setting suitable limits should prevent it from running out of
93 stack. The default values of the limits are very large, and unlikely ever to
94 operate. They can be changed when PCRE is built, and they can also be set when
95 \fBpcre_exec()\fP is called. For details of these interfaces, see the
96 .\" HREF
97 \fBpcrebuild\fP
98 .\"
99 documentation and the
100 .\" HTML <a href="pcreapi.html#extradata">
101 .\" </a>
102 section on extra data for \fBpcre_exec()\fP
103 .\"
104 in the
105 .\" HREF
106 \fBpcreapi\fP
107 .\"
108 documentation.
109 .P
110 As a very rough rule of thumb, you should reckon on about 500 bytes per
111 recursion. Thus, if you want to limit your stack usage to 8Mb, you
112 should set the limit at 16000 recursions. A 64Mb stack, on the other hand, can
113 support around 128000 recursions.
114 .P
115 In Unix-like environments, the \fBpcretest\fP test program has a command line
116 option (\fB-S\fP) that can be used to increase the size of its stack. As long
117 as the stack is large enough, another option (\fB-M\fP) can be used to find the
118 smallest limits that allow a particular pattern to match a given subject
119 string. This is done by calling \fBpcre_exec()\fP repeatedly with different
120 limits.
121 .
122 .
123 .SS "Changing stack size in Unix-like systems"
124 .rs
125 .sp
126 In Unix-like environments, there is not often a problem with the stack unless
127 very long strings are involved, though the default limit on stack size varies
128 from system to system. Values from 8Mb to 64Mb are common. You can find your
129 default limit by running the command:
130 .sp
131   ulimit -s
132 .sp
133 Unfortunately, the effect of running out of stack is often SIGSEGV, though
134 sometimes a more explicit error message is given. You can normally increase the
135 limit on stack size by code such as this:
136 .sp
137   struct rlimit rlim;
138   getrlimit(RLIMIT_STACK, &rlim);
139   rlim.rlim_cur = 100*1024*1024;
140   setrlimit(RLIMIT_STACK, &rlim);
141 .sp
142 This reads the current limits (soft and hard) using \fBgetrlimit()\fP, then
143 attempts to increase the soft limit to 100Mb using \fBsetrlimit()\fP. You must
144 do this before calling \fBpcre_exec()\fP.
145 .
146 .
147 .SS "Changing stack size in Mac OS X"
148 .rs
149 .sp
150 Using \fBsetrlimit()\fP, as described above, should also work on Mac OS X. It
151 is also possible to set a stack size when linking a program. There is a
152 discussion about stack sizes in Mac OS X at this web site:
153 .\" HTML <a href="http://developer.apple.com/qa/qa2005/qa1419.html">
154 .\" </a>
155 http://developer.apple.com/qa/qa2005/qa1419.html.
156 .\"
157 .
158 .
159 .SH AUTHOR
160 .rs
161 .sp
162 .nf
163 Philip Hazel
164 University Computing Service
165 Cambridge CB2 3QH, England.
166 .fi
167 .
168 .
169 .SH REVISION
170 .rs
171 .sp
172 .nf
173 Last updated: 03 January 2010
174 Copyright (c) 1997-2010 University of Cambridge.
175 .fi