/[pcre]/code/trunk/doc/pcrejit.3
ViewVC logotype

Diff of /code/trunk/doc/pcrejit.3

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 788 by ph10, Tue Dec 6 15:38:01 2011 UTC revision 835 by ph10, Wed Dec 28 16:10:09 2011 UTC
# Line 34  The Power PC support is designated as ex Line 34  The Power PC support is designated as ex
34  fully tested. If --enable-jit is set on an unsupported platform, compilation  fully tested. If --enable-jit is set on an unsupported platform, compilation
35  fails.  fails.
36  .P  .P
37  A program that is linked with PCRE 8.20 or later can tell if JIT support is  A program can tell if JIT support is available by calling \fBpcre_config()\fP
38  available by calling \fBpcre_config()\fP with the PCRE_CONFIG_JIT option. The  with the PCRE_CONFIG_JIT option. The result is 1 when JIT is available, and 0
39  result is 1 when JIT is available, and 0 otherwise. However, a simple program  otherwise. However, a simple program does not need to check this in order to
40  does not need to check this in order to use JIT. The API is implemented in a  use JIT. The API is implemented in a way that falls back to the ordinary PCRE
41  way that falls back to the ordinary PCRE code if JIT is not available.  code if JIT is not available.
 .P  
 If your program may sometimes be linked with versions of PCRE that are older  
 than 8.20, but you want to use JIT when it is available, you can test  
 the values of PCRE_MAJOR and PCRE_MINOR, or the existence of a JIT macro such  
 as PCRE_CONFIG_JIT, for compile-time control of your code.  
42  .  .
43  .  .
44  .SH "SIMPLE USE OF JIT"  .SH "SIMPLE USE OF JIT"
# Line 59  You have to do two things to make use of Line 54  You have to do two things to make use of
54        no longer needed instead of just freeing it yourself. This        no longer needed instead of just freeing it yourself. This
55        ensures that any JIT data is also freed.        ensures that any JIT data is also freed.
56  .sp  .sp
 For a program that may be linked with pre-8.20 versions of PCRE, you can insert  
 .sp  
   #ifndef PCRE_STUDY_JIT_COMPILE  
   #define PCRE_STUDY_JIT_COMPILE 0  
   #endif  
 .sp  
 so that no option is passed to \fBpcre_study()\fP, and then use something like  
 this to free the study data:  
 .sp  
   #ifdef PCRE_CONFIG_JIT  
       pcre_free_study(study_ptr);  
   #else  
       pcre_free(study_ptr);  
   #endif  
 .sp  
57  In some circumstances you may need to call additional functions. These are  In some circumstances you may need to call additional functions. These are
58  described in the section entitled  described in the section entitled
59  .\" HTML <a href="#stackcontrol">  .\" HTML <a href="#stackcontrol">
# Line 115  supported. Line 95  supported.
95  .P  .P
96  The unsupported pattern items are:  The unsupported pattern items are:
97  .sp  .sp
98    \eC             match a single byte; not supported in UTF-8 mode    \eC            match a single byte; not supported in UTF-8 mode
99    (?Cn)          callouts    (?Cn)          callouts
100    (*COMMIT)      )    (*COMMIT)      )
101    (*MARK)        )    (*MARK)        )
# Line 173  When the compiled JIT code runs, it need Line 153  When the compiled JIT code runs, it need
153  By default, it uses 32K on the machine stack. However, some large or  By default, it uses 32K on the machine stack. However, some large or
154  complicated patterns need more than this. The error PCRE_ERROR_JIT_STACKLIMIT  complicated patterns need more than this. The error PCRE_ERROR_JIT_STACKLIMIT
155  is given when there is not enough stack. Three functions are provided for  is given when there is not enough stack. Three functions are provided for
156  managing blocks of memory for use as JIT stacks. There is further discussion  managing blocks of memory for use as JIT stacks.
 about the use of JIT stacks in the section entitled  
 .\" HTML <a href="#stackcontrol">  
 .\" </a>  
 "JIT stack FAQ"  
 .\"  
 below.  
