@@ -45,6 +45,22 @@ LONG CALLBACK sqExceptionFilter(LPEXCEPTION_POINTERS exp)
4545 return EXCEPTION_WRONG_ACCESS ;
4646}
4747
48+ # define roundDownToPage (v ) ((sqIntptr_t)(v)&pageMask)
49+ # define roundUpToPage (v ) (((sqIntptr_t)(v)+pageSize-1)&pageMask)
50+
51+ static void
52+ initPageSize ()
53+ {
54+ SYSTEM_INFO sysInfo ;
55+
56+ /* determine page boundaries & available address space */
57+ if (!pageSize ) {
58+ GetSystemInfo (& sysInfo );
59+ pageSize = sysInfo .dwPageSize ;
60+ pageMask = ~(pageSize - 1 );
61+ }
62+ }
63+
4864/************************************************************************/
4965/* sqAllocateMemory: Initialize virtual memory */
5066/************************************************************************/
@@ -59,10 +75,10 @@ sqAllocateMemory(usqInt minHeapSize, usqInt desiredHeapSize)
5975 pageMask = ~(pageSize - 1 );
6076
6177 /* round the requested size up to the next page boundary */
62- nowReserved = (desiredHeapSize + pageSize ) & pageMask ;
78+ nowReserved = roundUpToPage (desiredHeapSize ) ;
6379
6480 /* round the initial commited size up to the next page boundary */
65- initialCommit = (minHeapSize + pageSize ) & pageMask ;
81+ initialCommit = roundUpToPage (minHeapSize ) ;
6682
6783 /* Here, we only reserve the maximum memory to be used
6884 It will later be committed during actual access */
@@ -78,15 +94,15 @@ sqAllocateMemory(usqInt minHeapSize, usqInt desiredHeapSize)
7894 } while (!pageBase );
7995 if (!pageBase ) {
8096 sqMessageBox (MB_OK | MB_ICONSTOP , TEXT ("VM Error:" ),
81- "Unable to allocate memory (%d bytes requested)" ,
97+ TEXT ( "Unable to allocate memory (%d bytes requested)" ) ,
8298 maxReserved );
8399 return pageBase ;
84100 }
85101 /* commit initial memory as requested */
86102 commit = nowReserved ;
87103 if (!VirtualAlloc (pageBase , commit , MEM_COMMIT , PAGE_READWRITE )) {
88104 sqMessageBox (MB_OK | MB_ICONSTOP , TEXT ("VM Error:" ),
89- "Unable to commit memory (%d bytes requested)" ,
105+ TEXT ( "Unable to commit memory (%d bytes requested)" ) ,
90106 commit );
91107 return NULL ;
92108 }
@@ -176,34 +192,40 @@ sqMemoryExtraBytesLeft(int includingSwap) {
176192 return bytesLeft ;
177193}
178194
179- #define roundDownToPage (v ) ((v)&pageMask)
180- #define roundUpToPage (v ) (((v)+pageSize-1)&pageMask)
181-
182195# if COGVM
183196void
184197sqMakeMemoryExecutableFromToCodeToDataDelta (usqInt startAddr , usqInt endAddr , sqInt * codeToDataDelta )
185198{
186- DWORD previous ;
187-
188- if (!VirtualProtect ((void * )startAddr ,
189- endAddr - startAddr + 1 ,
190- PAGE_EXECUTE_READWRITE ,
191- & previous ))
192- sqWin32PrintLastError ("VirtualProtect(x,y,PAGE_EXECUTE_READWRITE)" );
193- if (codeToDataDelta )
194- * codeToDataDelta = 0 ;
199+ DWORD previous ;
200+ SIZE_T size ;
201+
202+ size = endAddr - startAddr ;
203+ if (!VirtualProtect ((void * )startAddr ,
204+ size ,
205+ PAGE_EXECUTE_READWRITE ,
206+ & previous ))
207+ sqWin32PrintLastError ("VirtualProtect(x,y,PAGE_EXECUTE_READWRITE)" );
208+ if (codeToDataDelta )
209+ * codeToDataDelta = 0 ;
195210}
196211
197- void
198- sqMakeMemoryNotExecutableFromTo (usqInt startAddr , usqInt endAddr )
212+ void *
213+ allocateJITMemory (usqInt * desiredSize )
199214{
200- DWORD previous ;
215+ initPageSize ();
216+
217+ sqInt allocBytes = roundUpToPage (* desiredSize );
218+
219+ /* Allocate extra memory for the JIT. No need to make it executable (i.e., PAGE_EXECUTE_READWRITE) right away because there will be an extra call to sqMakeMemoryExecutableFromToCodeToDataDelta(..) anyway. */
220+ char * alloc = VirtualAlloc (NULL ,allocBytes ,MEM_COMMIT |MEM_RESERVE , PAGE_READWRITE );
221+
222+ if (!alloc ) {
223+ sqWin32PrintLastError ("Could not allocate JIT memory" );
224+ exit (1 );
225+ }
201226
202- if (!VirtualProtect ((void * )startAddr ,
203- endAddr - startAddr + 1 ,
204- PAGE_READWRITE ,
205- & previous ))
206- sqWin32PrintLastError ("VirtualProtect(x,y,PAGE_EXECUTE_READWRITE)" );
227+ * desiredSize = allocBytes ;
228+ return alloc ;
207229}
208230# endif /* COGVM */
209231#endif /* !defined(NO_VIRTUAL_MEMORY) && !SPURVM */
0 commit comments