/[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 182 - (show annotations) (download) (as text)
Wed Jun 13 15:09:54 2007 UTC (7 years, 4 months ago) by ph10
File MIME type: text/html
File size: 6410 byte(s)
More document tidies, pre-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 hardly uses recursion at all. The limit on its complexity is the amount of
35 workspace it is given. The comments that follow do NOT apply to
36 <b>pcre_dfa_exec()</b>; they are relevant only for <b>pcre_exec()</b>.
37 </P>
38 <P>
39 You can set limits on the number of times that <b>match()</b> is called, both in
40 total and recursively. If the limit is exceeded, an error occurs. For details,
41 see the
42 <a href="pcreapi.html#extradata">section on extra data for <b>pcre_exec()</b></a>
43 in the
44 <a href="pcreapi.html"><b>pcreapi</b></a>
45 documentation.
46 </P>
47 <P>
48 Each time that <b>match()</b> is actually called recursively, it uses memory
49 from the process stack. For certain kinds of pattern and data, very large
50 amounts of stack may be needed, despite the recognition of "tail recursion".
51 You can often reduce the amount of recursion, and therefore the amount of stack
52 used, by modifying the pattern that is being matched. Consider, for example,
53 this pattern:
54 <pre>
55 ([^&#60;]|&#60;(?!inet))+
56 </pre>
57 It matches from wherever it starts until it encounters "&#60;inet" or the end of
58 the data, and is the kind of pattern that might be used when processing an XML
59 file. Each iteration of the outer parentheses matches either one character that
60 is not "&#60;" or a "&#60;" that is not followed by "inet". However, each time a
61 parenthesis is processed, a recursion occurs, so this formulation uses a stack
62 frame for each matched character. For a long string, a lot of stack is
63 required. Consider now this rewritten pattern, which matches exactly the same
64 strings:
65 <pre>
66 ([^&#60;]++|&#60;(?!inet))+
67 </pre>
68 This uses very much less stack, because runs of characters that do not contain
69 "&#60;" are "swallowed" in one item inside the parentheses. Recursion happens only
70 when a "&#60;" character that is not followed by "inet" is encountered (and we
71 assume this is relatively rare). A possessive quantifier is used to stop any
72 backtracking into the runs of non-"&#60;" characters, but that is not related to
73 stack usage.
74 </P>
75 <P>
76 This example shows that one way of avoiding stack problems when matching long
77 subject strings is to write repeated parenthesized subpatterns to match more
78 than one character whenever possible.
79 </P>
80 <P>
81 In environments where stack memory is constrained, you might want to compile
82 PCRE to use heap memory instead of stack for remembering back-up points. This
83 makes it run a lot more slowly, however. Details of how to do this are given in
84 the
85 <a href="pcrebuild.html"><b>pcrebuild</b></a>
86 documentation. When built in this way, instead of using the stack, PCRE obtains
87 and frees memory by calling the functions that are pointed to by the
88 <b>pcre_stack_malloc</b> and <b>pcre_stack_free</b> variables. By default, these
89 point to <b>malloc()</b> and <b>free()</b>, but you can replace the pointers to
90 cause PCRE to use your own functions. Since the block sizes are always the
91 same, and are always freed in reverse order, it may be possible to implement
92 customized memory handlers that are more efficient than the standard functions.
93 </P>
94 <P>
95 In Unix-like environments, there is not often a problem with the stack unless
96 very long strings are involved, though the default limit on stack size varies
97 from system to system. Values from 8Mb to 64Mb are common. You can find your
98 default limit by running the command:
99 <pre>
100 ulimit -s
101 </pre>
102 Unfortunately, the effect of running out of stack is often SIGSEGV, though
103 sometimes a more explicit error message is given. You can normally increase the
104 limit on stack size by code such as this:
105 <pre>
106 struct rlimit rlim;
107 getrlimit(RLIMIT_STACK, &rlim);
108 rlim.rlim_cur = 100*1024*1024;
109 setrlimit(RLIMIT_STACK, &rlim);
110 </pre>
111 This reads the current limits (soft and hard) using <b>getrlimit()</b>, then
112 attempts to increase the soft limit to 100Mb using <b>setrlimit()</b>. You must
113 do this before calling <b>pcre_exec()</b>.
114 </P>
115 <P>
116 PCRE has an internal counter that can be used to limit the depth of recursion,
117 and thus cause <b>pcre_exec()</b> to give an error code before it runs out of
118 stack. By default, the limit is very large, and unlikely ever to operate. It
119 can be changed when PCRE is built, and it can also be set when
120 <b>pcre_exec()</b> is called. For details of these interfaces, see the
121 <a href="pcrebuild.html"><b>pcrebuild</b></a>
122 and
123 <a href="pcreapi.html"><b>pcreapi</b></a>
124 documentation.
125 </P>
126 <P>
127 As a very rough rule of thumb, you should reckon on about 500 bytes per
128 recursion. Thus, if you want to limit your stack usage to 8Mb, you
129 should set the limit at 16000 recursions. A 64Mb stack, on the other hand, can
130 support around 128000 recursions. The <b>pcretest</b> test program has a command
131 line option (<b>-S</b>) that can be used to increase the size of its stack.
132 </P>
133 <br><b>
134 AUTHOR
135 </b><br>
136 <P>
137 Philip Hazel
138 <br>
139 University Computing Service
140 <br>
141 Cambridge CB2 3QH, England.
142 <br>
143 </P>
144 <br><b>
145 REVISION
146 </b><br>
147 <P>
148 Last updated: 05 June 2007
149 <br>
150 Copyright &copy; 1997-2007 University of Cambridge.
151 <br>
152 <p>
153 Return to the <a href="index.html">PCRE index page</a>.
154 </p>

Properties

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12