1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
5 /* PCRE is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language.
8 Written by Philip Hazel
9 Original API code Copyright (c) 1997-2012 University of Cambridge
10 New API code Copyright (c) 2016 University of Cambridge
12 -----------------------------------------------------------------------------
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
16 * Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
19 * Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
23 * Neither the name of the University of Cambridge nor the names of its
24 contributors may be used to endorse or promote products derived from
25 this software without specific prior written permission.
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 POSSIBILITY OF SUCH DAMAGE.
38 -----------------------------------------------------------------------------
42 #ifndef INCLUDED_FROM_PCRE2_JIT_COMPILE
43 #error This file must be included from pcre2_jit_compile.c.
48 /*************************************************
49 * Free JIT read-only data *
50 *************************************************/
53 PRIV(jit_free_rodata)(void *current, void *allocator_data)
58 #else /* SUPPORT_JIT */
61 SLJIT_UNUSED_ARG(allocator_data);
63 while (current != NULL)
65 next = *(void**)current;
66 SLJIT_FREE(current, allocator_data);
70 #endif /* SUPPORT_JIT */
73 /*************************************************
74 * Free JIT compiled code *
75 *************************************************/
78 PRIV(jit_free)(void *executable_jit, pcre2_memctl *memctl)
83 #else /* SUPPORT_JIT */
85 executable_functions *functions = (executable_functions *)executable_jit;
86 void *allocator_data = memctl;
89 for (i = 0; i < JIT_NUMBER_OF_COMPILE_MODES; i++)
91 if (functions->executable_funcs[i] != NULL)
92 sljit_free_code(functions->executable_funcs[i]);
93 PRIV(jit_free_rodata)(functions->read_only_data_heads[i], allocator_data);
96 SLJIT_FREE(functions, allocator_data);
98 #endif /* SUPPORT_JIT */
102 /*************************************************
103 * Free unused JIT memory *
104 *************************************************/
106 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
107 pcre2_jit_free_unused_memory(pcre2_general_context *gcontext)
110 (void)gcontext; /* Suppress warning */
111 #else /* SUPPORT_JIT */
112 SLJIT_UNUSED_ARG(gcontext);
113 sljit_free_unused_memory_exec();
114 #endif /* SUPPORT_JIT */
119 /*************************************************
120 * Allocate a JIT stack *
121 *************************************************/
123 PCRE2_EXP_DEFN pcre2_jit_stack * PCRE2_CALL_CONVENTION
124 pcre2_jit_stack_create(size_t startsize, size_t maxsize,
125 pcre2_general_context *gcontext)
134 #else /* SUPPORT_JIT */
136 pcre2_jit_stack *jit_stack;
138 if (startsize < 1 || maxsize < 1)
140 if (startsize > maxsize)
142 startsize = (startsize + STACK_GROWTH_RATE - 1) & ~(STACK_GROWTH_RATE - 1);
143 maxsize = (maxsize + STACK_GROWTH_RATE - 1) & ~(STACK_GROWTH_RATE - 1);
145 jit_stack = PRIV(memctl_malloc)(sizeof(pcre2_real_jit_stack), (pcre2_memctl *)gcontext);
146 if (jit_stack == NULL) return NULL;
147 jit_stack->stack = sljit_allocate_stack(startsize, maxsize, &jit_stack->memctl);
154 /*************************************************
155 * Assign a JIT stack to a pattern *
156 *************************************************/
158 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
159 pcre2_jit_stack_assign(pcre2_match_context *mcontext, pcre2_jit_callback callback,
166 #else /* SUPPORT_JIT */
168 if (mcontext == NULL) return;
169 mcontext->jit_callback = callback;
170 mcontext->jit_callback_data = callback_data;
172 #endif /* SUPPORT_JIT */
176 /*************************************************
178 *************************************************/
180 PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
181 pcre2_jit_stack_free(pcre2_jit_stack *jit_stack)
185 #else /* SUPPORT_JIT */
186 if (jit_stack != NULL)
188 sljit_free_stack((struct sljit_stack *)(jit_stack->stack), &jit_stack->memctl);
189 jit_stack->memctl.free(jit_stack, jit_stack->memctl.memory_data);
191 #endif /* SUPPORT_JIT */
195 /*************************************************
196 * Get target CPU type *
197 *************************************************/
200 PRIV(jit_get_target)(void)
203 return "JIT is not supported";
204 #else /* SUPPORT_JIT */
205 return sljit_get_platform_name();
206 #endif /* SUPPORT_JIT */
210 /*************************************************
211 * Get size of JIT code *
212 *************************************************/
215 PRIV(jit_get_size)(void *executable_jit)
218 (void)executable_jit;
220 #else /* SUPPORT_JIT */
221 sljit_uw *executable_sizes = ((executable_functions *)executable_jit)->executable_sizes;
222 SLJIT_COMPILE_ASSERT(JIT_NUMBER_OF_COMPILE_MODES == 3, number_of_compile_modes_changed);
223 return executable_sizes[0] + executable_sizes[1] + executable_sizes[2];
227 /* End of pcre2_jit_misc.c */