/[pcre]/code/trunk/doc/html/pcrestack.html
ViewVC logotype

Contents of /code/trunk/doc/html/pcrestack.html

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

  ViewVC Help
Powered by ViewVC 1.1.5