157  .P  .P
158  The \fBpcre_jit_stack_alloc()\fP function creates a JIT stack. Its arguments  The \fBpcre_jit_stack_alloc()\fP function creates a JIT stack. Its arguments
159  are a starting size and a maximum size, and it returns a pointer to an opaque  are a starting size and a maximum size, and it returns a pointer to an opaque
# Line 243  is non-NULL and points to a \fBpcre_extr Line 217  is non-NULL and points to a \fBpcre_extr
217  successful study with PCRE_STUDY_JIT_COMPILE.  successful study with PCRE_STUDY_JIT_COMPILE.
218  .  .
219  .  .
 .\" HTML <a name="stackfaq"></a>  
 .SH "JIT STACK FAQ"  
 .rs  
 .sp  
 (1) Why do we need JIT stacks?  
 .sp  
 PCRE (and JIT) is a recursive, depth-first engine, so it needs a stack where  
 the local data of the current node is pushed before checking its child nodes.  
 Allocating real machine stack on some platforms is difficult. For example, the  
 stack chain needs to be updated every time if we extend the stack on PowerPC.  
 Although it is possible, its updating time overhead decreases performance. So  
 we do the recursion in memory.  
 .P  
 (2) Why don't we simply allocate blocks of memory with \fBmalloc()\fP?  
 .sp  
 Modern operating systems have a nice feature: they can reserve an address space  
 instead of allocating memory. We can safely allocate memory pages inside this  
 address space, so the stack could grow without moving memory data (this is  
 important because of pointers). Thus we can allocate 1M address space, and use  
 only a single memory page (usually 4K) if that is enough. However, we can still  
 grow up to 1M anytime if needed.  
 .P  
 (3) Who "owns" a JIT stack?  
 .sp  
 The owner of the stack is the user program, not the JIT studied pattern or  
 anything else. The user program must ensure that if a stack is used by  
 \fBpcre_exec()\fP, (that is, it is assigned to the pattern currently running),  
 that stack must not be used by any other threads (to avoid overwriting the same  
 memory area). The best practice for multithreaded programs is to allocate a  
 stack for each thread, and return this stack through the JIT callback function.  
 .P  
 (4) When should a JIT stack be freed?  
 .sp  
 You can free a JIT stack at any time, as long as it will not be used by  
 \fBpcre_exec()\fP again. When you assign the stack to a pattern, only a pointer  
 is set. There is no reference counting or any other magic. You can free the  
 patterns and stacks in any order, anytime. Just \fIdo not\fP call  
 \fBpcre_exec()\fP with a pattern pointing to an already freed stack, as that  
 will cause SEGFAULT. (Also, do not free a stack currently used by  
 \fBpcre_exec()\fP in another thread). You can also replace the stack for a  
 pattern at any time. You can even free the previous stack before assigning a  
 replacement.  
 .P  
 (5) Should I allocate/free a stack every time before/after calling  
 \fBpcre_exec()\fP?  
 .sp  
 No, because this is too costly in terms of resources. However, you could  
 implement some clever idea which release the stack if it is not used in let's  
 say two minutes. The JIT callback can help to achive this without keeping a  
 list of the currently JIT studied patterns.  
 .P  
 (6) OK, the stack is for long term memory allocation. But what happens if a  
 pattern causes stack overflow with a stack of 1M? Is that 1M kept until the  
 stack is freed?  
 .sp  
 Especially on embedded sytems, it might be a good idea to release  
 memory sometimes without freeing the stack. There is no API for this at the  
 moment. Probably a function call which returns with the currently allocated  
 memory for any stack and another which allows releasing memory (shrinking the  
 stack) would be a good idea if someone needs this.  
 .P  
 (7) This is too much of a headache. Isn't there any better solution for JIT  
 stack handling?  
 .sp  
 No, thanks to Windows. If POSIX threads were used everywhere, we could throw  
 out this complicated API.  
 .  
 .  
220  .SH "EXAMPLE CODE"  .SH "EXAMPLE CODE"
221  .rs  .rs
222  .sp  .sp
# Line 347  callback. Line 253  callback.
253  .rs  .rs
254  .sp  .sp
255  .nf  .nf
256  Philip Hazel (FAQ by Zoltan Herczeg)  Philip Hazel
257  University Computing Service  University Computing Service
258  Cambridge CB2 3QH, England.  Cambridge CB2 3QH, England.
259  .fi  .fi
# Line 357  Cambridge CB2 3QH, England. Line 263  Cambridge CB2 3QH, England.
263  .rs  .rs
264  .sp  .sp
265  .nf  .nf
266  Last updated: 26 November 2011  Last updated: 15 November 2011
267  Copyright (c) 1997-2011 University of Cambridge.  Copyright (c) 1997-2011 University of Cambridge.
268  .fi  .fi

Legend:
Removed from v.788  
changed lines
  Added in v.835

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12