1PCRESTACK(3)               Library Functions Manual               PCRESTACK(3)
2
3
4

NAME

6       PCRE - Perl-compatible regular expressions
7

PCRE DISCUSSION OF STACK USAGE

9
10       When  you call pcre_exec(), it makes use of an internal function called
11       match(). This calls itself recursively at branch points in the pattern,
12       in  order to remember the state of the match so that it can back up and
13       try a different alternative if the first one fails.  As  matching  pro‐
14       ceeds  deeper  and deeper into the tree of possibilities, the recursion
15       depth increases.
16
17       Not all calls of match() increase the recursion depth; for an item such
18       as  a* it may be called several times at the same level, after matching
19       different numbers of a's. Furthermore, in a number of cases  where  the
20       result  of  the  recursive call would immediately be passed back as the
21       result of the current call (a "tail recursion"), the function  is  just
22       restarted instead.
23
24       The pcre_dfa_exec() function operates in an entirely different way, and
25       hardly uses recursion at all. The limit on its complexity is the amount
26       of  workspace  it  is  given.  The comments that follow do NOT apply to
27       pcre_dfa_exec(); they are relevant only for pcre_exec().
28
29       You can set limits on the number of times that match() is called,  both
30       in  total  and  recursively. If the limit is exceeded, an error occurs.
31       For details, see the section on  extra  data  for  pcre_exec()  in  the
32       pcreapi documentation.
33
34       Each  time  that match() is actually called recursively, it uses memory
35       from the process stack. For certain kinds of  pattern  and  data,  very
36       large  amounts of stack may be needed, despite the recognition of "tail
37       recursion".  You can often reduce the amount of recursion,  and  there‐
38       fore  the  amount of stack used, by modifying the pattern that is being
39       matched. Consider, for example, this pattern:
40
41         ([^<]|<(?!inet))+
42
43       It matches from wherever it starts until it encounters "<inet"  or  the
44       end  of  the  data,  and is the kind of pattern that might be used when
45       processing an XML file. Each iteration of the outer parentheses matches
46       either  one  character that is not "<" or a "<" that is not followed by
47       "inet". However, each time a  parenthesis  is  processed,  a  recursion
48       occurs, so this formulation uses a stack frame for each matched charac‐
49       ter. For a long string, a lot of stack is required. Consider  now  this
50       rewritten pattern, which matches exactly the same strings:
51
52         ([^<]++|<(?!inet))+
53
54       This  uses very much less stack, because runs of characters that do not
55       contain "<" are "swallowed" in one item inside the parentheses.  Recur‐
56       sion  happens  only when a "<" character that is not followed by "inet"
57       is encountered (and we assume this is relatively  rare).  A  possessive
58       quantifier  is  used  to stop any backtracking into the runs of non-"<"
59       characters, but that is not related to stack usage.
60
61       This example shows that one way of avoiding stack problems when  match‐
62       ing long subject strings is to write repeated parenthesized subpatterns
63       to match more than one character whenever possible.
64
65   Compiling PCRE to use heap instead of stack
66
67       In environments where stack memory is constrained, you  might  want  to
68       compile  PCRE to use heap memory instead of stack for remembering back-
69       up points. This makes it run a lot more slowly, however. Details of how
70       to do this are given in the pcrebuild documentation. When built in this
71       way, instead of using the stack, PCRE obtains and frees memory by call‐
72       ing  the  functions  that  are  pointed to by the pcre_stack_malloc and
73       pcre_stack_free variables. By default,  these  point  to  malloc()  and
74       free(),  but you can replace the pointers to cause PCRE to use your own
75       functions. Since the block sizes are always the same,  and  are  always
76       freed in reverse order, it may be possible to implement customized mem‐
77       ory handlers that are more efficient than the standard functions.
78
79   Limiting PCRE's stack usage
80
81       PCRE has an internal counter that can be used to  limit  the  depth  of
82       recursion,  and  thus cause pcre_exec() to give an error code before it
83       runs out of stack. By default, the limit is very  large,  and  unlikely
84       ever  to operate. It can be changed when PCRE is built, and it can also
85       be set when pcre_exec() is called. For details of these interfaces, see
86       the pcrebuild and pcreapi documentation.
87
88       As a very rough rule of thumb, you should reckon on about 500 bytes per
89       recursion. Thus, if you want to limit your  stack  usage  to  8Mb,  you
90       should  set  the  limit at 16000 recursions. A 64Mb stack, on the other
91       hand, can support around 128000 recursions. The pcretest  test  program
92       has a command line option (-S) that can be used to increase the size of
93       its stack.
94
95   Changing stack size in Unix-like systems
96
97       In Unix-like environments, there is not often a problem with the  stack
98       unless  very  long  strings  are  involved, though the default limit on
99       stack size varies from system to system. Values from 8Mb  to  64Mb  are
100       common. You can find your default limit by running the command:
101
102         ulimit -s
103
104       Unfortunately,  the  effect  of  running out of stack is often SIGSEGV,
105       though sometimes a more explicit error message is given. You  can  nor‐
106       mally increase the limit on stack size by code such as this:
107
108         struct rlimit rlim;
109         getrlimit(RLIMIT_STACK, &rlim);
110         rlim.rlim_cur = 100*1024*1024;
111         setrlimit(RLIMIT_STACK, &rlim);
112
113       This  reads  the current limits (soft and hard) using getrlimit(), then
114       attempts to increase the soft limit to  100Mb  using  setrlimit().  You
115       must do this before calling pcre_exec().
116
117   Changing stack size in Mac OS X
118
119       Using setrlimit(), as described above, should also work on Mac OS X. It
120       is also possible to set a stack size when linking a program. There is a
121       discussion   about   stack  sizes  in  Mac  OS  X  at  this  web  site:
122       http://developer.apple.com/qa/qa2005/qa1419.html.
123

AUTHOR

125
126       Philip Hazel
127       University Computing Service
128       Cambridge CB2 3QH, England.
129

REVISION

131
132       Last updated: 09 July 2008
133       Copyright (c) 1997-2008 University of Cambridge.
134
135
136
137                                                                  PCRESTACK(3)
Impressum