@@ -236,10 +236,13 @@ def __init__(self, name:str, lib:bytes):
236
236
PAGE_EXECUTE_READWRITE = 0x40
237
237
MEM_COMMIT = 0x1000
238
238
MEM_RESERVE = 0x2000
239
- ctypes .windll .kernel32 .VirtualAlloc .restype = ctypes .c_uint64
240
- ptr = ctypes .windll .kernel32 .VirtualAlloc (ctypes .c_int (0 ), ctypes .c_int (len (lib )), MEM_COMMIT | MEM_RESERVE , PAGE_EXECUTE_READWRITE )
241
- ctypes .memmove (ptr , lib , len (lib ))
242
- self .fxn = ctypes .CFUNCTYPE (None )(ptr )
239
+ ctypes .windll .kernel32 .VirtualAlloc .restype = ctypes .c_void_p
240
+ self .mem = ctypes .windll .kernel32 .VirtualAlloc (ctypes .c_void_p (0 ), ctypes .c_size_t (len (lib )), MEM_COMMIT | MEM_RESERVE , PAGE_EXECUTE_READWRITE )
241
+ ctypes .memmove (self .mem , lib , len (lib ))
242
+ ctypes .windll .kernel32 .GetCurrentProcess .restype = ctypes .c_void_p
243
+ proc = ctypes .windll .kernel32 .GetCurrentProcess ()
244
+ ctypes .windll .kernel32 .FlushInstructionCache (ctypes .c_void_p (proc ), ctypes .c_void_p (self .mem ), ctypes .c_size_t (len (lib )))
245
+ self .fxn = ctypes .CFUNCTYPE (None )(self .mem )
243
246
else :
244
247
from mmap import mmap , PROT_READ , PROT_WRITE , PROT_EXEC , MAP_ANON , MAP_PRIVATE
245
248
# On apple silicon with SPRR enabled (it always is in macos) RWX pages are unrepresentable: https://blog.svenpeter.dev/posts/m1_sprr_gxf/
@@ -268,6 +271,9 @@ def __call__(self, *bufs, vals=(), wait=False):
268
271
if platform .machine () == "arm64" and OSX : args = args [:8 ] + [ctypes .c_int64 (a ) if isinstance (a , int ) else a for a in args [8 :]]
269
272
return cpu_time_execution (lambda : self .fxn (* args ), enable = wait )
270
273
274
+ def __del__ (self ):
275
+ if sys .platform == 'win32' : ctypes .windll .kernel32 .VirtualFree (ctypes .c_void_p (self .mem ), ctypes .c_size_t (0 ), 0x8000 ) #0x8000 - MEM_RELEASE
276
+
271
277
# **************** for Compiled Devices ****************
272
278
273
279
class CompileError (Exception ): pass
0 commit comments