sljitUtils.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Stack-less Just-In-Time compiler
  3. *
  4. * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification, are
  7. * permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this list of
  10. * conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  13. * of conditions and the following disclaimer in the documentation and/or other materials
  14. * provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  21. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  22. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  24. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /* ------------------------------------------------------------------------ */
  27. /* Locks */
  28. /* ------------------------------------------------------------------------ */
  29. #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR) || (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
  30. #if (defined SLJIT_SINGLE_THREADED && SLJIT_SINGLE_THREADED)
  31. #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
  32. static SLJIT_INLINE void allocator_grab_lock(void)
  33. {
  34. /* Always successful. */
  35. }
  36. static SLJIT_INLINE void allocator_release_lock(void)
  37. {
  38. /* Always successful. */
  39. }
  40. #endif /* SLJIT_EXECUTABLE_ALLOCATOR */
  41. #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
  42. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_grab_lock(void)
  43. {
  44. /* Always successful. */
  45. }
  46. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_release_lock(void)
  47. {
  48. /* Always successful. */
  49. }
  50. #endif /* SLJIT_UTIL_GLOBAL_LOCK */
  51. #elif defined(_WIN32) /* SLJIT_SINGLE_THREADED */
  52. #include "windows.h"
  53. #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
  54. static HANDLE allocator_mutex = 0;
  55. static SLJIT_INLINE void allocator_grab_lock(void)
  56. {
  57. /* No idea what to do if an error occures. Static mutexes should never fail... */
  58. if (!allocator_mutex)
  59. allocator_mutex = CreateMutex(NULL, TRUE, NULL);
  60. else
  61. WaitForSingleObject(allocator_mutex, INFINITE);
  62. }
  63. static SLJIT_INLINE void allocator_release_lock(void)
  64. {
  65. ReleaseMutex(allocator_mutex);
  66. }
  67. #endif /* SLJIT_EXECUTABLE_ALLOCATOR */
  68. #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
  69. static HANDLE global_mutex = 0;
  70. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_grab_lock(void)
  71. {
  72. /* No idea what to do if an error occures. Static mutexes should never fail... */
  73. if (!global_mutex)
  74. global_mutex = CreateMutex(NULL, TRUE, NULL);
  75. else
  76. WaitForSingleObject(global_mutex, INFINITE);
  77. }
  78. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_release_lock(void)
  79. {
  80. ReleaseMutex(global_mutex);
  81. }
  82. #endif /* SLJIT_UTIL_GLOBAL_LOCK */
  83. #else /* _WIN32 */
  84. #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
  85. #include <pthread.h>
  86. static pthread_mutex_t allocator_mutex = PTHREAD_MUTEX_INITIALIZER;
  87. static SLJIT_INLINE void allocator_grab_lock(void)
  88. {
  89. pthread_mutex_lock(&allocator_mutex);
  90. }
  91. static SLJIT_INLINE void allocator_release_lock(void)
  92. {
  93. pthread_mutex_unlock(&allocator_mutex);
  94. }
  95. #endif /* SLJIT_EXECUTABLE_ALLOCATOR */
  96. #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
  97. #include <pthread.h>
  98. static pthread_mutex_t global_mutex = PTHREAD_MUTEX_INITIALIZER;
  99. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_grab_lock(void)
  100. {
  101. pthread_mutex_lock(&global_mutex);
  102. }
  103. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_release_lock(void)
  104. {
  105. pthread_mutex_unlock(&global_mutex);
  106. }
  107. #endif /* SLJIT_UTIL_GLOBAL_LOCK */
  108. #endif /* _WIN32 */
  109. /* ------------------------------------------------------------------------ */
  110. /* Stack */
  111. /* ------------------------------------------------------------------------ */
  112. #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) || (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
  113. #ifdef _WIN32
  114. #include "windows.h"
  115. #else
  116. /* Provides mmap function. */
  117. #include <sys/types.h>
  118. #include <sys/mman.h>
  119. #ifndef MAP_ANON
  120. #ifdef MAP_ANONYMOUS
  121. #define MAP_ANON MAP_ANONYMOUS
  122. #endif
  123. #endif
  124. /* For detecting the page size. */
  125. #include <unistd.h>
  126. #ifndef MAP_ANON
  127. #include <fcntl.h>
  128. /* Some old systems does not have MAP_ANON. */
  129. static sljit_s32 dev_zero = -1;
  130. #if (defined SLJIT_SINGLE_THREADED && SLJIT_SINGLE_THREADED)
  131. static SLJIT_INLINE sljit_s32 open_dev_zero(void)
  132. {
  133. dev_zero = open("/dev/zero", O_RDWR);
  134. return dev_zero < 0;
  135. }
  136. #else /* SLJIT_SINGLE_THREADED */
  137. #include <pthread.h>
  138. static pthread_mutex_t dev_zero_mutex = PTHREAD_MUTEX_INITIALIZER;
  139. static SLJIT_INLINE sljit_s32 open_dev_zero(void)
  140. {
  141. pthread_mutex_lock(&dev_zero_mutex);
  142. /* The dev_zero might be initialized by another thread during the waiting. */
  143. if (dev_zero < 0) {
  144. dev_zero = open("/dev/zero", O_RDWR);
  145. }
  146. pthread_mutex_unlock(&dev_zero_mutex);
  147. return dev_zero < 0;
  148. }
  149. #endif /* SLJIT_SINGLE_THREADED */
  150. #endif
  151. #endif
  152. #endif /* SLJIT_UTIL_STACK || SLJIT_EXECUTABLE_ALLOCATOR */
  153. #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
  154. /* Planning to make it even more clever in the future. */
  155. static sljit_sw sljit_page_align = 0;
  156. SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_FUNC sljit_allocate_stack(sljit_uw start_size, sljit_uw max_size, void *allocator_data)
  157. {
  158. struct sljit_stack *stack;
  159. void *ptr;
  160. #ifdef _WIN32
  161. SYSTEM_INFO si;
  162. #endif
  163. SLJIT_UNUSED_ARG(allocator_data);
  164. if (start_size > max_size || start_size < 1)
  165. return NULL;
  166. #ifdef _WIN32
  167. if (!sljit_page_align) {
  168. GetSystemInfo(&si);
  169. sljit_page_align = si.dwPageSize - 1;
  170. }
  171. #else
  172. if (!sljit_page_align) {
  173. sljit_page_align = sysconf(_SC_PAGESIZE);
  174. /* Should never happen. */
  175. if (sljit_page_align < 0)
  176. sljit_page_align = 4096;
  177. sljit_page_align--;
  178. }
  179. #endif
  180. stack = (struct sljit_stack*)SLJIT_MALLOC(sizeof(struct sljit_stack), allocator_data);
  181. if (!stack)
  182. return NULL;
  183. /* Align max_size. */
  184. max_size = (max_size + sljit_page_align) & ~sljit_page_align;
  185. #ifdef _WIN32
  186. ptr = VirtualAlloc(NULL, max_size, MEM_RESERVE, PAGE_READWRITE);
  187. if (!ptr) {
  188. SLJIT_FREE(stack, allocator_data);
  189. return NULL;
  190. }
  191. stack->min_start = (sljit_u8 *)ptr;
  192. stack->end = stack->min_start + max_size;
  193. stack->start = stack->end;
  194. if (sljit_stack_resize(stack, stack->end - start_size) == NULL) {
  195. sljit_free_stack(stack, allocator_data);
  196. return NULL;
  197. }
  198. #else
  199. #ifdef MAP_ANON
  200. ptr = mmap(NULL, max_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
  201. #else
  202. if (dev_zero < 0) {
  203. if (open_dev_zero()) {
  204. SLJIT_FREE(stack, allocator_data);
  205. return NULL;
  206. }
  207. }
  208. ptr = mmap(NULL, max_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, dev_zero, 0);
  209. #endif
  210. if (ptr == MAP_FAILED) {
  211. SLJIT_FREE(stack, allocator_data);
  212. return NULL;
  213. }
  214. stack->min_start = (sljit_u8 *)ptr;
  215. stack->end = stack->min_start + max_size;
  216. stack->start = stack->end - start_size;
  217. #endif
  218. stack->top = stack->end;
  219. return stack;
  220. }
  221. #undef PAGE_ALIGN
  222. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_free_stack(struct sljit_stack *stack, void *allocator_data)
  223. {
  224. SLJIT_UNUSED_ARG(allocator_data);
  225. #ifdef _WIN32
  226. VirtualFree((void*)stack->min_start, 0, MEM_RELEASE);
  227. #else
  228. munmap((void*)stack->min_start, stack->end - stack->min_start);
  229. #endif
  230. SLJIT_FREE(stack, allocator_data);
  231. }
  232. SLJIT_API_FUNC_ATTRIBUTE sljit_u8 *SLJIT_FUNC sljit_stack_resize(struct sljit_stack *stack, sljit_u8 *new_start)
  233. {
  234. sljit_uw aligned_old_start;
  235. sljit_uw aligned_new_start;
  236. if ((new_start < stack->min_start) || (new_start >= stack->end))
  237. return NULL;
  238. #ifdef _WIN32
  239. aligned_new_start = (sljit_uw)new_start & ~sljit_page_align;
  240. aligned_old_start = ((sljit_uw)stack->start) & ~sljit_page_align;
  241. if (aligned_new_start != aligned_old_start) {
  242. if (aligned_new_start < aligned_old_start) {
  243. if (!VirtualAlloc((void*)aligned_new_start, aligned_old_start - aligned_new_start, MEM_COMMIT, PAGE_READWRITE))
  244. return NULL;
  245. }
  246. else {
  247. if (!VirtualFree((void*)aligned_old_start, aligned_new_start - aligned_old_start, MEM_DECOMMIT))
  248. return NULL;
  249. }
  250. }
  251. #else
  252. if (stack->start < new_start) {
  253. aligned_new_start = (sljit_uw)new_start & ~sljit_page_align;
  254. aligned_old_start = ((sljit_uw)stack->start) & ~sljit_page_align;
  255. /* If madvise is available, we release the unnecessary space. */
  256. #if defined(MADV_DONTNEED)
  257. if (aligned_new_start > aligned_old_start)
  258. madvise((void*)aligned_old_start, aligned_new_start - aligned_old_start, MADV_DONTNEED);
  259. #elif defined(POSIX_MADV_DONTNEED)
  260. if (aligned_new_start > aligned_old_start)
  261. posix_madvise((void*)aligned_old_start, aligned_new_start - aligned_old_start, POSIX_MADV_DONTNEED);
  262. #endif
  263. }
  264. #endif
  265. stack->start = new_start;
  266. return new_start;
  267. }
  268. #endif /* SLJIT_UTIL_STACK */
  269. #endif