use win32.wstring_to_utf8, default to temp_allocator, guarantees proper null termination

This commit is contained in:
PePerRoNii
2025-07-15 12:29:26 +07:00
parent 8f81b8761a
commit 228e2752a3
2 changed files with 5 additions and 12 deletions

View File

@@ -162,7 +162,7 @@ If thread is nil the procedure will get the name of the calling thread.
allocates memory for the returned string using provided allocator.
*/
get_name :: proc(thread: ^Thread, allocator := context.allocator, loc := #caller_location) -> (string, runtime.Allocator_Error) {
get_name :: proc(thread: ^Thread, allocator := context.temp_allocator, loc := #caller_location) -> (string, runtime.Allocator_Error) {
return _get_name(thread, allocator, loc)
}

View File

@@ -163,15 +163,9 @@ _get_name :: proc(thread: ^Thread, allocator: runtime.Allocator, loc: runtime.So
t_handle = thread.win32_thread
}
buf_8: [_THREAD_DESCRIPTION_LENGTH * 2]u8
buf_16: [_THREAD_DESCRIPTION_LENGTH]u16
win32.GetThreadDescription(t_handle, raw_data(buf_16[:]))
n := utf16.decode_to_utf8(buf_8[:], buf_16[:])
buf := make([]u8, n, allocator, loc)
copy(buf[:], buf_8[:])
name = transmute(string)buf
buf_16: win32.PWSTR
win32.GetThreadDescription(t_handle, &buf_16)
name = win32.wstring_to_utf8(buf_16, -1, allocator) or_return
return
}
@@ -185,9 +179,8 @@ _set_name :: proc(thread: ^Thread, name: string) {
}
buf: [_THREAD_DESCRIPTION_LENGTH]u16
utf16.encode_string(buf[:], name)
// _THREAD_DESCRIPTION_LENGTH includes terminating null
buf[len(buf) - 1] = 0
utf16.encode_string(buf[:len(buf) - 1], name)
win32.SetThreadDescription(t_handle, raw_data(buf[:]))
}