Browse Source

Add minor additions to mem, sync, and sys/win32

gingerBill 6 years ago
parent
commit
79b585ada8
3 changed files with 79 additions and 2 deletions
  1. 8 0
      core/mem/mem.odin
  2. 28 0
      core/sync/sync_windows.odin
  3. 43 2
      core/sys/win32/kernel32.odin

+ 8 - 0
core/mem/mem.odin

@@ -183,6 +183,14 @@ align_forward_uintptr :: proc(ptr, align: uintptr) -> uintptr {
 	return uintptr(p);
 	return uintptr(p);
 }
 }
 
 
+
+align_forward_int :: inline proc(ptr, align: int) -> int {
+	return int(align_forward_uintptr(uintptr(ptr), uintptr(align)));
+}
+align_forward_uint :: inline proc(ptr, align: uint) -> uint {
+	return uint(align_forward_uintptr(uintptr(ptr), uintptr(align)));
+}
+
 context_from_allocator :: proc(a: Allocator) -> type_of(context) {
 context_from_allocator :: proc(a: Allocator) -> type_of(context) {
 	context.allocator = a;
 	context.allocator = a;
 	return context;
 	return context;

+ 28 - 0
core/sync/sync_windows.odin

@@ -2,6 +2,11 @@ package sync
 
 
 import "core:sys/win32"
 import "core:sys/win32"
 
 
+foreign {
+	@(link_name="llvm.x86.sse2.pause")
+	yield_processor :: proc() ---
+}
+
 Semaphore :: struct {
 Semaphore :: struct {
 	_handle: win32.Handle,
 	_handle: win32.Handle,
 }
 }
@@ -14,6 +19,12 @@ Condition :: struct {
 	event: win32.Handle,
 	event: win32.Handle,
 }
 }
 
 
+Ticket_Mutex :: struct {
+	ticket:  u64,
+	serving: u64,
+}
+
+
 current_thread_id :: proc() -> i32 {
 current_thread_id :: proc() -> i32 {
 	return i32(win32.get_current_thread_id());
 	return i32(win32.get_current_thread_id());
 }
 }
@@ -81,3 +92,20 @@ condition_destroy :: proc(using c: ^Condition) {
 		win32.close_handle(event);
 		win32.close_handle(event);
 	}
 	}
 }
 }
+
+
+ticket_mutex_init :: proc(m: ^Ticket_Mutex) {
+	atomic_store(&m.ticket,  0, Ordering.Relaxed);
+	atomic_store(&m.serving, 0, Ordering.Relaxed);
+}
+
+ticket_mutex_lock :: inline proc(m: ^Ticket_Mutex) {
+	ticket := atomic_add(&m.ticket, 1, Ordering.Relaxed);
+	for ticket != m.serving {
+		yield_processor();
+	}
+}
+
+ticket_mutex_unlock :: inline proc(m: ^Ticket_Mutex) {
+	atomic_add(&m.serving, 1, Ordering.Relaxed);
+}

+ 43 - 2
core/sys/win32/kernel32.odin

