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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5