pcre2_jit_misc.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Original API code Copyright (c) 1997-2012 University of Cambridge
  8. New API code Copyright (c) 2016 University of Cambridge
  9. -----------------------------------------------------------------------------
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. * Neither the name of the University of Cambridge nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31. -----------------------------------------------------------------------------
  32. */
  33. #ifndef INCLUDED_FROM_PCRE2_JIT_COMPILE
  34. #error This file must be included from pcre2_jit_compile.c.
  35. #endif
  36. /*************************************************
  37. * Free JIT read-only data *
  38. *************************************************/
  39. void
  40. PRIV(jit_free_rodata)(void *current, void *allocator_data)
  41. {
  42. #ifndef SUPPORT_JIT
  43. (void)current;
  44. (void)allocator_data;
  45. #else /* SUPPORT_JIT */
  46. void *next;
  47. SLJIT_UNUSED_ARG(allocator_data);
  48. while (current != NULL)
  49. {
  50. next = *(void**)current;
  51. SLJIT_FREE(current, allocator_data);
  52. current = next;
  53. }
  54. #endif /* SUPPORT_JIT */
  55. }
  56. /*************************************************
  57. * Free JIT compiled code *
  58. *************************************************/
  59. void
  60. PRIV(jit_free)(void *executable_jit, pcre2_memctl *memctl)
  61. {
  62. #ifndef SUPPORT_JIT
  63. (void)executable_jit;
  64. (void)memctl;
  65. #else /* SUPPORT_JIT */
  66. executable_functions *functions = (executable_functions *)executable_jit;
  67. void *allocator_data = memctl;
  68. int i;
  69. for (i = 0; i < JIT_NUMBER_OF_COMPILE_MODES; i++)
  70. {
  71. if (functions->executable_funcs[i] != NULL)
  72. sljit_free_code(functions->executable_funcs[i], NULL);
  73. PRIV(jit_free_rodata)(functions->read_only_data_heads[i], allocator_data);
  74. }
  75. SLJIT_FREE(functions, allocator_data);
  76. #endif /* SUPPORT_JIT */
  77. }
  78. /*************************************************
  79. * Free unused JIT memory *
  80. *************************************************/
  81. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  82. pcre2_jit_free_unused_memory(pcre2_general_context *gcontext)
  83. {
  84. #ifndef SUPPORT_JIT
  85. (void)gcontext; /* Suppress warning */
  86. #else /* SUPPORT_JIT */
  87. SLJIT_UNUSED_ARG(gcontext);
  88. sljit_free_unused_memory_exec();
  89. #endif /* SUPPORT_JIT */
  90. }
  91. /*************************************************
  92. * Allocate a JIT stack *
  93. *************************************************/
  94. PCRE2_EXP_DEFN pcre2_jit_stack * PCRE2_CALL_CONVENTION
  95. pcre2_jit_stack_create(size_t startsize, size_t maxsize,
  96. pcre2_general_context *gcontext)
  97. {
  98. #ifndef SUPPORT_JIT
  99. (void)gcontext;
  100. (void)startsize;
  101. (void)maxsize;
  102. return NULL;
  103. #else /* SUPPORT_JIT */
  104. pcre2_jit_stack *jit_stack;
  105. if (startsize < 1 || maxsize < 1)
  106. return NULL;
  107. if (startsize > maxsize)
  108. startsize = maxsize;
  109. startsize = (startsize + STACK_GROWTH_RATE - 1) & ~(STACK_GROWTH_RATE - 1);
  110. maxsize = (maxsize + STACK_GROWTH_RATE - 1) & ~(STACK_GROWTH_RATE - 1);
  111. jit_stack = PRIV(memctl_malloc)(sizeof(pcre2_real_jit_stack), (pcre2_memctl *)gcontext);
  112. if (jit_stack == NULL) return NULL;
  113. jit_stack->stack = sljit_allocate_stack(startsize, maxsize, &jit_stack->memctl);
  114. if (jit_stack->stack == NULL)
  115. {
  116. jit_stack->memctl.free(jit_stack, jit_stack->memctl.memory_data);
  117. return NULL;
  118. }
  119. return jit_stack;
  120. #endif
  121. }
  122. /*************************************************
  123. * Assign a JIT stack to a pattern *
  124. *************************************************/
  125. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  126. pcre2_jit_stack_assign(pcre2_match_context *mcontext, pcre2_jit_callback callback,
  127. void *callback_data)
  128. {
  129. #ifndef SUPPORT_JIT
  130. (void)mcontext;
  131. (void)callback;
  132. (void)callback_data;
  133. #else /* SUPPORT_JIT */
  134. if (mcontext == NULL) return;
  135. mcontext->jit_callback = callback;
  136. mcontext->jit_callback_data = callback_data;
  137. #endif /* SUPPORT_JIT */
  138. }
  139. /*************************************************
  140. * Free a JIT stack *
  141. *************************************************/
  142. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  143. pcre2_jit_stack_free(pcre2_jit_stack *jit_stack)
  144. {
  145. #ifndef SUPPORT_JIT
  146. (void)jit_stack;
  147. #else /* SUPPORT_JIT */
  148. if (jit_stack != NULL)
  149. {
  150. sljit_free_stack((struct sljit_stack *)(jit_stack->stack), &jit_stack->memctl);
  151. jit_stack->memctl.free(jit_stack, jit_stack->memctl.memory_data);
  152. }
  153. #endif /* SUPPORT_JIT */
  154. }
  155. /*************************************************
  156. * Get target CPU type *
  157. *************************************************/
  158. const char*
  159. PRIV(jit_get_target)(void)
  160. {
  161. #ifndef SUPPORT_JIT
  162. return "JIT is not supported";
  163. #else /* SUPPORT_JIT */
  164. return sljit_get_platform_name();
  165. #endif /* SUPPORT_JIT */
  166. }
  167. /*************************************************
  168. * Get size of JIT code *
  169. *************************************************/
  170. size_t
  171. PRIV(jit_get_size)(void *executable_jit)
  172. {
  173. #ifndef SUPPORT_JIT
  174. (void)executable_jit;
  175. return 0;
  176. #else /* SUPPORT_JIT */
  177. sljit_uw *executable_sizes = ((executable_functions *)executable_jit)->executable_sizes;
  178. SLJIT_COMPILE_ASSERT(JIT_NUMBER_OF_COMPILE_MODES == 3, number_of_compile_modes_changed);
  179. return executable_sizes[0] + executable_sizes[1] + executable_sizes[2];
  180. #endif
  181. }
  182. /* End of pcre2_jit_misc.c */