@@ -56,8 +56,8 @@ foreign kernel32 {
 	@(link_name="GetFileSizeEx")              get_file_size_ex               :: proc(file_handle: Handle, file_size: ^i64) -> Bool ---;
 	@(link_name="GetFileSizeEx")              get_file_size_ex               :: proc(file_handle: Handle, file_size: ^i64) -> Bool ---;
 	@(link_name="GetFileAttributesA")         get_file_attributes_a          :: proc(filename: cstring) -> u32 ---;
 	@(link_name="GetFileAttributesA")         get_file_attributes_a          :: proc(filename: cstring) -> u32 ---;
 	@(link_name="GetFileAttributesW")         get_file_attributes_w          :: proc(filename: Wstring) -> u32 ---;
 	@(link_name="GetFileAttributesW")         get_file_attributes_w          :: proc(filename: Wstring) -> u32 ---;
-	@(link_name="GetFileAttributesExA")       get_file_attributes_ex_a       :: proc(filename: cstring, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: rawptr) -> Bool ---;
-	@(link_name="GetFileAttributesExW")       get_file_attributes_ex_w       :: proc(filename: Wstring, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: rawptr) -> Bool ---;
+	@(link_name="GetFileAttributesExA")       get_file_attributes_ex_a       :: proc(filename: cstring, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: ^File_Attribute_Data) -> Bool ---;
+	@(link_name="GetFileAttributesExW")       get_file_attributes_ex_w       :: proc(filename: Wstring, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: ^File_Attribute_Data) -> Bool ---;
 	@(link_name="GetFileInformationByHandle") get_file_information_by_handle :: proc(file_handle: Handle, file_info: ^By_Handle_File_Information) -> Bool ---;
 	@(link_name="GetFileInformationByHandle") get_file_information_by_handle :: proc(file_handle: Handle, file_info: ^By_Handle_File_Information) -> Bool ---;
 
 
 	@(link_name="CreateDirectoryA") 		  create_directory_a			 :: proc(path: cstring, security_attributes: ^Security_Attributes) -> Bool ---;
 	@(link_name="CreateDirectoryA") 		  create_directory_a			 :: proc(path: cstring, security_attributes: ^Security_Attributes) -> Bool ---;
@@ -168,3 +168,44 @@ foreign kernel32 {
 	@(link_name="GetProcAddress") get_proc_address :: proc(h: Hmodule, c_str: cstring) -> rawptr ---;
 	@(link_name="GetProcAddress") get_proc_address :: proc(h: Hmodule, c_str: cstring) -> rawptr ---;
 
 
 }
 }
+
+Memory_Basic_Information :: struct {
+	base_address:       rawptr,
+	allocation_base:    rawptr,
+	allocation_protect: u32,
+	region_size:        uint,
+	state:              u32,
+	protect:            u32,
+	type:               u32,
+}
+
+@(default_calling_convention = "std")
+foreign kernel32 {
+	@(link_name="VirtualAlloc")   virtual_alloc    :: proc(address: rawptr, size: uint, allocation_type: u32, protect: u32) -> rawptr ---
+	@(link_name="VirtualAllocEx") virtual_alloc_ex :: proc(process: Handle, address: rawptr, size: uint, allocation_type: u32, protect: u32) -> rawptr ---
+	@(link_name="VirtualFree")    virtual_free     :: proc(address: rawptr, size: uint, free_type: u32) -> Bool ---
+	@(link_name="VirtualLock")    virtual_lock     :: proc(address: rawptr, size: uint) -> Bool ---
+	@(link_name="VirtualProtect") virtual_protect  :: proc(address: rawptr, size: uint, new_protect: u32, old_protect: ^u32) -> Bool ---
+	@(link_name="VirtualQuery")   virtual_query    :: proc(address: rawptr, buffer: ^Memory_Basic_Information, length: uint) -> uint ---
+}
+
+MEM_COMMIT      :: 0x00001000;
+MEM_RESERVE     :: 0x00002000;
+MEM_DECOMMIT    :: 0x00004000;
+MEM_RELEASE     :: 0x00008000;
+MEM_RESET       :: 0x00080000;
+MEM_RESET_UNDO  :: 0x01000000;
+
+MEM_LARGE_PAGES :: 0x20000000;
+MEM_PHYSICAL    :: 0x00400000;
+MEM_TOP_DOWN    :: 0x00100000;
+MEM_WRITE_WATCH :: 0x00200000;
+
+PAGE_NOACCESS           :: 0x01;
+PAGE_READONLY           :: 0x02;
+PAGE_READWRITE          :: 0x04;
+PAGE_WRITECOPY          :: 0x08;
+PAGE_EXECUTE            :: 0x10;
+PAGE_EXECUTE_READ       :: 0x20;
+PAGE_EXECUTE_READWRITE  :: 0x40;
+PAGE_EXECUTE_WRITECOPY  :: 0x80;