| 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 can tell if JIT support is available by calling \fBpcre_config()\fP |
A program that is linked with PCRE 8.20 or later can tell if JIT support is |
| 38 |
with the PCRE_CONFIG_JIT option. The result is 1 when JIT is available, and 0 |
available by calling \fBpcre_config()\fP with the PCRE_CONFIG_JIT option. The |
| 39 |
otherwise. However, a simple program does not need to check this in order to |
result is 1 when JIT is available, and 0 otherwise. However, a simple program |
| 40 |
use JIT. The API is implemented in a way that falls back to the ordinary PCRE |
does not need to check this in order to use JIT. The API is implemented in a |
| 41 |
code if JIT is not available. |
way that falls back to the ordinary PCRE code if JIT is not available. |
| 42 |
|
.P |
| 43 |
|
If your program may sometimes be linked with versions of PCRE that are older |
| 44 |
|
than 8.20, but you want to use JIT when it is available, you can test |
| 45 |
|
the values of PCRE_MAJOR and PCRE_MINOR, or the existence of a JIT macro such |
| 46 |
|
as PCRE_CONFIG_JIT, for compile-time control of your code. |
| 47 |
. |
. |
| 48 |
. |
. |
| 49 |
.SH "SIMPLE USE OF JIT" |
.SH "SIMPLE USE OF JIT" |
| 59 |
no longer needed instead of just freeing it yourself. This |
no longer needed instead of just freeing it yourself. This |
| 60 |
ensures that any JIT data is also freed. |
ensures that any JIT data is also freed. |
| 61 |
.sp |
.sp |
| 62 |
|
For a program that may be linked with pre-8.20 versions of PCRE, you can insert |
| 63 |
|
.sp |
| 64 |
|
#ifndef PCRE_STUDY_JIT_COMPILE |
| 65 |
|
#define PCRE_STUDY_JIT_COMPILE 0 |
| 66 |
|
#endif |
| 67 |
|
.sp |
| 68 |
|
so that no option is passed to \fBpcre_study()\fP, and then use something like |
| 69 |
|
this to free the study data: |
| 70 |
|
.sp |
| 71 |
|
#ifdef PCRE_CONFIG_JIT |
| 72 |
|
pcre_free_study(study_ptr); |
| 73 |
|
#else |
| 74 |
|
pcre_free(study_ptr); |
| 75 |
|
#endif |
| 76 |
|
.sp |
| 77 |
In some circumstances you may need to call additional functions. These are |
In some circumstances you may need to call additional functions. These are |
| 78 |
described in the section entitled |
described in the section entitled |
| 79 |
.\" HTML <a href="#stackcontrol"> |
.\" HTML <a href="#stackcontrol"> |
| 115 |
.P |
.P |
| 116 |
The unsupported pattern items are: |
The unsupported pattern items are: |
| 117 |
.sp |
.sp |
| 118 |
\eC match a single byte; not supported in UTF-8 mode |
\eC match a single byte; not supported in UTF-8 mode |
| 119 |
(?Cn) callouts |
(?Cn) callouts |
| 120 |
(*COMMIT) ) |
(*COMMIT) ) |
| 121 |
(*MARK) ) |
(*MARK) ) |
| 173 |
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 |
| 174 |
complicated patterns need more than this. The error PCRE_ERROR_JIT_STACKLIMIT |
complicated patterns need more than this. The error PCRE_ERROR_JIT_STACKLIMIT |
| 175 |
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 |
| 176 |
managing blocks of memory for use as JIT stacks. |
managing blocks of memory for use as JIT stacks. There is further discussion |
| 177 |
|
about the use of JIT stacks in the section entitled |
| 178 |
|
.\" HTML <a href="#stackcontrol"> |
| 179 |
|
.\" </a> |
| 180 |
|
"JIT stack FAQ" |
| 181 |
|
.\" |
| 182 |
|
below. |
| 183 |
.P |
.P |
| 184 |
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 |
| 185 |
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 |
| 243 |
successful study with PCRE_STUDY_JIT_COMPILE. |
successful study with PCRE_STUDY_JIT_COMPILE. |
| 244 |
. |
. |
| 245 |
. |
. |
| 246 |
|
.\" HTML <a name="stackfaq"></a> |
| 247 |
|
.SH "JIT STACK FAQ" |
| 248 |
|
.rs |
| 249 |
|
.sp |
| 250 |
|
(1) Why do we need JIT stacks? |
| 251 |
|
.sp |
| 252 |
|
PCRE (and JIT) is a recursive, depth-first engine, so it needs a stack where |
| 253 |
|
the local data of the current node is pushed before checking its child nodes. |
| 254 |
|
Allocating real machine stack on some platforms is difficult. For example, the |
| 255 |
|
stack chain needs to be updated every time if we extend the stack on PowerPC. |
| 256 |
|
Although it is possible, its updating time overhead decreases performance. So |
| 257 |
|
we do the recursion in memory. |
| 258 |
|
.P |
| 259 |
|
(2) Why don't we simply allocate blocks of memory with \fBmalloc()\fP? |
| 260 |
|
.sp |
| 261 |
|
Modern operating systems have a nice feature: they can reserve an address space |
| 262 |
|
instead of allocating memory. We can safely allocate memory pages inside this |
| 263 |
|
address space, so the stack could grow without moving memory data (this is |
| 264 |
|
important because of pointers). Thus we can allocate 1M address space, and use |
| 265 |
|
only a single memory page (usually 4K) if that is enough. However, we can still |
| 266 |
|
grow up to 1M anytime if needed. |
| 267 |
|
.P |
| 268 |
|
(3) Who "owns" a JIT stack? |
| 269 |
|
.sp |
| 270 |
|
The owner of the stack is the user program, not the JIT studied pattern or |
| 271 |
|
anything else. The user program must ensure that if a stack is used by |
| 272 |
|
\fBpcre_exec()\fP, (that is, it is assigned to the pattern currently running), |
| 273 |
|
that stack must not be used by any other threads (to avoid overwriting the same |
| 274 |
|
memory area). The best practice for multithreaded programs is to allocate a |
| 275 |
|
stack for each thread, and return this stack through the JIT callback function. |
| 276 |
|
.P |
| 277 |
|
(4) When should a JIT stack be freed? |
| 278 |
|
.sp |
| 279 |
|
You can free a JIT stack at any time, as long as it will not be used by |
| 280 |
|
\fBpcre_exec()\fP again. When you assign the stack to a pattern, only a pointer |
| 281 |
|
is set. There is no reference counting or any other magic. You can free the |
| 282 |
|
patterns and stacks in any order, anytime. Just \fIdo not\fP call |
| 283 |
|
\fBpcre_exec()\fP with a pattern pointing to an already freed stack, as that |
| 284 |
|
will cause SEGFAULT. (Also, do not free a stack currently used by |
| 285 |
|
\fBpcre_exec()\fP in another thread). You can also replace the stack for a |
| 286 |
|
pattern at any time. You can even free the previous stack before assigning a |
| 287 |
|
replacement. |
| 288 |
|
.P |
| 289 |
|
(5) Should I allocate/free a stack every time before/after calling |
| 290 |
|
\fBpcre_exec()\fP? |
| 291 |
|
.sp |
| 292 |
|
No, because this is too costly in terms of resources. However, you could |
| 293 |
|
implement some clever idea which release the stack if it is not used in let's |
| 294 |
|
say two minutes. The JIT callback can help to achive this without keeping a |
| 295 |
|
list of the currently JIT studied patterns. |
| 296 |
|
.P |
| 297 |
|
(6) OK, the stack is for long term memory allocation. But what happens if a |
| 298 |
|
pattern causes stack overflow with a stack of 1M? Is that 1M kept until the |
| 299 |
|
stack is freed? |
| 300 |
|
.sp |
| 301 |
|
Especially on embedded sytems, it might be a good idea to release |
| 302 |
|
memory sometimes without freeing the stack. There is no API for this at the |
| 303 |
|
moment. Probably a function call which returns with the currently allocated |
| 304 |
|
memory for any stack and another which allows releasing memory (shrinking the |
| 305 |
|
stack) would be a good idea if someone needs this. |
| 306 |
|
.P |
| 307 |
|
(7) This is too much of a headache. Isn't there any better solution for JIT |
| 308 |
|
stack handling? |
| 309 |
|
.sp |
| 310 |
|
No, thanks to Windows. If POSIX threads were used everywhere, we could throw |
| 311 |
|
out this complicated API. |
| 312 |
|
. |
| 313 |
|
. |
| 314 |
.SH "EXAMPLE CODE" |
.SH "EXAMPLE CODE" |
| 315 |
.rs |
.rs |
| 316 |
.sp |
.sp |
| 347 |
.rs |
.rs |
| 348 |
.sp |
.sp |
| 349 |
.nf |
.nf |
| 350 |
Philip Hazel |
Philip Hazel (FAQ by Zoltan Herczeg) |
| 351 |
University Computing Service |
University Computing Service |
| 352 |
Cambridge CB2 3QH, England. |
Cambridge CB2 3QH, England. |
| 353 |
.fi |
.fi |
| 357 |
.rs |
.rs |
| 358 |
.sp |
.sp |
| 359 |
.nf |
.nf |
| 360 |
Last updated: 15 November 2011 |
Last updated: 26 November 2011 |
| 361 |
Copyright (c) 1997-2011 University of Cambridge. |
Copyright (c) 1997-2011 University of Cambridge. |
| 362 |
.fi |
.fi |