2 * Stack-less Just-In-Time compiler
4 * Copyright 2009-2012 Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
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.
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.
27 /* ------------------------------------------------------------------------ */
29 /* ------------------------------------------------------------------------ */
31 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR) || (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
37 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
39 static HANDLE allocator_mutex = 0;
41 static SLJIT_INLINE void allocator_grab_lock(void)
43 /* No idea what to do if an error occures. Static mutexes should never fail... */
45 allocator_mutex = CreateMutex(NULL, TRUE, NULL);
47 WaitForSingleObject(allocator_mutex, INFINITE);
50 static SLJIT_INLINE void allocator_release_lock(void)
52 ReleaseMutex(allocator_mutex);
55 #endif /* SLJIT_EXECUTABLE_ALLOCATOR */
57 #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
59 static HANDLE global_mutex = 0;
61 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_grab_lock(void)
63 /* No idea what to do if an error occures. Static mutexes should never fail... */
65 global_mutex = CreateMutex(NULL, TRUE, NULL);
67 WaitForSingleObject(global_mutex, INFINITE);
70 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_release_lock(void)
72 ReleaseMutex(global_mutex);
75 #endif /* SLJIT_UTIL_GLOBAL_LOCK */
81 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
83 static pthread_mutex_t allocator_mutex = PTHREAD_MUTEX_INITIALIZER;
85 static SLJIT_INLINE void allocator_grab_lock(void)
87 pthread_mutex_lock(&allocator_mutex);
90 static SLJIT_INLINE void allocator_release_lock(void)
92 pthread_mutex_unlock(&allocator_mutex);
95 #endif /* SLJIT_EXECUTABLE_ALLOCATOR */
97 #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
99 static pthread_mutex_t global_mutex = PTHREAD_MUTEX_INITIALIZER;
101 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_grab_lock(void)
103 pthread_mutex_lock(&global_mutex);
106 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_release_lock(void)
108 pthread_mutex_unlock(&global_mutex);
111 #endif /* SLJIT_UTIL_GLOBAL_LOCK */
115 /* ------------------------------------------------------------------------ */
117 /* ------------------------------------------------------------------------ */
119 #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
124 #include <sys/mman.h>
128 /* Planning to make it even more clever in the future. */
129 static sljit_w sljit_page_align = 0;
131 SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_CALL sljit_allocate_stack(sljit_uw limit, sljit_uw max_limit)
133 struct sljit_stack *stack;
142 if (limit > max_limit || limit < 1)
146 if (!sljit_page_align) {
148 sljit_page_align = si.dwPageSize - 1;
151 if (!sljit_page_align) {
152 sljit_page_align = sysconf(_SC_PAGESIZE);
153 /* Should never happen. */
154 if (sljit_page_align < 0)
155 sljit_page_align = 4096;
160 /* Align limit and max_limit. */
161 max_limit = (max_limit + sljit_page_align) & ~sljit_page_align;
163 stack = (struct sljit_stack*)SLJIT_MALLOC(sizeof(struct sljit_stack));
168 base.ptr = VirtualAlloc(0, max_limit, MEM_RESERVE, PAGE_READWRITE);
173 stack->base = base.uw;
174 stack->limit = stack->base;
175 stack->max_limit = stack->base + max_limit;
176 if (sljit_stack_resize(stack, stack->base + limit)) {
177 sljit_free_stack(stack);
181 base.ptr = mmap(0, max_limit, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
182 if (base.ptr == MAP_FAILED) {
186 stack->base = base.uw;
187 stack->limit = stack->base + limit;
188 stack->max_limit = stack->base + max_limit;
190 stack->top = stack->base;
196 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_free_stack(struct sljit_stack* stack)
199 VirtualFree((void*)stack->base, 0, MEM_RELEASE);
201 munmap((void*)stack->base, stack->max_limit - stack->base);
206 SLJIT_API_FUNC_ATTRIBUTE sljit_w SLJIT_CALL sljit_stack_resize(struct sljit_stack* stack, sljit_uw new_limit)
208 sljit_uw aligned_old_limit;
209 sljit_uw aligned_new_limit;
211 if ((new_limit > stack->max_limit) || (new_limit < stack->base))
214 aligned_new_limit = (new_limit + sljit_page_align) & ~sljit_page_align;
215 aligned_old_limit = (stack->limit + sljit_page_align) & ~sljit_page_align;
216 if (aligned_new_limit != aligned_old_limit) {
217 if (aligned_new_limit > aligned_old_limit) {
218 if (!VirtualAlloc((void*)aligned_old_limit, aligned_new_limit - aligned_old_limit, MEM_COMMIT, PAGE_READWRITE))
222 if (!VirtualFree((void*)aligned_new_limit, aligned_old_limit - aligned_new_limit, MEM_DECOMMIT))
226 stack->limit = new_limit;
229 if (new_limit >= stack->limit) {
230 stack->limit = new_limit;
233 aligned_new_limit = (new_limit + sljit_page_align) & ~sljit_page_align;
234 aligned_old_limit = (stack->limit + sljit_page_align) & ~sljit_page_align;
235 if (aligned_new_limit < aligned_old_limit)
236 madvise((void*)aligned_new_limit, aligned_old_limit - aligned_new_limit, MADV_DONTNEED);
237 stack->limit = new_limit;
242 #endif /* SLJIT_UTIL_STACK */