Path: blob/master/thirdparty/pcre2/src/pcre2_jit_misc.c
9903 views
/*************************************************1* Perl-Compatible Regular Expressions *2*************************************************/34/* PCRE is a library of functions to support regular expressions whose syntax5and semantics are as close as possible to those of the Perl 5 language.67Written by Philip Hazel8Original API code Copyright (c) 1997-2012 University of Cambridge9New API code Copyright (c) 2016 University of Cambridge1011-----------------------------------------------------------------------------12Redistribution and use in source and binary forms, with or without13modification, are permitted provided that the following conditions are met:1415* Redistributions of source code must retain the above copyright notice,16this list of conditions and the following disclaimer.1718* Redistributions in binary form must reproduce the above copyright19notice, this list of conditions and the following disclaimer in the20documentation and/or other materials provided with the distribution.2122* Neither the name of the University of Cambridge nor the names of its23contributors may be used to endorse or promote products derived from24this software without specific prior written permission.2526THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE29ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE30LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR31CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF32SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS33INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN34CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)35ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE36POSSIBILITY OF SUCH DAMAGE.37-----------------------------------------------------------------------------38*/394041#ifndef INCLUDED_FROM_PCRE2_JIT_COMPILE42#error This file must be included from pcre2_jit_compile.c.43#endif44454647/*************************************************48* Free JIT read-only data *49*************************************************/5051void52PRIV(jit_free_rodata)(void *current, void *allocator_data)53{54#ifndef SUPPORT_JIT55(void)current;56(void)allocator_data;57#else /* SUPPORT_JIT */58void *next;5960SLJIT_UNUSED_ARG(allocator_data);6162while (current != NULL)63{64next = *(void**)current;65SLJIT_FREE(current, allocator_data);66current = next;67}6869#endif /* SUPPORT_JIT */70}7172/*************************************************73* Free JIT compiled code *74*************************************************/7576void77PRIV(jit_free)(void *executable_jit, pcre2_memctl *memctl)78{79#ifndef SUPPORT_JIT80(void)executable_jit;81(void)memctl;82#else /* SUPPORT_JIT */8384executable_functions *functions = (executable_functions *)executable_jit;85void *allocator_data = memctl;86int i;8788for (i = 0; i < JIT_NUMBER_OF_COMPILE_MODES; i++)89{90if (functions->executable_funcs[i] != NULL)91sljit_free_code(functions->executable_funcs[i], NULL);92PRIV(jit_free_rodata)(functions->read_only_data_heads[i], allocator_data);93}9495SLJIT_FREE(functions, allocator_data);9697#endif /* SUPPORT_JIT */98}99100101/*************************************************102* Free unused JIT memory *103*************************************************/104105PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION106pcre2_jit_free_unused_memory(pcre2_general_context *gcontext)107{108#ifndef SUPPORT_JIT109(void)gcontext; /* Suppress warning */110#else /* SUPPORT_JIT */111SLJIT_UNUSED_ARG(gcontext);112#if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)113sljit_free_unused_memory_exec();114#endif /* SLJIT_EXECUTABLE_ALLOCATOR */115#endif /* SUPPORT_JIT */116}117118119120/*************************************************121* Allocate a JIT stack *122*************************************************/123124PCRE2_EXP_DEFN pcre2_jit_stack * PCRE2_CALL_CONVENTION125pcre2_jit_stack_create(size_t startsize, size_t maxsize,126pcre2_general_context *gcontext)127{128#ifndef SUPPORT_JIT129130(void)gcontext;131(void)startsize;132(void)maxsize;133return NULL;134135#else /* SUPPORT_JIT */136137pcre2_jit_stack *jit_stack;138139if (startsize == 0 || maxsize == 0 || maxsize > SIZE_MAX - STACK_GROWTH_RATE)140return NULL;141if (startsize > maxsize)142startsize = maxsize;143startsize = (startsize + STACK_GROWTH_RATE - 1) & (size_t)(~(STACK_GROWTH_RATE - 1));144maxsize = (maxsize + STACK_GROWTH_RATE - 1) & (size_t)(~(STACK_GROWTH_RATE - 1));145146jit_stack = PRIV(memctl_malloc)(sizeof(pcre2_real_jit_stack), (pcre2_memctl *)gcontext);147if (jit_stack == NULL) return NULL;148jit_stack->stack = sljit_allocate_stack(startsize, maxsize, &jit_stack->memctl);149if (jit_stack->stack == NULL)150{151jit_stack->memctl.free(jit_stack, jit_stack->memctl.memory_data);152return NULL;153}154return jit_stack;155156#endif157}158159160/*************************************************161* Assign a JIT stack to a pattern *162*************************************************/163164PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION165pcre2_jit_stack_assign(pcre2_match_context *mcontext, pcre2_jit_callback callback,166void *callback_data)167{168#ifndef SUPPORT_JIT169(void)mcontext;170(void)callback;171(void)callback_data;172#else /* SUPPORT_JIT */173174if (mcontext == NULL) return;175mcontext->jit_callback = callback;176mcontext->jit_callback_data = callback_data;177178#endif /* SUPPORT_JIT */179}180181182/*************************************************183* Free a JIT stack *184*************************************************/185186PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION187pcre2_jit_stack_free(pcre2_jit_stack *jit_stack)188{189#ifndef SUPPORT_JIT190(void)jit_stack;191#else /* SUPPORT_JIT */192if (jit_stack != NULL)193{194sljit_free_stack((struct sljit_stack *)(jit_stack->stack), &jit_stack->memctl);195jit_stack->memctl.free(jit_stack, jit_stack->memctl.memory_data);196}197#endif /* SUPPORT_JIT */198}199200201/*************************************************202* Get target CPU type *203*************************************************/204205const char*206PRIV(jit_get_target)(void)207{208#ifndef SUPPORT_JIT209return "JIT is not supported";210#else /* SUPPORT_JIT */211return sljit_get_platform_name();212#endif /* SUPPORT_JIT */213}214215216/*************************************************217* Get size of JIT code *218*************************************************/219220size_t221PRIV(jit_get_size)(void *executable_jit)222{223#ifndef SUPPORT_JIT224(void)executable_jit;225return 0;226#else /* SUPPORT_JIT */227sljit_uw *executable_sizes = ((executable_functions *)executable_jit)->executable_sizes;228SLJIT_COMPILE_ASSERT(JIT_NUMBER_OF_COMPILE_MODES == 3, number_of_compile_modes_changed);229return executable_sizes[0] + executable_sizes[1] + executable_sizes[2];230#endif231}232233/* End of pcre2_jit_misc.c */234235236