@@ -8,6 +8,15 @@ use inkwell::module::Linkage;
88use inkwell:: targets:: { CodeModel , InitializationConfig , Target } ;
99use inkwell:: { AddressSpace , IntPredicate , OptimizationLevel } ;
1010
11+ #[ cfg( target_os = "windows" ) ]
12+ use windows:: Win32 :: System :: {
13+ Memory :: {
14+ VirtualAlloc , VirtualFree , VirtualProtect , MEM_COMMIT , MEM_RELEASE , MEM_RESERVE , PAGE_EXECUTE_READ ,
15+ PAGE_READWRITE ,
16+ } ,
17+ SystemInformation :: { GetSystemInfo , SYSTEM_INFO } ,
18+ } ;
19+
1120type Thunk = unsafe extern "C" fn ( ) ;
1221
1322#[ test]
@@ -374,8 +383,20 @@ struct MockMemoryManagerData {
374383impl MockMemoryManager {
375384 pub fn new ( ) -> Self {
376385 let capacity_bytes = 128 * 1024 ;
386+ #[ cfg( unix) ]
377387 let page_size = unsafe { libc:: sysconf ( libc:: _SC_PAGESIZE) as usize } ;
378388
389+ #[ cfg( target_os = "windows" ) ]
390+ let page_size = {
391+ let mut info = std:: mem:: MaybeUninit :: < SYSTEM_INFO > :: uninit ( ) ;
392+ unsafe {
393+ GetSystemInfo ( info. as_mut_ptr ( ) ) ;
394+ }
395+ let info = unsafe { info. assume_init ( ) } ;
396+ info. dwPageSize as usize
397+ } ;
398+
399+ #[ cfg( unix) ]
379400 let code_buff_ptr = unsafe {
380401 std:: ptr:: NonNull :: new_unchecked ( libc:: mmap (
381402 std:: ptr:: null_mut ( ) ,
@@ -387,6 +408,14 @@ impl MockMemoryManager {
387408 ) as * mut u8 )
388409 } ;
389410
411+ #[ cfg( target_os = "windows" ) ]
412+ let code_buff_ptr = unsafe {
413+ std:: ptr:: NonNull :: new_unchecked (
414+ VirtualAlloc ( None , capacity_bytes, MEM_COMMIT | MEM_RESERVE , PAGE_READWRITE ) . cast :: < u8 > ( ) ,
415+ )
416+ } ;
417+
418+ #[ cfg( unix) ]
390419 let data_buff_ptr = unsafe {
391420 std:: ptr:: NonNull :: new_unchecked ( libc:: mmap (
392421 std:: ptr:: null_mut ( ) ,
@@ -398,6 +427,13 @@ impl MockMemoryManager {
398427 ) as * mut u8 )
399428 } ;
400429
430+ #[ cfg( target_os = "windows" ) ]
431+ let data_buff_ptr = unsafe {
432+ std:: ptr:: NonNull :: new_unchecked (
433+ VirtualAlloc ( None , capacity_bytes, MEM_COMMIT | MEM_RESERVE , PAGE_READWRITE ) . cast :: < u8 > ( ) ,
434+ )
435+ } ;
436+
401437 Self {
402438 data : Rc :: new ( RefCell :: new ( MockMemoryManagerData {
403439 fixed_capacity_bytes : capacity_bytes,
@@ -460,6 +496,7 @@ impl McjitMemoryManager for MockMemoryManager {
460496
461497 data. finalize_calls += 1 ;
462498
499+ #[ cfg( unix) ]
463500 unsafe {
464501 libc:: mprotect (
465502 data. code_buff_ptr . as_ptr ( ) as * mut libc:: c_void ,
@@ -473,6 +510,25 @@ impl McjitMemoryManager for MockMemoryManager {
473510 ) ;
474511 }
475512
513+ #[ cfg( windows) ]
514+ unsafe {
515+ let mut old_protect = PAGE_READWRITE ;
516+ VirtualProtect (
517+ data. code_buff_ptr . as_ptr ( ) as * mut _ ,
518+ data. fixed_capacity_bytes ,
519+ PAGE_EXECUTE_READ ,
520+ & mut old_protect,
521+ )
522+ . expect ( "VirtualProtect failed" ) ;
523+ VirtualProtect (
524+ data. data_buff_ptr . as_ptr ( ) as * mut _ ,
525+ data. fixed_capacity_bytes ,
526+ PAGE_READWRITE ,
527+ & mut old_protect,
528+ )
529+ . expect ( "VirtualProtect failed" ) ;
530+ }
531+
476532 Ok ( ( ) )
477533 }
478534
@@ -481,6 +537,7 @@ impl McjitMemoryManager for MockMemoryManager {
481537
482538 data. destroy_calls += 1 ;
483539
540+ #[ cfg( unix) ]
484541 unsafe {
485542 libc:: munmap (
486543 data. code_buff_ptr . as_ptr ( ) as * mut libc:: c_void ,
@@ -491,5 +548,11 @@ impl McjitMemoryManager for MockMemoryManager {
491548 data. fixed_capacity_bytes ,
492549 ) ;
493550 }
551+
552+ #[ cfg( windows) ]
553+ unsafe {
554+ VirtualFree ( data. code_buff_ptr . as_ptr ( ) as * mut _ , 0 , MEM_RELEASE ) . expect ( "Failed to free memory." ) ;
555+ VirtualFree ( data. data_buff_ptr . as_ptr ( ) as * mut _ , 0 , MEM_RELEASE ) . expect ( "Failed to free memory." ) ;
556+ }
494557 }
495558}
0 commit comments