# Diff of /code/trunk/doc/html/pcrestack.html

revision 691 by ph10, Sun Sep 11 14:31:21 2011 UTC revision 869 by ph10, Sat Jan 14 11:16:23 2012 UTC
# Line 16  man page, in case the conversion went wr Line 16  man page, in case the conversion went wr
16  PCRE DISCUSSION OF STACK USAGE  PCRE DISCUSSION OF STACK USAGE
17  </b><br>  </b><br>
18  <P>  <P>
19  When you call <b>pcre_exec()</b>, it makes use of an internal function called  When you call <b>pcre[16]_exec()</b>, it makes use of an internal function
20  <b>match()</b>. This calls itself recursively at branch points in the pattern,  called <b>match()</b>. This calls itself recursively at branch points in the
21  in order to remember the state of the match so that it can back up and try a  pattern, in order to remember the state of the match so that it can back up and
22  different alternative if the first one fails. As matching proceeds deeper and  try a different alternative if the first one fails. As matching proceeds deeper
23  deeper into the tree of possibilities, the recursion depth increases. The  and deeper into the tree of possibilities, the recursion depth increases. The
24  <b>match()</b> function is also called in other circumstances, for example,  <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  whenever a parenthesized sub-pattern is entered, and in certain cases of
26  repetition.  repetition.
# Line 33  the recursive call would immediately be Line 33  the recursive call would immediately be
33  current call (a "tail recursion"), the function is just restarted instead.  current call (a "tail recursion"), the function is just restarted instead.
34  </P>  </P>
35  <P>  <P>
36  The above comments apply when <b>pcre_exec()</b> is run in its normal  The above comments apply when <b>pcre[16]_exec()</b> is run in its normal
37  interpretive manner. If the pattern was studied with the  interpretive manner. If the pattern was studied with the
38  PCRE_STUDY_JIT_COMPILE option, and just-in-time compiling was successful, and  PCRE_STUDY_JIT_COMPILE option, and just-in-time compiling was successful, and
39  the options passed to <b>pcre_exec()</b> were not incompatible, the matching  the options passed to <b>pcre[16]_exec()</b> were not incompatible, the matching
40  process uses the JIT-compiled code instead of the <b>match()</b> function. In  process uses the JIT-compiled code instead of the <b>match()</b> function. In
41  this case, the memory requirements are handled entirely differently. See the  this case, the memory requirements are handled entirely differently. See the
42  <a href="pcrejit.html"><b>pcrejit</b></a>  <a href="pcrejit.html"><b>pcrejit</b></a>
43  documentation for details.  documentation for details.
44  </P>  </P>
45  <P>  <P>
46  The <b>pcre_dfa_exec()</b> function operates in an entirely different way, and  The <b>pcre[16]_dfa_exec()</b> function operates in an entirely different way,
47  uses recursion only when there is a regular expression recursion or subroutine  and uses recursion only when there is a regular expression recursion or
48  call in the pattern. This includes the processing of assertion and "once-only"  subroutine call in the pattern. This includes the processing of assertion and
49  subpatterns, which are handled like subroutine calls. Normally, these are never  "once-only" subpatterns, which are handled like subroutine calls. Normally,
50  very deep, and the limit on the complexity of <b>pcre_dfa_exec()</b> is  these are never very deep, and the limit on the complexity of
51  controlled by the amount of workspace it is given. However, it is possible to  <b>pcre[16]_dfa_exec()</b> is controlled by the amount of workspace it is given.
52  write patterns with runaway infinite recursions; such patterns will cause  However, it is possible to write patterns with runaway infinite recursions;
53  <b>pcre_dfa_exec()</b> to run out of stack. At present, there is no protection  such patterns will cause <b>pcre[16]_dfa_exec()</b> to run out of stack. At
54  against this.  present, there is no protection against this.
55  </P>  </P>
56  <P>  <P>
57  The comments that follow do NOT apply to <b>pcre_dfa_exec()</b>; they are  The comments that follow do NOT apply to <b>pcre[16]_dfa_exec()</b>; they are
58  relevant only for <b>pcre_exec()</b> without the JIT optimization.  relevant only for <b>pcre[16]_exec()</b> without the JIT optimization.
59  </P>  </P>
60  <br><b>  <br><b>
61  Reducing <b>pcre_exec()</b>'s stack usage  Reducing <b>pcre[16]_exec()</b>'s stack usage
62  </b><br>  </b><br>
63  <P>  <P>
64  Each time that <b>match()</b> is actually called recursively, it uses memory  Each time that <b>match()</b> is actually called recursively, it uses memory
# Line 94  subject strings is to write repeated par Line 94  subject strings is to write repeated par
94  than one character whenever possible.  than one character whenever possible.
95  </P>  </P>
96  <br><b>  <br><b>
97  Compiling PCRE to use heap instead of stack for <b>pcre_exec()</b>  Compiling PCRE to use heap instead of stack for <b>pcre[16]_exec()</b>
98  </b><br>  </b><br>
99  <P>  <P>
100  In environments where stack memory is constrained, you might want to compile  In environments where stack memory is constrained, you might want to compile
101  PCRE to use heap memory instead of stack for remembering back-up points when  PCRE to use heap memory instead of stack for remembering back-up points when
102  <b>pcre_exec()</b> is running. This makes it run a lot more slowly, however.  <b>pcre[16]_exec()</b> is running. This makes it run a lot more slowly, however.
103  Details of how to do this are given in the  Details of how to do this are given in the
104  <a href="pcrebuild.html"><b>pcrebuild</b></a>  <a href="pcrebuild.html"><b>pcrebuild</b></a>
105  documentation. When built in this way, instead of using the stack, PCRE obtains  documentation. When built in this way, instead of using the stack, PCRE obtains
106  and frees memory by calling the functions that are pointed to by the  and frees memory by calling the functions that are pointed to by the
107  <b>pcre_stack_malloc</b> and <b>pcre_stack_free</b> variables. By default, these  <b>pcre[16]_stack_malloc</b> and <b>pcre[16]_stack_free</b> variables. By
108  point to <b>malloc()</b> and <b>free()</b>, but you can replace the pointers to  default, these point to <b>malloc()</b> and <b>free()</b>, but you can replace
109  cause PCRE to use your own functions. Since the block sizes are always the  the pointers to cause PCRE to use your own functions. Since the block sizes are
110  same, and are always freed in reverse order, it may be possible to implement  always the same, and are always freed in reverse order, it may be possible to
111  customized memory handlers that are more efficient than the standard functions.  implement customized memory handlers that are more efficient than the standard
112    functions.
113  </P>  </P>
114  <br><b>  <br><b>
115  Limiting <b>pcre_exec()</b>'s stack usage  Limiting <b>pcre[16]_exec()</b>'s stack usage
116  </b><br>  </b><br>
117  <P>  <P>
118  You can set limits on the number of times that <b>match()</b> is called, both in  You can set limits on the number of times that <b>match()</b> is called, both in
119  total and recursively. If a limit is exceeded, <b>pcre_exec()</b> returns an  total and recursively. If a limit is exceeded, <b>pcre[16]_exec()</b> returns an
120  error code. Setting suitable limits should prevent it from running out of  error code. Setting suitable limits should prevent it from running out of
121  stack. The default values of the limits are very large, and unlikely ever to  stack. The default values of the limits are very large, and unlikely ever to
122  operate. They can be changed when PCRE is built, and they can also be set when  operate. They can be changed when PCRE is built, and they can also be set when
123  <b>pcre_exec()</b> is called. For details of these interfaces, see the  <b>pcre[16]_exec()</b> is called. For details of these interfaces, see the
124  <a href="pcrebuild.html"><b>pcrebuild</b></a>  <a href="pcrebuild.html"><b>pcrebuild</b></a>
125  documentation and the  documentation and the
126  <a href="pcreapi.html#extradata">section on extra data for <b>pcre_exec()</b></a>  <a href="pcreapi.html#extradata">section on extra data for <b>pcre[16]_exec()</b></a>
127  in the  in the
128  <a href="pcreapi.html"><b>pcreapi</b></a>  <a href="pcreapi.html"><b>pcreapi</b></a>
129  documentation.  documentation.
# Line 138  In Unix-like environments, the <b>pcrete Line 139  In Unix-like environments, the <b>pcrete
139  option (<b>-S</b>) that can be used to increase the size of its stack. As long  option (<b>-S</b>) that can be used to increase the size of its stack. As long
140  as the stack is large enough, another option (<b>-M</b>) can be used to find the  as the stack is large enough, another option (<b>-M</b>) can be used to find the
141  smallest limits that allow a particular pattern to match a given subject  smallest limits that allow a particular pattern to match a given subject
142  string. This is done by calling <b>pcre_exec()</b> repeatedly with different  string. This is done by calling <b>pcre[16]_exec()</b> repeatedly with different
143  limits.  limits.
144  </P>  </P>
145  <br><b>  <br><b>
# Line 163  limit on stack size by code such as this Line 164  limit on stack size by code such as this
164  </pre>  </pre>
165  This reads the current limits (soft and hard) using <b>getrlimit()</b>, then  This reads the current limits (soft and hard) using <b>getrlimit()</b>, then
166  attempts to increase the soft limit to 100Mb using <b>setrlimit()</b>. You must  attempts to increase the soft limit to 100Mb using <b>setrlimit()</b>. You must
167  do this before calling <b>pcre_exec()</b>.  do this before calling <b>pcre[16]_exec()</b>.
168  </P>  </P>
169  <br><b>  <br><b>
170  Changing stack size in Mac OS X  Changing stack size in Mac OS X
# Line 189  Cambridge CB2 3QH, England. Line 190  Cambridge CB2 3QH, England.
190  REVISION  REVISION
191  </b><br>  </b><br>
192  <P>  <P>
193  Last updated: 26 August 2011  Last updated: 10 January 2012
194  <br>  <br>
195  Copyright &copy; 1997-2011 University of Cambridge.  Copyright &copy; 1997-2012 University of Cambridge.
196  <br>  <br>
197  <p>  <p>