Merged with master

This commit is contained in:
Andreas T Jonsson
2024-05-02 09:27:46 +02:00
70 changed files with 1571 additions and 795 deletions
+4
View File
@@ -27,6 +27,8 @@ tests/documentation/all.odin-doc
tests/internal/test_map tests/internal/test_map
tests/internal/test_pow tests/internal/test_pow
tests/internal/test_rtti tests/internal/test_rtti
tests/core/test_base64
tests/core/test_cbor
tests/core/test_core_compress tests/core/test_core_compress
tests/core/test_core_container tests/core/test_core_container
tests/core/test_core_filepath tests/core/test_core_filepath
@@ -40,8 +42,10 @@ tests/core/test_core_net
tests/core/test_core_os_exit tests/core/test_core_os_exit
tests/core/test_core_reflect tests/core/test_core_reflect
tests/core/test_core_strings tests/core/test_core_strings
tests/core/test_core_time
tests/core/test_crypto tests/core/test_crypto
tests/core/test_hash tests/core/test_hash
tests/core/test_hex
tests/core/test_hxa tests/core/test_hxa
tests/core/test_json tests/core/test_json
tests/core/test_linalg_glsl_math tests/core/test_linalg_glsl_math
+4 -4
View File
@@ -1043,8 +1043,8 @@ __write_bits :: proc "contextless" (dst, src: [^]byte, offset: uintptr, size: ui
for i in 0..<size { for i in 0..<size {
j := offset+i j := offset+i
the_bit := byte((src[i>>3]) & (1<<(i&7)) != 0) the_bit := byte((src[i>>3]) & (1<<(i&7)) != 0)
b := the_bit<<(j&7) dst[j>>3] &~= 1<<(j&7)
dst[j>>3] = (dst[j>>3] &~ b) | b dst[j>>3] |= the_bit<<(j&7)
} }
} }
@@ -1052,7 +1052,7 @@ __read_bits :: proc "contextless" (dst, src: [^]byte, offset: uintptr, size: uin
for j in 0..<size { for j in 0..<size {
i := offset+j i := offset+j
the_bit := byte((src[i>>3]) & (1<<(i&7)) != 0) the_bit := byte((src[i>>3]) & (1<<(i&7)) != 0)
b := the_bit<<(j&7) dst[j>>3] &~= 1<<(j&7)
dst[j>>3] = (dst[j>>3] &~ b) | b dst[j>>3] |= the_bit<<(j&7)
} }
} }
+5 -5
View File
@@ -32,11 +32,11 @@ if [ -z "$LLVM_CONFIG" ]; then
elif [ -n "$(command -v llvm-config-12)" ]; then LLVM_CONFIG="llvm-config-12" elif [ -n "$(command -v llvm-config-12)" ]; then LLVM_CONFIG="llvm-config-12"
elif [ -n "$(command -v llvm-config-11)" ]; then LLVM_CONFIG="llvm-config-11" elif [ -n "$(command -v llvm-config-11)" ]; then LLVM_CONFIG="llvm-config-11"
# freebsd # freebsd
elif [ -n "$(command -v llvm-config17)" ]; then LLVM_CONFIG="llvm-config-17" elif [ -n "$(command -v llvm-config17)" ]; then LLVM_CONFIG="llvm-config17"
elif [ -n "$(command -v llvm-config14)" ]; then LLVM_CONFIG="llvm-config-14" elif [ -n "$(command -v llvm-config14)" ]; then LLVM_CONFIG="llvm-config14"
elif [ -n "$(command -v llvm-config13)" ]; then LLVM_CONFIG="llvm-config-13" elif [ -n "$(command -v llvm-config13)" ]; then LLVM_CONFIG="llvm-config13"
elif [ -n "$(command -v llvm-config12)" ]; then LLVM_CONFIG="llvm-config-12" elif [ -n "$(command -v llvm-config12)" ]; then LLVM_CONFIG="llvm-config12"
elif [ -n "$(command -v llvm-config11)" ]; then LLVM_CONFIG="llvm-config-11" elif [ -n "$(command -v llvm-config11)" ]; then LLVM_CONFIG="llvm-config11"
# fallback # fallback
elif [ -n "$(command -v llvm-config)" ]; then LLVM_CONFIG="llvm-config" elif [ -n "$(command -v llvm-config)" ]; then LLVM_CONFIG="llvm-config"
else else
+4 -4
View File
@@ -81,7 +81,7 @@ _reader_read_new_chunk :: proc(b: ^Reader) -> io.Error {
for i := b.max_consecutive_empty_reads; i > 0; i -= 1 { for i := b.max_consecutive_empty_reads; i > 0; i -= 1 {
n, err := io.read(b.rd, b.buf[b.w:]) n, err := io.read(b.rd, b.buf[b.w:])
if n < 0 { if n < 0 {
return .Negative_Read return err if err != nil else .Negative_Read
} }
b.w += n b.w += n
if err != nil { if err != nil {
@@ -189,7 +189,7 @@ reader_read :: proc(b: ^Reader, p: []byte) -> (n: int, err: io.Error) {
if len(p) >= len(b.buf) { if len(p) >= len(b.buf) {
n, b.err = io.read(b.rd, p) n, b.err = io.read(b.rd, p)
if n < 0 { if n < 0 {
return 0, .Negative_Read return 0, b.err if b.err != nil else .Negative_Read
} }
if n > 0 { if n > 0 {
@@ -202,7 +202,7 @@ reader_read :: proc(b: ^Reader, p: []byte) -> (n: int, err: io.Error) {
b.r, b.w = 0, 0 b.r, b.w = 0, 0
n, b.err = io.read(b.rd, b.buf) n, b.err = io.read(b.rd, b.buf)
if n < 0 { if n < 0 {
return 0, .Negative_Read return 0, b.err if b.err != nil else .Negative_Read
} }
if n == 0 { if n == 0 {
return 0, _reader_consume_err(b) return 0, _reader_consume_err(b)
@@ -290,7 +290,7 @@ reader_write_to :: proc(b: ^Reader, w: io.Writer) -> (n: i64, err: io.Error) {
write_buf :: proc(b: ^Reader, w: io.Writer) -> (i64, io.Error) { write_buf :: proc(b: ^Reader, w: io.Writer) -> (i64, io.Error) {
n, err := io.write(w, b.buf[b.r:b.w]) n, err := io.write(w, b.buf[b.r:b.w])
if n < 0 { if n < 0 {
return 0, .Negative_Write return 0, err if err != nil else .Negative_Write
} }
b.r += n b.r += n
return i64(n), err return i64(n), err
+4
View File
@@ -95,6 +95,10 @@ writer_write :: proc(b: ^Writer, p: []byte) -> (n: int, err: io.Error) {
m: int m: int
if writer_buffered(b) == 0 { if writer_buffered(b) == 0 {
m, b.err = io.write(b.wr, p) m, b.err = io.write(b.wr, p)
if m < 0 && b.err == nil {
b.err = .Negative_Write
break
}
} else { } else {
m = copy(b.buf[b.n:], p) m = copy(b.buf[b.n:], p)
b.n += m b.n += m
+1 -1
View File
@@ -359,7 +359,7 @@ buffer_read_from :: proc(b: ^Buffer, r: io.Reader) -> (n: i64, err: io.Error) #n
resize(&b.buf, i) resize(&b.buf, i)
m, e := io.read(r, b.buf[i:cap(b.buf)]) m, e := io.read(r, b.buf[i:cap(b.buf)])
if m < 0 { if m < 0 {
err = .Negative_Read err = e if e != nil else .Negative_Read
return return
} }
+3 -6
View File
@@ -49,15 +49,12 @@ compare_byte_ptrs_constant_time :: proc "contextless" (a, b: ^byte, n: int) -> i
// the system entropy source. This routine will block if the system entropy // the system entropy source. This routine will block if the system entropy
// source is not ready yet. All system entropy source failures are treated // source is not ready yet. All system entropy source failures are treated
// as catastrophic, resulting in a panic. // as catastrophic, resulting in a panic.
//
// Support for the system entropy source can be checked with the
// `HAS_RAND_BYTES` boolean constant.
rand_bytes :: proc (dst: []byte) { rand_bytes :: proc (dst: []byte) {
// zero-fill the buffer first // zero-fill the buffer first
mem.zero_explicit(raw_data(dst), len(dst)) mem.zero_explicit(raw_data(dst), len(dst))
_rand_bytes(dst) _rand_bytes(dst)
} }
// has_rand_bytes returns true iff the target has support for accessing the
// system entropty source.
has_rand_bytes :: proc () -> bool {
return _has_rand_bytes()
}
+3 -4
View File
@@ -3,14 +3,13 @@ package crypto
foreign import libc "system:c" foreign import libc "system:c"
HAS_RAND_BYTES :: true
foreign libc { foreign libc {
arc4random_buf :: proc(buf: [^]byte, nbytes: uint) --- arc4random_buf :: proc(buf: [^]byte, nbytes: uint) ---
} }
@(private)
_rand_bytes :: proc(dst: []byte) { _rand_bytes :: proc(dst: []byte) {
arc4random_buf(raw_data(dst), len(dst)) arc4random_buf(raw_data(dst), len(dst))
} }
_has_rand_bytes :: proc() -> bool {
return true
}
+3 -4
View File
@@ -5,6 +5,9 @@ import "core:fmt"
import CF "core:sys/darwin/CoreFoundation" import CF "core:sys/darwin/CoreFoundation"
import Sec "core:sys/darwin/Security" import Sec "core:sys/darwin/Security"
HAS_RAND_BYTES :: true
@(private)
_rand_bytes :: proc(dst: []byte) { _rand_bytes :: proc(dst: []byte) {
err := Sec.RandomCopyBytes(count=len(dst), bytes=raw_data(dst)) err := Sec.RandomCopyBytes(count=len(dst), bytes=raw_data(dst))
if err != .Success { if err != .Success {
@@ -12,7 +15,3 @@ _rand_bytes :: proc(dst: []byte) {
panic(fmt.tprintf("crypto/rand_bytes: SecRandomCopyBytes returned non-zero result: %v %s", err, msg)) panic(fmt.tprintf("crypto/rand_bytes: SecRandomCopyBytes returned non-zero result: %v %s", err, msg))
} }
} }
_has_rand_bytes :: proc() -> bool {
return true
}
+3 -4
View File
@@ -7,10 +7,9 @@
//+build !js //+build !js
package crypto package crypto
HAS_RAND_BYTES :: false
@(private)
_rand_bytes :: proc(dst: []byte) { _rand_bytes :: proc(dst: []byte) {
unimplemented("crypto: rand_bytes not supported on this OS") unimplemented("crypto: rand_bytes not supported on this OS")
} }
_has_rand_bytes :: proc() -> bool {
return false
}
+4 -4
View File
@@ -6,8 +6,12 @@ foreign odin_env {
env_rand_bytes :: proc "contextless" (buf: []byte) --- env_rand_bytes :: proc "contextless" (buf: []byte) ---
} }
HAS_RAND_BYTES :: true
@(private)
_MAX_PER_CALL_BYTES :: 65536 // 64kiB _MAX_PER_CALL_BYTES :: 65536 // 64kiB
@(private)
_rand_bytes :: proc(dst: []byte) { _rand_bytes :: proc(dst: []byte) {
dst := dst dst := dst
@@ -18,7 +22,3 @@ _rand_bytes :: proc(dst: []byte) {
dst = dst[to_read:] dst = dst[to_read:]
} }
} }
_has_rand_bytes :: proc() -> bool {
return true
}
+4 -4
View File
@@ -4,8 +4,12 @@ import "core:fmt"
import "core:sys/linux" import "core:sys/linux"
HAS_RAND_BYTES :: true
@(private)
_MAX_PER_CALL_BYTES :: 33554431 // 2^25 - 1 _MAX_PER_CALL_BYTES :: 33554431 // 2^25 - 1
@(private)
_rand_bytes :: proc (dst: []byte) { _rand_bytes :: proc (dst: []byte) {
dst := dst dst := dst
l := len(dst) l := len(dst)
@@ -34,7 +38,3 @@ _rand_bytes :: proc (dst: []byte) {
dst = dst[n_read:] dst = dst[n_read:]
} }
} }
_has_rand_bytes :: proc() -> bool {
return true
}
+3 -4
View File
@@ -4,6 +4,9 @@ import win32 "core:sys/windows"
import "core:os" import "core:os"
import "core:fmt" import "core:fmt"
HAS_RAND_BYTES :: true
@(private)
_rand_bytes :: proc(dst: []byte) { _rand_bytes :: proc(dst: []byte) {
ret := (os.Errno)(win32.BCryptGenRandom(nil, raw_data(dst), u32(len(dst)), win32.BCRYPT_USE_SYSTEM_PREFERRED_RNG)) ret := (os.Errno)(win32.BCryptGenRandom(nil, raw_data(dst), u32(len(dst)), win32.BCRYPT_USE_SYSTEM_PREFERRED_RNG))
if ret != os.ERROR_NONE { if ret != os.ERROR_NONE {
@@ -21,7 +24,3 @@ _rand_bytes :: proc(dst: []byte) {
} }
} }
} }
_has_rand_bytes :: proc() -> bool {
return true
}
+51
View File
@@ -0,0 +1,51 @@
/*
A debug stack trace library. Only works when debug symbols are enabled `-debug`.
Example:
import "base:runtime"
import "core:debug/trace"
import "core:fmt"
global_trace_ctx: trace.Context
debug_trace_assertion_failure_proc :: proc(prefix, message: string, loc := #caller_location) -> ! {
runtime.print_caller_location(loc)
runtime.print_string(" ")
runtime.print_string(prefix)
if len(message) > 0 {
runtime.print_string(": ")
runtime.print_string(message)
}
runtime.print_byte('\n')
ctx := &trace_ctx
if !trace.in_resolve(ctx) {
buf: [64]trace.Frame
runtime.print_string("Debug Trace:\n")
frames := trace.frames(ctx, 1, buf[:])
for f, i in frames {
fl := trace.resolve(ctx, f, context.temp_allocator)
if fl.loc.file_path == "" && fl.loc.line == 0 {
continue
}
runtime.print_caller_location(fl.loc)
runtime.print_string(" - frame ")
runtime.print_int(i)
runtime.print_byte('\n')
}
}
runtime.trap()
}
main :: proc() {
trace.init(&global_trace_ctx)
defer trace.destroy(&global_trace_ctx)
context.assertion_failure_proc = debug_trace_assertion_failure_proc
...
}
*/
package debug_trace
+47
View File
@@ -0,0 +1,47 @@
package debug_trace
import "base:intrinsics"
import "base:runtime"
Frame :: distinct uintptr
Frame_Location :: struct {
using loc: runtime.Source_Code_Location,
allocator: runtime.Allocator,
}
delete_frame_location :: proc(fl: Frame_Location) -> runtime.Allocator_Error {
allocator := fl.allocator
delete(fl.loc.procedure, allocator) or_return
delete(fl.loc.file_path, allocator) or_return
return nil
}
Context :: struct {
in_resolve: bool, // atomic
impl: _Context,
}
init :: proc(ctx: ^Context) -> bool {
return _init(ctx)
}
destroy :: proc(ctx: ^Context) -> bool {
return _destroy(ctx)
}
@(require_results)
frames :: proc(ctx: ^Context, skip: uint, frames_buffer: []Frame) -> []Frame {
return _frames(ctx, skip, frames_buffer)
}
@(require_results)
resolve :: proc(ctx: ^Context, frame: Frame, allocator: runtime.Allocator) -> (result: Frame_Location) {
return _resolve(ctx, frame, allocator)
}
@(require_results)
in_resolve :: proc "contextless" (ctx: ^Context) -> bool {
return intrinsics.atomic_load(&ctx.in_resolve)
}
+195
View File
@@ -0,0 +1,195 @@
//+private file
//+build linux, darwin
package debug_trace
import "base:intrinsics"
import "base:runtime"
import "core:strings"
import "core:fmt"
import "core:c"
// NOTE: Relies on C++23 which adds <stacktrace> and becomes ABI and that can be used
foreign import stdcpplibbacktrace "system:stdc++_libbacktrace"
foreign import libdl "system:dl"
backtrace_state :: struct {}
backtrace_error_callback :: proc "c" (data: rawptr, msg: cstring, errnum: c.int)
backtrace_simple_callback :: proc "c" (data: rawptr, pc: uintptr) -> c.int
backtrace_full_callback :: proc "c" (data: rawptr, pc: uintptr, filename: cstring, lineno: c.int, function: cstring) -> c.int
backtrace_syminfo_callback :: proc "c" (data: rawptr, pc: uintptr, symname: cstring, symval: uintptr, symsize: uintptr)
@(default_calling_convention="c", link_prefix="__glibcxx_")
foreign stdcpplibbacktrace {
backtrace_create_state :: proc(
filename: cstring,
threaded: c.int,
error_callback: backtrace_error_callback,
data: rawptr,
) -> ^backtrace_state ---
backtrace_simple :: proc(
state: ^backtrace_state,
skip: c.int,
callback: backtrace_simple_callback,
error_callback: backtrace_error_callback,
data: rawptr,
) -> c.int ---
backtrace_pcinfo :: proc(
state: ^backtrace_state,
pc: uintptr,
callback: backtrace_full_callback,
error_callback: backtrace_error_callback,
data: rawptr,
) -> c.int ---
backtrace_syminfo :: proc(
state: ^backtrace_state,
addr: uintptr,
callback: backtrace_syminfo_callback,
error_callback: backtrace_error_callback,
data: rawptr,
) -> c.int ---
// NOTE(bill): this is technically an internal procedure, but it is exposed
backtrace_free :: proc(
state: ^backtrace_state,
p: rawptr,
size: c.size_t, // unused
error_callback: backtrace_error_callback, // unused
data: rawptr, // unused
) ---
}
Dl_info :: struct {
dli_fname: cstring,
dli_fbase: rawptr,
dli_sname: cstring,
dli_saddr: rawptr,
}
@(default_calling_convention="c")
foreign libdl {
dladdr :: proc(addr: rawptr, info: ^Dl_info) -> c.int ---
}
@(private="package")
_Context :: struct {
state: ^backtrace_state,
}
@(private="package")
_init :: proc(ctx: ^Context) -> (ok: bool) {
defer if !ok do destroy(ctx)
ctx.impl.state = backtrace_create_state("odin-debug-trace", 1, nil, ctx)
return ctx.impl.state != nil
}
@(private="package")
_destroy :: proc(ctx: ^Context) -> bool {
if ctx != nil {
backtrace_free(ctx.impl.state, nil, 0, nil, nil)
}
return true
}
@(private="package")
_frames :: proc "contextless" (ctx: ^Context, skip: uint, frames_buffer: []Frame) -> (frames: []Frame) {
Backtrace_Context :: struct {
ctx: ^Context,
frames: []Frame,
frame_count: int,
}
btc := &Backtrace_Context{
ctx = ctx,
frames = frames_buffer,
}
backtrace_simple(
ctx.impl.state,
c.int(skip + 2),
proc "c" (user: rawptr, address: uintptr) -> c.int {
btc := (^Backtrace_Context)(user)
address := Frame(address)
if address == 0 {
return 1
}
if btc.frame_count == len(btc.frames) {
return 1
}
btc.frames[btc.frame_count] = address
btc.frame_count += 1
return 0
},
nil,
btc,
)
if btc.frame_count > 0 {
frames = btc.frames[:btc.frame_count]
}
return
}
@(private="package")
_resolve :: proc(ctx: ^Context, frame: Frame, allocator: runtime.Allocator) -> Frame_Location {
intrinsics.atomic_store(&ctx.in_resolve, true)
defer intrinsics.atomic_store(&ctx.in_resolve, false)
Backtrace_Context :: struct {
rt_ctx: runtime.Context,
allocator: runtime.Allocator,
frame: Frame_Location,
}
btc := &Backtrace_Context{
rt_ctx = context,
allocator = allocator,
}
done := backtrace_pcinfo(
ctx.impl.state,
uintptr(frame),
proc "c" (data: rawptr, address: uintptr, file: cstring, line: c.int, symbol: cstring) -> c.int {
btc := (^Backtrace_Context)(data)
context = btc.rt_ctx
frame := &btc.frame
if file != nil {
frame.file_path = strings.clone_from_cstring(file, btc.allocator)
} else if info: Dl_info; dladdr(rawptr(address), &info) != 0 && info.dli_fname != "" {
frame.file_path = strings.clone_from_cstring(info.dli_fname, btc.allocator)
}
if symbol != nil {
frame.procedure = strings.clone_from_cstring(symbol, btc.allocator)
} else if info: Dl_info; dladdr(rawptr(address), &info) != 0 && info.dli_sname != "" {
frame.procedure = strings.clone_from_cstring(info.dli_sname, btc.allocator)
} else {
frame.procedure = fmt.aprintf("(procedure: 0x%x)", allocator=btc.allocator)
}
frame.line = i32(line)
return 0
},
nil,
btc,
)
if done != 0 {
return btc.frame
}
// NOTE(bill): pcinfo cannot resolve, but it might be possible to get the procedure name at least
backtrace_syminfo(
ctx.impl.state,
uintptr(frame),
proc "c" (data: rawptr, address: uintptr, symbol: cstring, _ignore0, _ignore1: uintptr) {
if symbol != nil {
btc := (^Backtrace_Context)(data)
context = btc.rt_ctx
btc.frame.procedure = strings.clone_from_cstring(symbol, btc.allocator)
}
},
nil,
btc,
)
return btc.frame
}
+18
View File
@@ -0,0 +1,18 @@
//+build !windows !linux !darwin
package debug_trace
_Context :: struct {
}
_init :: proc(ctx: ^Context) -> (ok: bool) {
return true
}
_destroy :: proc(ctx: ^Context) -> bool {
return true
}
_frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> []Frame {
return nil
}
_resolve :: proc(ctx: ^Context, frame: Frame, allocator: runtime.Allocator) -> (result: runtime.Source_Code_Location) {
return
}
+68
View File
@@ -0,0 +1,68 @@
//+private
//+build windows
package debug_trace
import "base:intrinsics"
import "base:runtime"
import win32 "core:sys/windows"
import "core:fmt"
_Context :: struct {
hProcess: win32.HANDLE,
lock: win32.SRWLOCK,
}
_init :: proc "contextless" (ctx: ^Context) -> (ok: bool) {
defer if !ok { _destroy(ctx) }
ctx.impl.hProcess = win32.GetCurrentProcess()
win32.SymInitialize(ctx.impl.hProcess, nil, true) or_return
win32.SymSetOptions(win32.SYMOPT_LOAD_LINES)
return true
}
_destroy :: proc "contextless" (ctx: ^Context) -> bool {
if ctx != nil {
win32.SymCleanup(ctx.impl.hProcess)
}
return true
}
_frames :: proc "contextless" (ctx: ^Context, skip: uint, frames_buffer: []Frame) -> []Frame {
frame_count := win32.RtlCaptureStackBackTrace(u32(skip) + 2, u32(len(frames_buffer)), ([^]rawptr)(&frames_buffer[0]), nil)
for i in 0..<frame_count {
// NOTE: Return address is one after the call instruction so subtract a byte to
// end up back inside the call instruction which is needed for SymFromAddr.
frames_buffer[i] -= 1
}
return frames_buffer[:frame_count]
}
_resolve :: proc(ctx: ^Context, frame: Frame, allocator: runtime.Allocator) -> (fl: Frame_Location) {
intrinsics.atomic_store(&ctx.in_resolve, true)
defer intrinsics.atomic_store(&ctx.in_resolve, false)
// NOTE(bill): Dbghelp is not thread-safe
win32.AcquireSRWLockExclusive(&ctx.impl.lock)
defer win32.ReleaseSRWLockExclusive(&ctx.impl.lock)
data: [size_of(win32.SYMBOL_INFOW) + size_of([256]win32.WCHAR)]byte
symbol := (^win32.SYMBOL_INFOW)(&data[0])
symbol.SizeOfStruct = size_of(symbol)
symbol.MaxNameLen = 255
if win32.SymFromAddrW(ctx.impl.hProcess, win32.DWORD64(frame), &{}, symbol) {
fl.procedure, _ = win32.wstring_to_utf8(&symbol.Name[0], -1, allocator)
} else {
fl.procedure = fmt.aprintf("(procedure: 0x%x)", frame, allocator=allocator)
}
line: win32.IMAGEHLP_LINE64
line.SizeOfStruct = size_of(line)
if win32.SymGetLineFromAddrW64(ctx.impl.hProcess, win32.DWORD64(frame), &{}, &line) {
fl.file_path, _ = win32.wstring_to_utf8(line.FileName, -1, allocator)
fl.line = i32(line.LineNumber)
}
return
}
+1 -1
View File
@@ -420,7 +420,7 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
data := rawptr(uintptr(v.data) + info.offsets[i]) data := rawptr(uintptr(v.data) + info.offsets[i])
the_value := any{data, id} the_value := any{data, id}
if is_omitempty(the_value) { if omitempty && is_omitempty(the_value) {
continue continue
} }
+1 -1
View File
@@ -348,7 +348,7 @@ json_name_from_tag_value :: proc(value: string) -> (json_name, extra: string) {
json_name = value json_name = value
if comma_index := strings.index_byte(json_name, ','); comma_index >= 0 { if comma_index := strings.index_byte(json_name, ','); comma_index >= 0 {
json_name = json_name[:comma_index] json_name = json_name[:comma_index]
extra = json_name[comma_index:] extra = value[1 + comma_index:]
} }
return return
} }
+22 -13
View File
@@ -120,11 +120,11 @@ register_user_formatter :: proc(id: typeid, formatter: User_Formatter) -> Regist
// //
// Returns: A formatted string. // Returns: A formatted string.
// //
@(require_results)
aprint :: proc(args: ..any, sep := " ", allocator := context.allocator) -> string { aprint :: proc(args: ..any, sep := " ", allocator := context.allocator) -> string {
str: strings.Builder str: strings.Builder
strings.builder_init(&str, allocator) strings.builder_init(&str, allocator)
sbprint(&str, ..args, sep=sep) return sbprint(&str, ..args, sep=sep)
return strings.to_string(str)
} }
// Creates a formatted string with a newline character at the end // Creates a formatted string with a newline character at the end
// //
@@ -136,11 +136,11 @@ aprint :: proc(args: ..any, sep := " ", allocator := context.allocator) -> strin
// //
// Returns: A formatted string with a newline character at the end. // Returns: A formatted string with a newline character at the end.
// //
@(require_results)
aprintln :: proc(args: ..any, sep := " ", allocator := context.allocator) -> string { aprintln :: proc(args: ..any, sep := " ", allocator := context.allocator) -> string {
str: strings.Builder str: strings.Builder
strings.builder_init(&str, allocator) strings.builder_init(&str, allocator)
sbprintln(&str, ..args, sep=sep) return sbprintln(&str, ..args, sep=sep)
return strings.to_string(str)
} }
// Creates a formatted string using a format string and arguments // Creates a formatted string using a format string and arguments
// //
@@ -153,11 +153,11 @@ aprintln :: proc(args: ..any, sep := " ", allocator := context.allocator) -> str
// //
// Returns: A formatted string. The returned string must be freed accordingly. // Returns: A formatted string. The returned string must be freed accordingly.
// //
@(require_results)
aprintf :: proc(fmt: string, args: ..any, allocator := context.allocator, newline := false) -> string { aprintf :: proc(fmt: string, args: ..any, allocator := context.allocator, newline := false) -> string {
str: strings.Builder str: strings.Builder
strings.builder_init(&str, allocator) strings.builder_init(&str, allocator)
sbprintf(&str, fmt, ..args, newline=newline) return sbprintf(&str, fmt, ..args, newline=newline)
return strings.to_string(str)
} }
// Creates a formatted string using a format string and arguments, followed by a newline. // Creates a formatted string using a format string and arguments, followed by a newline.
// //
@@ -169,6 +169,7 @@ aprintf :: proc(fmt: string, args: ..any, allocator := context.allocator, newlin
// //
// Returns: A formatted string. The returned string must be freed accordingly. // Returns: A formatted string. The returned string must be freed accordingly.
// //
@(require_results)
aprintfln :: proc(fmt: string, args: ..any, allocator := context.allocator) -> string { aprintfln :: proc(fmt: string, args: ..any, allocator := context.allocator) -> string {
return aprintf(fmt, ..args, allocator=allocator, newline=true) return aprintf(fmt, ..args, allocator=allocator, newline=true)
} }
@@ -182,11 +183,11 @@ aprintfln :: proc(fmt: string, args: ..any, allocator := context.allocator) -> s
// //
// Returns: A formatted string. // Returns: A formatted string.
// //
@(require_results)
tprint :: proc(args: ..any, sep := " ") -> string { tprint :: proc(args: ..any, sep := " ") -> string {
str: strings.Builder str: strings.Builder
strings.builder_init(&str, context.temp_allocator) strings.builder_init(&str, context.temp_allocator)
sbprint(&str, ..args, sep=sep) return sbprint(&str, ..args, sep=sep)
return strings.to_string(str)
} }
// Creates a formatted string with a newline character at the end // Creates a formatted string with a newline character at the end
// //
@@ -198,11 +199,11 @@ tprint :: proc(args: ..any, sep := " ") -> string {
// //
// Returns: A formatted string with a newline character at the end. // Returns: A formatted string with a newline character at the end.
// //
@(require_results)
tprintln :: proc(args: ..any, sep := " ") -> string { tprintln :: proc(args: ..any, sep := " ") -> string {
str: strings.Builder str: strings.Builder
strings.builder_init(&str, context.temp_allocator) strings.builder_init(&str, context.temp_allocator)
sbprintln(&str, ..args, sep=sep) return sbprintln(&str, ..args, sep=sep)
return strings.to_string(str)
} }
// Creates a formatted string using a format string and arguments // Creates a formatted string using a format string and arguments
// //
@@ -215,11 +216,11 @@ tprintln :: proc(args: ..any, sep := " ") -> string {
// //
// Returns: A formatted string. // Returns: A formatted string.
// //
@(require_results)
tprintf :: proc(fmt: string, args: ..any, newline := false) -> string { tprintf :: proc(fmt: string, args: ..any, newline := false) -> string {
str: strings.Builder str: strings.Builder
strings.builder_init(&str, context.temp_allocator) strings.builder_init(&str, context.temp_allocator)
sbprintf(&str, fmt, ..args, newline=newline) return sbprintf(&str, fmt, ..args, newline=newline)
return strings.to_string(str)
} }
// Creates a formatted string using a format string and arguments, followed by a newline. // Creates a formatted string using a format string and arguments, followed by a newline.
// //
@@ -231,6 +232,7 @@ tprintf :: proc(fmt: string, args: ..any, newline := false) -> string {
// //
// Returns: A formatted string. // Returns: A formatted string.
// //
@(require_results)
tprintfln :: proc(fmt: string, args: ..any) -> string { tprintfln :: proc(fmt: string, args: ..any) -> string {
return tprintf(fmt, ..args, newline=true) return tprintf(fmt, ..args, newline=true)
} }
@@ -339,6 +341,7 @@ panicf :: proc(fmt: string, args: ..any, loc := #caller_location) -> ! {
// //
// Returns: A formatted C string // Returns: A formatted C string
// //
@(require_results)
caprintf :: proc(format: string, args: ..any, newline := false) -> cstring { caprintf :: proc(format: string, args: ..any, newline := false) -> cstring {
str: strings.Builder str: strings.Builder
strings.builder_init(&str) strings.builder_init(&str)
@@ -357,6 +360,7 @@ caprintf :: proc(format: string, args: ..any, newline := false) -> cstring {
// //
// Returns: A formatted C string // Returns: A formatted C string
// //
@(require_results)
caprintfln :: proc(format: string, args: ..any) -> cstring { caprintfln :: proc(format: string, args: ..any) -> cstring {
return caprintf(format, ..args, newline=true) return caprintf(format, ..args, newline=true)
} }
@@ -371,6 +375,7 @@ caprintfln :: proc(format: string, args: ..any) -> cstring {
// //
// Returns: A formatted C string // Returns: A formatted C string
// //
@(require_results)
ctprintf :: proc(format: string, args: ..any, newline := false) -> cstring { ctprintf :: proc(format: string, args: ..any, newline := false) -> cstring {
str: strings.Builder str: strings.Builder
strings.builder_init(&str, context.temp_allocator) strings.builder_init(&str, context.temp_allocator)
@@ -389,6 +394,7 @@ ctprintf :: proc(format: string, args: ..any, newline := false) -> cstring {
// //
// Returns: A formatted C string // Returns: A formatted C string
// //
@(require_results)
ctprintfln :: proc(format: string, args: ..any) -> cstring { ctprintfln :: proc(format: string, args: ..any) -> cstring {
return ctprintf(format, ..args, newline=true) return ctprintf(format, ..args, newline=true)
} }
@@ -2526,8 +2532,11 @@ fmt_bit_field :: proc(fi: ^Info, v: any, verb: rune, info: runtime.Type_Info_Bit
bit_offset := info.bit_offsets[i] bit_offset := info.bit_offsets[i]
bit_size := info.bit_sizes[i] bit_size := info.bit_sizes[i]
value := read_bits(([^]byte)(v.data), bit_offset, bit_size)
type := info.types[i] type := info.types[i]
value := read_bits(([^]byte)(v.data), bit_offset, bit_size)
if reflect.is_endian_big(type) {
value <<= u64(8*type.size) - u64(bit_size)
}
if !reflect.is_unsigned(runtime.type_info_core(type)) { if !reflect.is_unsigned(runtime.type_info_core(type)) {
// Sign Extension // Sign Extension
+51
View File
@@ -1,7 +1,9 @@
//+build js //+build js
package fmt package fmt
import "core:bufio"
import "core:io" import "core:io"
import "core:os"
foreign import "odin_env" foreign import "odin_env"
@@ -31,6 +33,55 @@ stderr := io.Writer{
data = rawptr(uintptr(2)), data = rawptr(uintptr(2)),
} }
@(private="file")
fd_to_writer :: proc(fd: os.Handle, loc := #caller_location) -> io.Writer {
switch fd {
case 1: return stdout
case 2: return stderr
case: panic("`fmt.fprint` variant called with invalid file descriptor for JS, only 1 (stdout) and 2 (stderr) are supported", loc)
}
}
// fprint formats using the default print settings and writes to fd
fprint :: proc(fd: os.Handle, args: ..any, sep := " ", flush := true, loc := #caller_location) -> int {
buf: [1024]byte
b: bufio.Writer
defer bufio.writer_flush(&b)
bufio.writer_init_with_buf(&b, fd_to_writer(fd, loc), buf[:])
w := bufio.writer_to_writer(&b)
return wprint(w, ..args, sep=sep, flush=flush)
}
// fprintln formats using the default print settings and writes to fd
fprintln :: proc(fd: os.Handle, args: ..any, sep := " ", flush := true, loc := #caller_location) -> int {
buf: [1024]byte
b: bufio.Writer
defer bufio.writer_flush(&b)
bufio.writer_init_with_buf(&b, fd_to_writer(fd, loc), buf[:])
w := bufio.writer_to_writer(&b)
return wprintln(w, ..args, sep=sep, flush=flush)
}
// fprintf formats according to the specified format string and writes to fd
fprintf :: proc(fd: os.Handle, fmt: string, args: ..any, flush := true, newline := false, loc := #caller_location) -> int {
buf: [1024]byte
b: bufio.Writer
defer bufio.writer_flush(&b)
bufio.writer_init_with_buf(&b, fd_to_writer(fd, loc), buf[:])
w := bufio.writer_to_writer(&b)
return wprintf(w, fmt, ..args, flush=flush, newline=newline)
}
// fprintfln formats according to the specified format string and writes to fd, followed by a newline.
fprintfln :: proc(fd: os.Handle, fmt: string, args: ..any, flush := true, loc := #caller_location) -> int {
return fprintf(fd, fmt, ..args, flush=flush, newline=true, loc=loc)
}
// print formats using the default print settings and writes to stdout // print formats using the default print settings and writes to stdout
print :: proc(args: ..any, sep := " ", flush := true) -> int { return wprint(w=stdout, args=args, sep=sep, flush=flush) } print :: proc(args: ..any, sep := " ", flush := true) -> int { return wprint(w=stdout, args=args, sep=sep, flush=flush) }
// println formats using the default print settings and writes to stdout // println formats using the default print settings and writes to stdout
+2 -2
View File
@@ -1724,7 +1724,7 @@ quatFromMat4 :: proc "c" (m: mat4) -> (q: quat) {
@(require_results) @(require_results)
quatMulVec3 :: proc "c" (q: quat, v: vec3) -> vec3 { quatMulVec3 :: proc "c" (q: quat, v: vec3) -> vec3 {
xyz := vec3{q.x, q.y, q.z} xyz := vec3{q.x, q.y, q.z}
t := cross(xyz, v) t := cross(2.0 * xyz, v)
return v + q.w*t + cross(xyz, t) return v + q.w*t + cross(xyz, t)
} }
@@ -1832,7 +1832,7 @@ dquatFromDmat4 :: proc "c" (m: dmat4) -> (q: dquat) {
@(require_results) @(require_results)
dquatMulDvec3 :: proc "c" (q: dquat, v: dvec3) -> dvec3 { dquatMulDvec3 :: proc "c" (q: dquat, v: dvec3) -> dvec3 {
xyz := dvec3{q.x, q.y, q.z} xyz := dvec3{q.x, q.y, q.z}
t := cross(xyz, v) t := cross(2.0 * xyz, v)
return v + q.w*t + cross(xyz, t) return v + q.w*t + cross(xyz, t)
} }
+20 -17
View File
@@ -5,6 +5,7 @@ Package core:math/rand implements various random number generators
package rand package rand
import "base:intrinsics" import "base:intrinsics"
import "core:crypto"
import "core:math" import "core:math"
import "core:mem" import "core:mem"
@@ -105,26 +106,29 @@ init :: proc(r: ^Rand, seed: u64) {
/* /*
Initialises a random number generator to use the system random number generator. Initialises a random number generator to use the system random number generator.
The system random number generator is platform specific. The system random number generator is platform specific, and not supported
On `linux` refer to the `getrandom` syscall. on all targets.
On `darwin` refer to `getentropy`.
On `windows` refer to `BCryptGenRandom`.
All other platforms are not supported
Inputs: Inputs:
- r: The random number generator to use the system random number generator - r: The random number generator to use the system random number generator
WARNING: Panics if the system is not either `windows`, `darwin` or `linux` WARNING: Panics if the system random number generator is not supported.
Support can be determined via the `core:crypto.HAS_RAND_BYTES` constant.
Example: Example:
import "core:crypto"
import "core:math/rand" import "core:math/rand"
import "core:fmt" import "core:fmt"
init_as_system_example :: proc() { init_as_system_example :: proc() {
my_rand: rand.Rand my_rand: rand.Rand
rand.init_as_system(&my_rand) switch crypto.HAS_RAND_BYTES {
fmt.println(rand.uint64(&my_rand)) case true:
rand.init_as_system(&my_rand)
fmt.println(rand.uint64(&my_rand))
case false:
fmt.println("system random not supported!")
}
} }
Possible Output: Possible Output:
@@ -133,7 +137,7 @@ Possible Output:
*/ */
init_as_system :: proc(r: ^Rand) { init_as_system :: proc(r: ^Rand) {
if !#defined(_system_random) { if !crypto.HAS_RAND_BYTES {
panic(#procedure + " is not supported on this platform yet") panic(#procedure + " is not supported on this platform yet")
} }
r.state = 0 r.state = 0
@@ -144,15 +148,14 @@ init_as_system :: proc(r: ^Rand) {
@(private) @(private)
_random_u64 :: proc(r: ^Rand) -> u64 { _random_u64 :: proc(r: ^Rand) -> u64 {
r := r r := r
if r == nil { switch {
case r == nil:
r = &global_rand r = &global_rand
case r.is_system:
value: u64
crypto.rand_bytes((cast([^]u8)&value)[:size_of(u64)])
return value
} }
when #defined(_system_random) {
if r.is_system {
return _system_random()
}
}
old_state := r.state old_state := r.state
r.state = old_state * 6364136223846793005 + (r.inc|1) r.state = old_state * 6364136223846793005 + (r.inc|1)
-22
View File
@@ -1,22 +0,0 @@
package rand
import "core:sys/darwin"
@(require_results)
_system_random :: proc() -> u64 {
for {
value: u64
ret := darwin.syscall_getentropy(([^]u8)(&value), size_of(value))
if ret < 0 {
switch ret {
case -4: // EINTR
continue
case -78: // ENOSYS
panic("getentropy not available in kernel")
case:
panic("getentropy failed")
}
}
return value
}
}
-14
View File
@@ -1,14 +0,0 @@
package rand
foreign import "odin_env"
foreign odin_env {
@(link_name = "rand_bytes")
env_rand_bytes :: proc "contextless" (buf: []byte) ---
}
@(require_results)
_system_random :: proc() -> u64 {
buf: [8]u8
env_rand_bytes(buf[:])
return transmute(u64)buf
}
-29
View File
@@ -1,29 +0,0 @@
package rand
import "core:sys/linux"
@(require_results)
_system_random :: proc() -> u64 {
for {
value: u64
value_buf := (cast([^]u8)&value)[:size_of(u64)]
_, errno := linux.getrandom(value_buf, {})
#partial switch errno {
case .NONE:
// Do nothing
case .EINTR:
// Call interupted by a signal handler, just retry the request.
continue
case .ENOSYS:
// The kernel is apparently prehistoric (< 3.17 circa 2014)
// and does not support getrandom.
panic("getrandom not available in kernel")
case:
// All other failures are things that should NEVER happen
// unless the kernel interface changes (ie: the Linux
// developers break userland).
panic("getrandom failed")
}
return value
}
}
-13
View File
@@ -1,13 +0,0 @@
package rand
import win32 "core:sys/windows"
@(require_results)
_system_random :: proc() -> u64 {
value: u64
status := win32.BCryptGenRandom(nil, ([^]u8)(&value), size_of(value), win32.BCRYPT_USE_SYSTEM_PREFERRED_RNG)
if status < 0 {
panic("BCryptGenRandom failed")
}
return value
}
+1 -1
View File
@@ -1124,7 +1124,7 @@ buddy_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
case .Query_Info: case .Query_Info:
info := (^Allocator_Query_Info)(old_memory) info := (^Allocator_Query_Info)(old_memory)
if info != nil && info.pointer != nil { if info != nil && info.pointer != nil {
ptr := old_memory ptr := info.pointer
if !(b.head <= ptr && ptr <= b.tail) { if !(b.head <= ptr && ptr <= b.tail) {
return nil, .Invalid_Pointer return nil, .Invalid_Pointer
} }
+32
View File
@@ -22,6 +22,13 @@ Tracking_Allocator :: struct {
bad_free_array: [dynamic]Tracking_Allocator_Bad_Free_Entry, bad_free_array: [dynamic]Tracking_Allocator_Bad_Free_Entry,
mutex: sync.Mutex, mutex: sync.Mutex,
clear_on_free_all: bool, clear_on_free_all: bool,
total_memory_allocated: i64,
total_allocation_count: i64,
total_memory_freed: i64,
total_free_count: i64,
peak_memory_allocated: i64,
current_memory_allocated: i64,
} }
tracking_allocator_init :: proc(t: ^Tracking_Allocator, backing_allocator: Allocator, internals_allocator := context.allocator) { tracking_allocator_init :: proc(t: ^Tracking_Allocator, backing_allocator: Allocator, internals_allocator := context.allocator) {
@@ -44,6 +51,7 @@ tracking_allocator_clear :: proc(t: ^Tracking_Allocator) {
sync.mutex_lock(&t.mutex) sync.mutex_lock(&t.mutex)
clear(&t.allocation_map) clear(&t.allocation_map)
clear(&t.bad_free_array) clear(&t.bad_free_array)
t.current_memory_allocated = 0
sync.mutex_unlock(&t.mutex) sync.mutex_unlock(&t.mutex)
} }
@@ -59,6 +67,21 @@ tracking_allocator :: proc(data: ^Tracking_Allocator) -> Allocator {
tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int, size, alignment: int,
old_memory: rawptr, old_size: int, loc := #caller_location) -> (result: []byte, err: Allocator_Error) { old_memory: rawptr, old_size: int, loc := #caller_location) -> (result: []byte, err: Allocator_Error) {
track_alloc :: proc(data: ^Tracking_Allocator, entry: ^Tracking_Allocator_Entry) {
data.total_memory_allocated += i64(entry.size)
data.total_allocation_count += 1
data.current_memory_allocated += i64(entry.size)
if data.current_memory_allocated > data.peak_memory_allocated {
data.peak_memory_allocated = data.current_memory_allocated
}
}
track_free :: proc(data: ^Tracking_Allocator, entry: ^Tracking_Allocator_Entry) {
data.total_memory_freed += i64(entry.size)
data.total_free_count += 1
data.current_memory_allocated -= i64(entry.size)
}
data := (^Tracking_Allocator)(allocator_data) data := (^Tracking_Allocator)(allocator_data)
sync.mutex_guard(&data.mutex) sync.mutex_guard(&data.mutex)
@@ -100,13 +123,21 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
err = err, err = err,
location = loc, location = loc,
} }
track_alloc(data, &data.allocation_map[result_ptr])
case .Free: case .Free:
if old_memory != nil && old_memory in data.allocation_map {
track_free(data, &data.allocation_map[old_memory])
}
delete_key(&data.allocation_map, old_memory) delete_key(&data.allocation_map, old_memory)
case .Free_All: case .Free_All:
if data.clear_on_free_all { if data.clear_on_free_all {
clear_map(&data.allocation_map) clear_map(&data.allocation_map)
data.current_memory_allocated = 0
} }
case .Resize, .Resize_Non_Zeroed: case .Resize, .Resize_Non_Zeroed:
if old_memory != nil && old_memory in data.allocation_map {
track_free(data, &data.allocation_map[old_memory])
}
if old_memory != result_ptr { if old_memory != result_ptr {
delete_key(&data.allocation_map, old_memory) delete_key(&data.allocation_map, old_memory)
} }
@@ -118,6 +149,7 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
err = err, err = err,
location = loc, location = loc,
} }
track_alloc(data, &data.allocation_map[result_ptr])
case .Query_Features: case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory) set := (^Allocator_Mode_Set)(old_memory)
+2 -9
View File
@@ -1,9 +1,7 @@
//+build js //+build js
package os package os
import "base:intrinsics"
import "base:runtime" import "base:runtime"
import "core:unicode/utf16"
is_path_separator :: proc(c: byte) -> bool { is_path_separator :: proc(c: byte) -> bool {
return c == '/' || c == '\\' return c == '/' || c == '\\'
@@ -64,13 +62,8 @@ write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Errno)
unimplemented("core:os procedure not supported on JS target") unimplemented("core:os procedure not supported on JS target")
} }
stdout: Handle = 1
stderr: Handle = 2
// NOTE(bill): Uses startup to initialize it
//stdin := get_std_handle(uint(win32.STD_INPUT_HANDLE))
//stdout := get_std_handle(uint(win32.STD_OUTPUT_HANDLE))
//stderr := get_std_handle(uint(win32.STD_ERROR_HANDLE))
get_std_handle :: proc "contextless" (h: uint) -> Handle { get_std_handle :: proc "contextless" (h: uint) -> Handle {
context = runtime.default_context() context = runtime.default_context()
+61
View File
@@ -408,7 +408,68 @@ is_relative_multi_pointer :: proc(info: ^Type_Info) -> bool {
} }
@(require_results)
is_endian_platform :: proc(info: ^Type_Info) -> bool {
if info == nil { return false}
info := info
info = type_info_core(info)
#partial switch v in info.variant {
case Type_Info_Integer:
return v.endianness == .Platform
case Type_Info_Bit_Set:
if v.underlying != nil {
return is_endian_platform(v.underlying)
}
return true
case Type_Info_Pointer:
return true
}
return false
}
@(require_results)
is_endian_little :: proc(info: ^Type_Info) -> bool {
if info == nil { return false}
info := info
info = type_info_core(info)
#partial switch v in info.variant {
case Type_Info_Integer:
if v.endianness == .Platform {
return ODIN_ENDIAN == .Little
}
return v.endianness == .Little
case Type_Info_Bit_Set:
if v.underlying != nil {
return is_endian_platform(v.underlying)
}
return ODIN_ENDIAN == .Little
case Type_Info_Pointer:
return ODIN_ENDIAN == .Little
}
return ODIN_ENDIAN == .Little
}
@(require_results)
is_endian_big :: proc(info: ^Type_Info) -> bool {
if info == nil { return false}
info := info
info = type_info_core(info)
#partial switch v in info.variant {
case Type_Info_Integer:
if v.endianness == .Platform {
return ODIN_ENDIAN == .Big
}
return v.endianness == .Big
case Type_Info_Bit_Set:
if v.underlying != nil {
return is_endian_platform(v.underlying)
}
return ODIN_ENDIAN == .Big
case Type_Info_Pointer:
return ODIN_ENDIAN == .Big
}
return ODIN_ENDIAN == .Big
}
+2
View File
@@ -75,6 +75,7 @@ create_raw_unbuffered :: proc(#any_int msg_size, msg_alignment: int, allocator:
ptr := mem.alloc(size, align, allocator) or_return ptr := mem.alloc(size, align, allocator) or_return
c = (^Raw_Chan)(ptr) c = (^Raw_Chan)(ptr)
c.allocator = allocator
c.allocation_size = size c.allocation_size = size
c.unbuffered_data = ([^]byte)(ptr)[offset:] c.unbuffered_data = ([^]byte)(ptr)[offset:]
c.msg_size = u16(msg_size) c.msg_size = u16(msg_size)
@@ -99,6 +100,7 @@ create_raw_buffered :: proc(#any_int msg_size, msg_alignment: int, #any_int cap:
ptr := mem.alloc(size, align, allocator) or_return ptr := mem.alloc(size, align, allocator) or_return
c = (^Raw_Chan)(ptr) c = (^Raw_Chan)(ptr)
c.allocator = allocator
c.allocation_size = size c.allocation_size = size
bptr := ([^]byte)(ptr) bptr := ([^]byte)(ptr)
@@ -337,7 +337,7 @@ syscall_ftruncate :: #force_inline proc "contextless" (fd: c.int, length: off_t)
return cast(c.int)intrinsics.syscall(unix_offset_syscall(.ftruncate), uintptr(fd), uintptr(length)) return cast(c.int)intrinsics.syscall(unix_offset_syscall(.ftruncate), uintptr(fd), uintptr(length))
} }
syscall_sysctl :: #force_inline proc "contextless" (name: ^c.int, namelen: c.uint, oldp: rawptr, oldlenp: ^i64, newp: ^i8, newlen: i64) -> c.int { syscall_sysctl :: #force_inline proc "contextless" (name: [^]c.int, namelen: c.size_t, oldp: rawptr, oldlenp: ^c.size_t, newp: rawptr, newlen: c.size_t) -> c.int {
return cast(c.int)intrinsics.syscall(unix_offset_syscall(.sysctl), uintptr(name), uintptr(namelen), uintptr(oldp), uintptr(oldlenp), uintptr(newp), uintptr(newlen)) return cast(c.int)intrinsics.syscall(unix_offset_syscall(.sysctl), uintptr(name), uintptr(namelen), uintptr(oldp), uintptr(oldlenp), uintptr(newp), uintptr(newlen))
} }
@@ -390,8 +390,8 @@ syscall_adjtime :: #force_inline proc "contextless" (delta: ^timeval, old_delta:
return cast(c.int)intrinsics.syscall(unix_offset_syscall(.adjtime), uintptr(delta), uintptr(old_delta)) return cast(c.int)intrinsics.syscall(unix_offset_syscall(.adjtime), uintptr(delta), uintptr(old_delta))
} }
syscall_sysctlbyname :: #force_inline proc "contextless" (name: cstring, oldp: rawptr, oldlenp: ^i64, newp: rawptr, newlen: i64) -> c.int { syscall_sysctlbyname :: #force_inline proc "contextless" (name: string, oldp: rawptr, oldlenp: ^c.size_t, newp: rawptr, newlen: c.size_t) -> c.int {
return cast(c.int)intrinsics.syscall(unix_offset_syscall(.sysctlbyname), transmute(uintptr)name, uintptr(oldp), uintptr(oldlenp), uintptr(newp), uintptr(newlen)) return cast(c.int)intrinsics.syscall(unix_offset_syscall(.sysctlbyname), uintptr(raw_data(name)), uintptr(len(name)), uintptr(oldp), uintptr(oldlenp), uintptr(newp), uintptr(newlen))
} }
syscall_proc_info :: #force_inline proc "contextless" (num: c.int, pid: u32, flavor: c.int, arg: u64, buffer: rawptr, buffer_size: c.int) -> c.int { syscall_proc_info :: #force_inline proc "contextless" (num: c.int, pid: u32, flavor: c.int, arg: u64, buffer: rawptr, buffer_size: c.int) -> c.int {
+58 -14
View File
@@ -1,26 +1,70 @@
//+build arm32, arm64 //+build arm32, arm64
package sysinfo package sysinfo
// TODO: Set up an enum with the ARM equivalent of the above. import "core:sys/unix"
CPU_Feature :: enum u64 {}
cpu_features: Maybe(CPU_Feature) _ :: unix
cpu_name: Maybe(string)
@(init, private) CPU_Feature :: enum u64 {
init_cpu_features :: proc "c" () { // Advanced SIMD & floating-point capabilities:
asimd, // General support for Advanced SIMD instructions/neon.
floatingpoint, // General support for floating-point instructions.
asimdhp, // Advanced SIMD half-precision conversion instructions.
bf16, // Storage and arithmetic instructions of the Brain Floating Point (BFloat16) data type.
fcma, // Floating-point complex number instructions.
fhm, // Floating-point half-precision multiplication instructions.
fp16, // General half-precision floating-point data processing instructions.
frint, // Floating-point to integral valued floating-point number rounding instructions.
i8mm, // Advanced SIMD int8 matrix multiplication instructions.
jscvt, // JavaScript conversion instruction.
rdm, // Advanced SIMD rounding double multiply accumulate instructions.
flagm, // Condition flag manipulation instructions.
flagm2, // Enhancements to condition flag manipulation instructions.
crc32, // CRC32 instructions.
lse, // Atomic instructions to support large systems.
lse2, // Changes to single-copy atomicity and alignment requirements for loads and stores for large systems.
lrcpc, // Load-acquire Release Consistency processor consistent (RCpc) instructions.
lrcpc2, // Load-acquire Release Consistency processor consistent (RCpc) instructions version 2.
aes,
pmull,
sha1,
sha256,
sha512,
sha3,
sb, // Barrier instruction to control speculation.
ssbs, // Instructions to control speculation of loads and stores.
} }
CPU_Features :: distinct bit_set[CPU_Feature; u64]
cpu_features: Maybe(CPU_Features)
cpu_name: Maybe(string)
@(private) @(private)
_cpu_name_buf: [72]u8 cpu_name_buf: [128]byte
@(init, private) @(init, private)
init_cpu_name :: proc "c" () { init_cpu_name :: proc "contextless" () {
when ODIN_ARCH == .arm32 { generic := true
copy(_cpu_name_buf[:], "ARM")
cpu_name = string(_cpu_name_buf[:3]) when ODIN_OS == .Darwin {
} else { if unix.sysctlbyname("machdep.cpu.brand_string", &cpu_name_buf) {
copy(_cpu_name_buf[:], "ARM64") cpu_name = string(cstring(rawptr(&cpu_name_buf)))
cpu_name = string(_cpu_name_buf[:5]) generic = false
}
}
if generic {
when ODIN_ARCH == .arm64 {
copy(cpu_name_buf[:], "ARM64")
cpu_name = string(cpu_name_buf[:len("ARM64")])
} else {
copy(cpu_name_buf[:], "ARM")
cpu_name = string(cpu_name_buf[:len("ARM")])
}
} }
} }
+98
View File
@@ -0,0 +1,98 @@
package sysinfo
import "core:sys/unix"
@(init, private)
init_cpu_features :: proc "contextless" () {
@(static) features: CPU_Features
defer cpu_features = features
try_set :: proc "contextless" (name: string, feature: CPU_Feature) -> (ok: bool) {
support: b32
if ok = unix.sysctlbyname(name, &support); ok && support {
features += { feature }
}
return
}
// Docs from Apple: https://developer.apple.com/documentation/kernel/1387446-sysctlbyname/determining_instruction_set_characteristics
// Features from there that do not have (or I didn't find) an equivalent on Linux are commented out below.
// Advanced SIMD & floating-point capabilities:
{
if !try_set("hw.optional.AdvSIMD", .asimd) {
try_set("hw.optional.neon", .asimd)
}
try_set("hw.optional.floatingpoint", .floatingpoint)
if !try_set("hw.optional.AdvSIMD_HPFPCvt", .asimdhp) {
try_set("hw.optional.neon_hpfp", .asimdhp)
}
try_set("hw.optional.arm.FEAT_BF16", .bf16)
// try_set("hw.optional.arm.FEAT_DotProd", .dotprod)
if !try_set("hw.optional.arm.FEAT_FCMA", .fcma) {
try_set("hw.optional.armv8_3_compnum", .fcma)
}
if !try_set("hw.optional.arm.FEAT_FHM", .fhm) {
try_set("hw.optional.armv8_2_fhm", .fhm)
}
if !try_set("hw.optional.arm.FEAT_FP16", .fp16) {
try_set("hw.optional.neon_fp16", .fp16)
}
try_set("hw.optional.arm.FEAT_FRINTTS", .frint)
try_set("hw.optional.arm.FEAT_I8MM", .i8mm)
try_set("hw.optional.arm.FEAT_JSCVT", .jscvt)
try_set("hw.optional.arm.FEAT_RDM", .rdm)
}
// Integer capabilities:
{
try_set("hw.optional.arm.FEAT_FlagM", .flagm)
try_set("hw.optional.arm.FEAT_FlagM2", .flagm2)
try_set("hw.optional.armv8_crc32", .crc32)
}
// Atomic and memory ordering instruction capabilities:
{
try_set("hw.optional.arm.FEAT_LRCPC", .lrcpc)
try_set("hw.optional.arm.FEAT_LRCPC2", .lrcpc2)
if !try_set("hw.optional.arm.FEAT_LSE", .lse) {
try_set("hw.optional.armv8_1_atomics", .lse)
}
// try_set("hw.optional.arm.FEAT_LSE2", .lse2)
}
// Encryption capabilities:
{
try_set("hw.optional.arm.FEAT_AES", .aes)
try_set("hw.optional.arm.FEAT_PMULL", .pmull)
try_set("hw.optional.arm.FEAT_SHA1", .sha1)
try_set("hw.optional.arm.FEAT_SHA256", .sha256)
if !try_set("hw.optional.arm.FEAT_SHA512", .sha512) {
try_set("hw.optional.armv8_2_sha512", .sha512)
}
if !try_set("hw.optional.arm.FEAT_SHA3", .sha3) {
try_set("hw.optional.armv8_2_sha3", .sha3)
}
}
// General capabilities:
{
// try_set("hw.optional.arm.FEAT_BTI", .bti)
// try_set("hw.optional.arm.FEAT_DPB", .dpb)
// try_set("hw.optional.arm.FEAT_DPB2", .dpb2)
// try_set("hw.optional.arm.FEAT_ECV", .ecv)
try_set("hw.optional.arm.FEAT_SB", .sb)
try_set("hw.optional.arm.FEAT_SSBS", .ssbs)
}
}
+65
View File
@@ -0,0 +1,65 @@
//+build arm32, arm64
//+build linux
package sysinfo
import "core:sys/linux"
import "core:strings"
@(init, private)
init_cpu_features :: proc() {
fd, err := linux.open("/proc/cpuinfo", {})
if err != .NONE { return }
defer linux.close(fd)
// This is probably enough right?
buf: [4096]byte
n, rerr := linux.read(fd, buf[:])
if rerr != .NONE || n == 0 { return }
features: CPU_Features
defer cpu_features = features
str := string(buf[:n])
for line in strings.split_lines_iterator(&str) {
key, _, value := strings.partition(line, ":")
key = strings.trim_space(key)
value = strings.trim_space(value)
if key != "Features" { continue }
for feature in strings.split_by_byte_iterator(&value, ' ') {
switch feature {
case "asimd", "neon": features += { .asimd }
case "fp": features += { .floatingpoint }
case "asimdhp": features += { .asimdhp }
case "asimdbf16": features += { .bf16 }
case "fcma": features += { .fcma }
case "asimdfhm": features += { .fhm }
case "fphp", "half": features += { .fp16 }
case "frint": features += { .frint }
case "i8mm": features += { .i8mm }
case "jscvt": features += { .jscvt }
case "asimdrdm": features += { .rdm }
case "flagm": features += { .flagm }
case "flagm2": features += { .flagm2 }
case "crc32": features += { .crc32 }
case "atomics": features += { .lse }
case "lrcpc": features += { .lrcpc }
case "ilrcpc": features += { .lrcpc2 }
case "aes": features += { .aes }
case "pmull": features += { .pmull }
case "sha1": features += { .sha1 }
case "sha2": features += { .sha256 }
case "sha3": features += { .sha3 }
case "sha512": features += { .sha512 }
case "sb": features += { .sb }
case "ssbs": features += { .ssbs }
}
}
break
}
}
+15 -12
View File
@@ -19,18 +19,21 @@ Example:
import si "core:sys/info" import si "core:sys/info"
main :: proc() { main :: proc() {
fmt.printf("Odin: %v\n", ODIN_VERSION) fmt.printfln("Odin: %v", ODIN_VERSION)
fmt.printf("OS: %v\n", si.os_version.as_string) fmt.printfln("OS: %v", si.os_version.as_string)
fmt.printf("OS: %#v\n", si.os_version) fmt.printfln("OS: %#v", si.os_version)
fmt.printf("CPU: %v\n", si.cpu_name) fmt.printfln("CPU: %v", si.cpu_name)
fmt.printf("RAM: %v MiB\n", si.ram.total_ram / 1024 / 1024) fmt.printfln("RAM: %#.1M", si.ram.total_ram)
// fmt.printfln("Features: %v", si.cpu_features)
// fmt.printfln("MacOS version: %v", si.macos_version)
fmt.println() fmt.println()
for gpu, i in si.gpus { for gpu, i in si.gpus {
fmt.printf("GPU #%v:\n", i) fmt.printfln("GPU #%v:", i)
fmt.printf("\tVendor: %v\n", gpu.vendor_name) fmt.printfln("\tVendor: %v", gpu.vendor_name)
fmt.printf("\tModel: %v\n", gpu.model_name) fmt.printfln("\tModel: %v", gpu.model_name)
fmt.printf("\tVRAM: %v MiB\n", gpu.total_ram / 1024 / 1024) fmt.printfln("\tVRAM: %#.1M", gpu.total_ram)
} }
} }
@@ -51,11 +54,11 @@ Example:
as_string = "Windows 10 Professional (version: 20H2), build: 19042.1466", as_string = "Windows 10 Professional (version: 20H2), build: 19042.1466",
} }
CPU: AMD Ryzen 7 1800X Eight-Core Processor CPU: AMD Ryzen 7 1800X Eight-Core Processor
RAM: 65469 MiB RAM: 64.0 GiB
GPU #0: GPU #0:
Vendor: Advanced Micro Devices, Inc. Vendor: Advanced Micro Devices, Inc.
Model: Radeon RX Vega Model: Radeon RX Vega
VRAM: 8176 MiB VRAM: 8.0 GiB
- Example macOS output: - Example macOS output:
@@ -73,6 +76,6 @@ Example:
as_string = "macOS Monterey 12.4 (build 21F79, kernel 21.5.0)", as_string = "macOS Monterey 12.4 (build 21F79, kernel 21.5.0)",
} }
CPU: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz CPU: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
RAM: 8192 MiB RAM: 8.0 GiB
*/ */
package sysinfo package sysinfo
+1 -1
View File
@@ -65,7 +65,7 @@ init_os_version :: proc () {
os_version.as_string = strings.to_string(b) os_version.as_string = strings.to_string(b)
} }
@(init) @(init, private)
init_ram :: proc() { init_ram :: proc() {
// Retrieve RAM info using `sysctl` // Retrieve RAM info using `sysctl`
mib := []i32{sys.CTL_HW, sys.HW_PHYSMEM64} mib := []i32{sys.CTL_HW, sys.HW_PHYSMEM64}
+3 -2
View File
@@ -1,4 +1,3 @@
// +build darwin
package sysinfo package sysinfo
import sys "core:sys/unix" import sys "core:sys/unix"
@@ -76,6 +75,8 @@ init_os_version :: proc () {
os_version.minor = rel.darwin.y os_version.minor = rel.darwin.y
os_version.patch = rel.darwin.z os_version.patch = rel.darwin.z
macos_version = transmute(Version)rel.release.version
strings.write_string(&b, rel.os_name) strings.write_string(&b, rel.os_name)
if match == .Exact || match == .Nearest { if match == .Exact || match == .Nearest {
strings.write_rune(&b, ' ') strings.write_rune(&b, ' ')
@@ -113,7 +114,7 @@ init_os_version :: proc () {
os_version.as_string = strings.to_string(b) os_version.as_string = strings.to_string(b)
} }
@(init) @(init, private)
init_ram :: proc() { init_ram :: proc() {
// Retrieve RAM info using `sysctl` // Retrieve RAM info using `sysctl`
+1 -2
View File
@@ -1,4 +1,3 @@
// +build freebsd
package sysinfo package sysinfo
import sys "core:sys/unix" import sys "core:sys/unix"
@@ -68,7 +67,7 @@ init_os_version :: proc () {
} }
} }
@(init) @(init, private)
init_ram :: proc() { init_ram :: proc() {
// Retrieve RAM info using `sysctl` // Retrieve RAM info using `sysctl`
mib := []i32{sys.CTL_HW, sys.HW_PHYSMEM} mib := []i32{sys.CTL_HW, sys.HW_PHYSMEM}
+57 -46
View File
@@ -1,11 +1,9 @@
// +build linux
package sysinfo package sysinfo
import "base:intrinsics" import "base:intrinsics"
import "base:runtime"
import "core:strings"
import "core:strconv"
import "core:strconv"
import "core:strings"
import "core:sys/linux" import "core:sys/linux"
@(private) @(private)
@@ -14,32 +12,37 @@ version_string_buf: [1024]u8
@(init, private) @(init, private)
init_os_version :: proc () { init_os_version :: proc () {
os_version.platform = .Linux os_version.platform = .Linux
// Try to parse `/etc/os-release` for `PRETTY_NAME="Ubuntu 20.04.3 LTS`
fd, errno := linux.open("/etc/os-release", {.RDONLY}, {})
assert(errno == .NONE, "Failed to read /etc/os-release")
defer {
cerrno := linux.close(fd)
assert(cerrno == .NONE, "Failed to close the file descriptor")
}
os_release_buf: [2048]u8
n, read_errno := linux.read(fd, os_release_buf[:])
assert(read_errno == .NONE, "Failed to read data from /etc/os-release")
release := string(os_release_buf[:n])
// Search the line in the file until we find "PRETTY_NAME="
NEEDLE :: "PRETTY_NAME=\""
pretty_start := strings.index(release, NEEDLE)
b := strings.builder_from_bytes(version_string_buf[:]) b := strings.builder_from_bytes(version_string_buf[:])
if pretty_start > 0 {
for r, i in release[pretty_start + len(NEEDLE):] { // Try to parse `/etc/os-release` for `PRETTY_NAME="Ubuntu 20.04.3 LTS`
if r == '"' { {
strings.write_string(&b, release[pretty_start + len(NEEDLE):][:i]) fd, errno := linux.open("/etc/os-release", {})
break assert(errno == .NONE, "Failed to read /etc/os-release")
} else if r == '\r' || r == '\n' { defer {
strings.write_string(&b, "Unknown Linux Distro") cerrno := linux.close(fd)
break assert(cerrno == .NONE, "Failed to close the file descriptor")
}
os_release_buf: [2048]u8
n, read_errno := linux.read(fd, os_release_buf[:])
assert(read_errno == .NONE, "Failed to read data from /etc/os-release")
release := string(os_release_buf[:n])
// Search the line in the file until we find "PRETTY_NAME="
NEEDLE :: "PRETTY_NAME=\""
_, _, post := strings.partition(release, NEEDLE)
if len(post) > 0 {
end := strings.index_any(post, "\"\n")
if end > -1 && post[end] == '"' {
strings.write_string(&b, post[:end])
} }
} }
if strings.builder_len(b) == 0 {
strings.write_string(&b, "Unknown Linux Distro")
}
} }
// Grab kernel info using `uname()` syscall, https://linux.die.net/man/2/uname // Grab kernel info using `uname()` syscall, https://linux.die.net/man/2/uname
uts: linux.UTS_Name uts: linux.UTS_Name
uname_errno := linux.uname(&uts) uname_errno := linux.uname(&uts)
@@ -48,31 +51,39 @@ init_os_version :: proc () {
strings.write_string(&b, ", ") strings.write_string(&b, ", ")
strings.write_string(&b, string(cstring(&uts.sysname[0]))) strings.write_string(&b, string(cstring(&uts.sysname[0])))
strings.write_rune(&b, ' ') strings.write_rune(&b, ' ')
l := strings.builder_len(b)
release_i := strings.builder_len(b)
strings.write_string(&b, string(cstring(&uts.release[0]))) strings.write_string(&b, string(cstring(&uts.release[0])))
// Parse kernel version, as substrings of the version info in `version_string_buf` release_str := string(b.buf[release_i:])
runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
version_bits := strings.split_n(strings.to_string(b)[l:], "-", 2, context.temp_allocator) os_version.as_string = strings.to_string(b)
if len(version_bits) > 1 {
os_version.version = version_bits[1] // Parse the Linux version out of the release string
} {
// Parse major, minor, patch from release info version_num, _, version_suffix := strings.partition(release_str, "-")
triplet := strings.split(version_bits[0], ".", context.temp_allocator) os_version.version = version_suffix
if len(triplet) == 3 {
major, major_ok := strconv.parse_int(triplet[0]) i: int
minor, minor_ok := strconv.parse_int(triplet[1]) for part in strings.split_iterator(&version_num, ".") {
patch, patch_ok := strconv.parse_int(triplet[2]) defer i += 1
if major_ok && minor_ok && patch_ok {
os_version.major = major dst: ^int
os_version.minor = minor switch i {
os_version.patch = patch case 0: dst = &os_version.major
case 1: dst = &os_version.minor
case 2: dst = &os_version.patch
case: break
}
num, ok := strconv.parse_int(part)
if !ok { break }
dst^ = num
} }
} }
// Finish the string
os_version.as_string = strings.to_string(b)
} }
@(init) @(init, private)
init_ram :: proc() { init_ram :: proc() {
// Retrieve RAM info using `sysinfo` // Retrieve RAM info using `sysinfo`
sys_info: linux.Sys_Info sys_info: linux.Sys_Info
+1 -2
View File
@@ -1,4 +1,3 @@
// +build windows
package sysinfo package sysinfo
import sys "core:sys/windows" import sys "core:sys/windows"
@@ -259,7 +258,7 @@ init_os_version :: proc () {
} }
} }
@(init) @(init, private)
init_ram :: proc() { init_ram :: proc() {
state: sys.MEMORYSTATUSEX state: sys.MEMORYSTATUSEX
+8 -3
View File
@@ -8,6 +8,9 @@ os_version: OS_Version
ram: RAM ram: RAM
gpus: []GPU gpus: []GPU
// Only on MacOS, contains the actual MacOS version, while the `os_version` contains the kernel version.
macos_version: Version
OS_Version_Platform :: enum { OS_Version_Platform :: enum {
Unknown, Unknown,
Windows, Windows,
@@ -19,12 +22,14 @@ OS_Version_Platform :: enum {
NetBSD, NetBSD,
} }
Version :: struct {
major, minor, patch: int,
}
OS_Version :: struct { OS_Version :: struct {
platform: OS_Version_Platform, platform: OS_Version_Platform,
major: int, using _: Version,
minor: int,
patch: int,
build: [2]int, build: [2]int,
version: string, version: string,
+21 -18
View File
@@ -145,26 +145,29 @@ Errno :: enum i32 {
} }
/* /*
Bits for Open_Flags Bits for Open_Flags.
RDONLY flag is not present, because it has the value of 0, i.e. it is the
default, unless WRONLY or RDWR is specified.
*/ */
Open_Flags_Bits :: enum { Open_Flags_Bits :: enum {
RDONLY = 0, WRONLY = 0,
WRONLY = 1, RDWR = 1,
RDWR = 2, CREAT = 8,
CREAT = 6, EXCL = 9,
EXCL = 7, NOCTTY = 10,
NOCTTY = 8, TRUNC = 11,
TRUNC = 9, APPEND = 12,
APPEND = 10, NONBLOCK = 14,
NONBLOCK = 11, DSYNC = 16,
DSYNC = 12, ASYNC = 17,
ASYNC = 13, DIRECT = 18,
DIRECT = 14, LARGEFILE = 20,
DIRECTORY = 16, DIRECTORY = 21,
NOFOLLOW = 17, NOFOLLOW = 22,
NOATIME = 18, NOATIME = 24,
CLOEXEC = 19, CLOEXEC = 25,
PATH = 21, PATH = 28,
} }
/* /*
+18 -9
View File
@@ -1,20 +1,29 @@
//+build darwin //+build darwin
package unix package unix
import "core:sys/darwin"
import "base:intrinsics" import "base:intrinsics"
import "core:c"
import "core:sys/darwin"
_ :: darwin _ :: darwin
sysctl :: proc(mib: []i32, val: ^$T) -> (ok: bool) { sysctl :: proc "contextless" (mib: []i32, val: ^$T) -> (ok: bool) {
mib := mib result_size := c.size_t(size_of(T))
result_size := i64(size_of(T)) res := darwin.syscall_sysctl(
raw_data(mib), len(mib),
val, &result_size,
nil, 0,
)
return res == 0
}
res := intrinsics.syscall( sysctlbyname :: proc "contextless" (name: string, val: ^$T) -> (ok: bool) {
darwin.unix_offset_syscall(.sysctl), result_size := c.size_t(size_of(T))
uintptr(raw_data(mib)), uintptr(len(mib)), res := darwin.syscall_sysctlbyname(
uintptr(val), uintptr(&result_size), name,
uintptr(0), uintptr(0), val, &result_size,
nil, 0,
) )
return res == 0 return res == 0
} }
+38
View File
@@ -228,6 +228,38 @@ MINIDUMP_TYPE :: enum u32 {
ValidTypeFlags = 0x01ffffff, ValidTypeFlags = 0x01ffffff,
} }
SYMBOL_INFOW :: struct {
SizeOfStruct: ULONG,
TypeIndex: ULONG,
Reserved: [2]ULONG64,
Index: ULONG,
Size: ULONG,
ModBase: ULONG64,
Flags: ULONG,
Value: ULONG64,
Address: ULONG64,
Register: ULONG,
Scope: ULONG,
Tag: ULONG,
NameLen: ULONG,
MaxNameLen: ULONG,
Name: [1]WCHAR,
}
IMAGEHLP_LINE64 :: struct {
SizeOfStruct: DWORD,
Key: PVOID,
LineNumber: DWORD,
FileName: PWSTR,
Address: DWORD64,
}
PSYMBOL_INFOW :: ^SYMBOL_INFOW
PIMAGEHLP_LINEW64 :: ^IMAGEHLP_LINE64
SYMOPT_LOAD_LINES :: 0x00000010
@(default_calling_convention = "system") @(default_calling_convention = "system")
foreign Dbghelp { foreign Dbghelp {
MiniDumpWriteDump :: proc( MiniDumpWriteDump :: proc(
@@ -247,4 +279,10 @@ foreign Dbghelp {
StreamPointer: ^PVOID, StreamPointer: ^PVOID,
StreamSize: ^ULONG, StreamSize: ^ULONG,
) -> BOOL --- ) -> BOOL ---
SymInitialize :: proc(hProcess: HANDLE, UserSearchPath: PCSTR, fInvadeProcess: BOOL) -> BOOL ---
SymCleanup :: proc(hProcess: HANDLE) -> BOOL ---
SymSetOptions :: proc(SymOptions: DWORD) -> DWORD ---
SymFromAddrW :: proc(hProcess: HANDLE, Address: DWORD64, Displacement: PDWORD64, Symbol: PSYMBOL_INFOW) -> BOOL ---
SymGetLineFromAddrW64 :: proc(hProcess: HANDLE, dwAddr: DWORD64, pdwDisplacement: PDWORD, Line: PIMAGEHLP_LINEW64) -> BOOL ---
} }
+2
View File
@@ -460,6 +460,8 @@ foreign kernel32 {
PostQueuedCompletionStatus :: proc(CompletionPort: HANDLE, dwNumberOfBytesTransferred: DWORD, dwCompletionKey: c_ulong, lpOverlapped: ^OVERLAPPED) -> BOOL --- PostQueuedCompletionStatus :: proc(CompletionPort: HANDLE, dwNumberOfBytesTransferred: DWORD, dwCompletionKey: c_ulong, lpOverlapped: ^OVERLAPPED) -> BOOL ---
// [MS-Docs](https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-gethandleinformation) // [MS-Docs](https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-gethandleinformation)
GetHandleInformation :: proc(hObject: HANDLE, lpdwFlags: ^DWORD) -> BOOL --- GetHandleInformation :: proc(hObject: HANDLE, lpdwFlags: ^DWORD) -> BOOL ---
RtlCaptureStackBackTrace :: proc(FramesToSkip: ULONG, FramesToCapture: ULONG, BackTrace: [^]PVOID, BackTraceHash: PULONG) -> USHORT ---
} }
DEBUG_PROCESS :: 0x00000001 DEBUG_PROCESS :: 0x00000001
+4
View File
@@ -93,10 +93,14 @@ LONG32 :: i32
ULONG64 :: u64 ULONG64 :: u64
LONG64 :: i64 LONG64 :: i64
DWORD64 :: u64
PDWORD64 :: ^DWORD64
PDWORD_PTR :: ^DWORD_PTR PDWORD_PTR :: ^DWORD_PTR
ATOM :: distinct WORD ATOM :: distinct WORD
wstring :: [^]WCHAR wstring :: [^]WCHAR
PWSTR :: [^]WCHAR
PBYTE :: ^BYTE PBYTE :: ^BYTE
LPBYTE :: ^BYTE LPBYTE :: ^BYTE
+1
View File
@@ -126,6 +126,7 @@ foreign user32 {
CreatePopupMenu :: proc() -> HMENU --- CreatePopupMenu :: proc() -> HMENU ---
DestroyMenu :: proc(hMenu: HMENU) -> BOOL --- DestroyMenu :: proc(hMenu: HMENU) -> BOOL ---
AppendMenuW :: proc(hMenu: HMENU, uFlags: UINT, uIDNewItem: UINT_PTR, lpNewItem: LPCWSTR) -> BOOL --- AppendMenuW :: proc(hMenu: HMENU, uFlags: UINT, uIDNewItem: UINT_PTR, lpNewItem: LPCWSTR) -> BOOL ---
SetMenu :: proc(hWnd: HWND, hMenu: HMENU) -> BOOL ---
TrackPopupMenu :: proc(hMenu: HMENU, uFlags: UINT, x: int, y: int, nReserved: int, hWnd: HWND, prcRect: ^RECT) -> i32 --- TrackPopupMenu :: proc(hMenu: HMENU, uFlags: UINT, x: int, y: int, nReserved: int, hWnd: HWND, prcRect: ^RECT) -> i32 ---
RegisterWindowMessageW :: proc(lpString: LPCWSTR) -> UINT --- RegisterWindowMessageW :: proc(lpString: LPCWSTR) -> UINT ---
+20 -12
View File
@@ -8,6 +8,7 @@
// A Scanner may be customized to recognize only a subset of those literals and to recognize different identifiers and white space characters. // A Scanner may be customized to recognize only a subset of those literals and to recognize different identifiers and white space characters.
package text_scanner package text_scanner
import "base:runtime"
import "core:fmt" import "core:fmt"
import "core:strings" import "core:strings"
import "core:unicode" import "core:unicode"
@@ -23,10 +24,12 @@ Position :: struct {
} }
// position_is_valid reports where the position is valid // position_is_valid reports where the position is valid
@(require_results)
position_is_valid :: proc(pos: Position) -> bool { position_is_valid :: proc(pos: Position) -> bool {
return pos.line > 0 return pos.line > 0
} }
@(require_results)
position_to_string :: proc(pos: Position, allocator := context.temp_allocator) -> string { position_to_string :: proc(pos: Position, allocator := context.temp_allocator) -> string {
s := pos.filename s := pos.filename
if s == "" { if s == "" {
@@ -140,7 +143,7 @@ init :: proc(s: ^Scanner, src: string, filename := "") -> ^Scanner {
} }
@(private) @(private, require_results)
advance :: proc(s: ^Scanner) -> rune { advance :: proc(s: ^Scanner) -> rune {
if s.src_pos >= len(s.src) { if s.src_pos >= len(s.src) {
s.prev_char_len = 0 s.prev_char_len = 0
@@ -190,6 +193,7 @@ next :: proc(s: ^Scanner) -> rune {
// peek returns the next Unicode character in the source without advancing the scanner // peek returns the next Unicode character in the source without advancing the scanner
// It returns EOF if the scanner's position is at least the last character of the source // It returns EOF if the scanner's position is at least the last character of the source
// if n > 0, it call next n times and return the nth Unicode character and then restore the Scanner's state // if n > 0, it call next n times and return the nth Unicode character and then restore the Scanner's state
@(require_results)
peek :: proc(s: ^Scanner, n := 0) -> (ch: rune) { peek :: proc(s: ^Scanner, n := 0) -> (ch: rune) {
if s.ch == -2 { if s.ch == -2 {
s.ch = advance(s) s.ch = advance(s)
@@ -211,6 +215,7 @@ peek :: proc(s: ^Scanner, n := 0) -> (ch: rune) {
// peek returns the next token in the source // peek returns the next token in the source
// It returns EOF if the scanner's position is at least the last character of the source // It returns EOF if the scanner's position is at least the last character of the source
// if n > 0, it call next n times and return the nth token and then restore the Scanner's state // if n > 0, it call next n times and return the nth token and then restore the Scanner's state
@(require_results)
peek_token :: proc(s: ^Scanner, n := 0) -> (tok: rune) { peek_token :: proc(s: ^Scanner, n := 0) -> (tok: rune) {
assert(n >= 0) assert(n >= 0)
prev_s := s^ prev_s := s^
@@ -249,7 +254,7 @@ errorf :: proc(s: ^Scanner, format: string, args: ..any) {
error(s, fmt.tprintf(format, ..args)) error(s, fmt.tprintf(format, ..args))
} }
@(private) @(private, require_results)
is_ident_rune :: proc(s: ^Scanner, ch: rune, i: int) -> bool { is_ident_rune :: proc(s: ^Scanner, ch: rune, i: int) -> bool {
if s.is_ident_rune != nil { if s.is_ident_rune != nil {
return s.is_ident_rune(ch, i) return s.is_ident_rune(ch, i)
@@ -257,7 +262,7 @@ is_ident_rune :: proc(s: ^Scanner, ch: rune, i: int) -> bool {
return ch == '_' || unicode.is_letter(ch) || unicode.is_digit(ch) && i > 0 return ch == '_' || unicode.is_letter(ch) || unicode.is_digit(ch) && i > 0
} }
@(private) @(private, require_results)
scan_identifier :: proc(s: ^Scanner) -> rune { scan_identifier :: proc(s: ^Scanner) -> rune {
ch := advance(s) ch := advance(s)
for i := 1; is_ident_rune(s, ch, i); i += 1 { for i := 1; is_ident_rune(s, ch, i); i += 1 {
@@ -266,13 +271,13 @@ scan_identifier :: proc(s: ^Scanner) -> rune {
return ch return ch
} }
@(private) lower :: proc(ch: rune) -> rune { return ('a' - 'A') | ch } @(private, require_results) lower :: proc(ch: rune) -> rune { return ('a' - 'A') | ch }
@(private) is_decimal :: proc(ch: rune) -> bool { return '0' <= ch && ch <= '9' } @(private, require_results) is_decimal :: proc(ch: rune) -> bool { return '0' <= ch && ch <= '9' }
@(private) is_hex :: proc(ch: rune) -> bool { return '0' <= ch && ch <= '9' || 'a' <= lower(ch) && lower(ch) <= 'f' } @(private, require_results) is_hex :: proc(ch: rune) -> bool { return '0' <= ch && ch <= '9' || 'a' <= lower(ch) && lower(ch) <= 'f' }
@(private) @(private, require_results)
scan_number :: proc(s: ^Scanner, ch: rune, seen_dot: bool) -> (rune, rune) { scan_number :: proc(s: ^Scanner, ch: rune, seen_dot: bool) -> (rune, rune) {
lit_name :: proc(prefix: rune) -> string { lit_name :: proc(prefix: rune) -> string {
switch prefix { switch prefix {
@@ -417,7 +422,7 @@ scan_number :: proc(s: ^Scanner, ch: rune, seen_dot: bool) -> (rune, rune) {
return tok, ch return tok, ch
} }
@(private) @(private, require_results)
scan_string :: proc(s: ^Scanner, quote: rune) -> (n: int) { scan_string :: proc(s: ^Scanner, quote: rune) -> (n: int) {
digit_val :: proc(ch: rune) -> int { digit_val :: proc(ch: rune) -> int {
switch v := lower(ch); v { switch v := lower(ch); v {
@@ -484,7 +489,7 @@ scan_char :: proc(s: ^Scanner) {
} }
} }
@(private) @(private, require_results)
scan_comment :: proc(s: ^Scanner, ch: rune) -> rune { scan_comment :: proc(s: ^Scanner, ch: rune) -> rune {
ch := ch ch := ch
if ch == '/' { // line comment if ch == '/' { // line comment
@@ -563,13 +568,13 @@ scan :: proc(s: ^Scanner) -> (tok: rune) {
break break
case '"': case '"':
if .Scan_Strings in s.flags { if .Scan_Strings in s.flags {
scan_string(s, '"') _ = scan_string(s, '"')
tok = String tok = String
} }
ch = advance(s) ch = advance(s)
case '\'': case '\'':
if .Scan_Chars in s.flags { if .Scan_Chars in s.flags {
scan_string(s, '\'') _ = scan_string(s, '\'')
tok = Char tok = Char
} }
ch = advance(s) ch = advance(s)
@@ -611,6 +616,7 @@ scan :: proc(s: ^Scanner) -> (tok: rune) {
// position returns the position of the character immediately after the character or token returns by the previous call to next or scan // position returns the position of the character immediately after the character or token returns by the previous call to next or scan
// Use the Scanner's position field for the most recently scanned token position // Use the Scanner's position field for the most recently scanned token position
@(require_results)
position :: proc(s: ^Scanner) -> Position { position :: proc(s: ^Scanner) -> Position {
pos: Position pos: Position
pos.filename = s.pos.filename pos.filename = s.pos.filename
@@ -630,6 +636,7 @@ position :: proc(s: ^Scanner) -> Position {
} }
// token_text returns the string of the most recently scanned token // token_text returns the string of the most recently scanned token
@(require_results)
token_text :: proc(s: ^Scanner) -> string { token_text :: proc(s: ^Scanner) -> string {
if s.tok_pos < 0 { if s.tok_pos < 0 {
return "" return ""
@@ -639,7 +646,8 @@ token_text :: proc(s: ^Scanner) -> string {
// token_string returns a printable string for a token or Unicode character // token_string returns a printable string for a token or Unicode character
// By default, it uses the context.temp_allocator to produce the string // By default, it uses the context.temp_allocator to produce the string
token_string :: proc(tok: rune, allocator := context.temp_allocator) -> string { @(require_results)
token_string :: proc(tok: rune, allocator: runtime.Allocator) -> string {
context.allocator = allocator context.allocator = allocator
switch tok { switch tok {
case EOF: return strings.clone("EOF") case EOF: return strings.clone("EOF")
+5 -16
View File
@@ -1,21 +1,10 @@
//+private //+private
//+build darwin
package time package time
import "core:c" import "core:sys/unix"
foreign import libc "system:System.framework" _get_tsc_frequency :: proc "contextless" () -> (freq: u64, ok: bool) {
foreign libc { unix.sysctlbyname("machdep.tsc.frequency", &freq) or_return
@(link_name="sysctlbyname") _sysctlbyname :: proc(path: cstring, oldp: rawptr, oldlenp: rawptr, newp: rawptr, newlen: int) -> c.int --- ok = true
} return
_get_tsc_frequency :: proc "contextless" () -> (u64, bool) {
tmp_freq : u64 = 0
tmp_size : i64 = size_of(tmp_freq)
ret := _sysctlbyname("machdep.tsc.frequency", &tmp_freq, &tmp_size, nil, 0)
if ret < 0 {
return 0, false
}
return tmp_freq, true
} }
+1 -1
View File
@@ -2385,7 +2385,7 @@ matrix_type :: proc() {
c := a * b c := a * b
#assert(type_of(c) == matrix[2, 2]f32) #assert(type_of(c) == matrix[2, 2]f32)
fmt.tprintln("c = a * b", c) fmt.println("c = a * b", c)
} }
{ // Matrices support multiplication between matrices and arrays { // Matrices support multiplication between matrices and arrays
+20 -7
View File
@@ -204,14 +204,27 @@ gb_internal void report_cpu_info() {
} }
#elif defined(GB_CPU_ARM) #elif defined(GB_CPU_ARM)
/* bool generic = true;
TODO(Jeroen): On *nix, perhaps query `/proc/cpuinfo`.
*/ #if defined(GB_SYSTEM_OSX)
#if defined(GB_ARCH_64_BIT) char cpu_name[128] = {};
gb_printf("ARM64\n"); size_t cpu_name_size = 128;
#else if (sysctlbyname("machdep.cpu.brand_string", &cpu_name, &cpu_name_size, nullptr, 0) == 0) {
gb_printf("ARM\n"); generic = false;
gb_printf("%s\n", (char *)&cpu_name[0]);
}
#endif #endif
if (generic) {
/*
TODO(Jeroen): On *nix, perhaps query `/proc/cpuinfo`.
*/
#if defined(GB_ARCH_64_BIT)
gb_printf("ARM64\n");
#else
gb_printf("ARM\n");
#endif
}
#else #else
gb_printf("Unknown\n"); gb_printf("Unknown\n");
#endif #endif
+4 -2
View File
@@ -1596,8 +1596,10 @@ gb_internal void init_build_context(TargetMetrics *cross_target, Subtarget subta
if (bc->metrics.os == TargetOs_js || bc->metrics.os == TargetOs_wasi) { if (bc->metrics.os == TargetOs_js || bc->metrics.os == TargetOs_wasi) {
// TODO(bill): Should these even have a default "heap-like" allocator? // TODO(bill): Should these even have a default "heap-like" allocator?
} }
bc->ODIN_DEFAULT_TO_PANIC_ALLOCATOR = true;
bc->ODIN_DEFAULT_TO_NIL_ALLOCATOR = !bc->ODIN_DEFAULT_TO_PANIC_ALLOCATOR; if (!bc->ODIN_DEFAULT_TO_NIL_ALLOCATOR && !bc->ODIN_DEFAULT_TO_PANIC_ALLOCATOR) {
bc->ODIN_DEFAULT_TO_PANIC_ALLOCATOR = true;
}
} }
} }
+5 -5
View File
@@ -8079,11 +8079,10 @@ gb_internal void add_constant_switch_case(CheckerContext *ctx, SeenMap *seen, Op
} }
uintptr key = hash_exact_value(operand.value); uintptr key = hash_exact_value(operand.value);
TypeAndToken *found = map_get(seen, key); GB_ASSERT(key != 0);
if (found != nullptr) { isize count = multi_map_count(seen, key);
if (count) {
TEMPORARY_ALLOCATOR_GUARD(); TEMPORARY_ALLOCATOR_GUARD();
isize count = multi_map_count(seen, key);
TypeAndToken *taps = gb_alloc_array(temporary_allocator(), TypeAndToken, count); TypeAndToken *taps = gb_alloc_array(temporary_allocator(), TypeAndToken, count);
multi_map_get_all(seen, key, taps); multi_map_get_all(seen, key, taps);
@@ -9406,7 +9405,8 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast *
continue; continue;
} }
ExactValue v = f->Constant.value; ExactValue v = f->Constant.value;
auto found = map_get(&seen, hash_exact_value(v)); uintptr hash = hash_exact_value(v);
auto found = map_get(&seen, hash);
if (!found) { if (!found) {
array_add(&unhandled, f); array_add(&unhandled, f);
} }
+43 -14
View File
@@ -1629,6 +1629,17 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags)
if (build_context.no_rtti && is_type_enum(t->BitSet.elem)) { if (build_context.no_rtti && is_type_enum(t->BitSet.elem)) {
error(node, "Iteration over a bit_set of an enum is not allowed runtime type information (RTTI) has been disallowed"); error(node, "Iteration over a bit_set of an enum is not allowed runtime type information (RTTI) has been disallowed");
} }
if (rs->vals.count == 1 && rs->vals[0] && rs->vals[0]->kind == Ast_Ident) {
String name = rs->vals[0]->Ident.token.string;
Entity *found = scope_lookup(ctx->scope, name);
if (found && are_types_identical(found->type, t->BitSet.elem)) {
ERROR_BLOCK();
gbString s = expr_to_string(expr);
error(rs->vals[0], "'%.*s' shadows a previous declaration which might be ambiguous with 'for (%.*s in %s)'", LIT(name), LIT(name), s);
error_line("\tSuggestion: Use a different identifier if iteration is wanted, or surround in parentheses if a normal for loop is wanted\n");
gb_string_free(s);
}
}
break; break;
case Type_EnumeratedArray: case Type_EnumeratedArray:
@@ -1664,17 +1675,36 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags)
if (is_reverse) { if (is_reverse) {
error(node, "#reverse for is not supported for map types, as maps are unordered"); error(node, "#reverse for is not supported for map types, as maps are unordered");
} }
if (rs->vals.count == 1 && rs->vals[0] && rs->vals[0]->kind == Ast_Ident) {
String name = rs->vals[0]->Ident.token.string;
Entity *found = scope_lookup(ctx->scope, name);
if (found && are_types_identical(found->type, t->Map.key)) {
ERROR_BLOCK();
gbString s = expr_to_string(expr);
error(rs->vals[0], "'%.*s' shadows a previous declaration which might be ambiguous with 'for (%.*s in %s)'", LIT(name), LIT(name), s);
error_line("\tSuggestion: Use a different identifier if iteration is wanted, or surround in parentheses if a normal for loop is wanted\n");
gb_string_free(s);
}
}
break; break;
case Type_Tuple: case Type_Tuple:
{ {
isize count = t->Tuple.variables.count; isize count = t->Tuple.variables.count;
if (count < 1 || count > 3) { if (count < 1) {
ERROR_BLOCK(); ERROR_BLOCK();
check_not_tuple(ctx, &operand); check_not_tuple(ctx, &operand);
error_line("\tMultiple return valued parameters in a range statement are limited to a maximum of 2 usable values with a trailing boolean for the conditional\n"); error_line("\tMultiple return valued parameters in a range statement are limited to a minimum of 1 usable values with a trailing boolean for the conditional, got %td\n", count);
break; break;
} }
enum : isize {MAXIMUM_COUNT = 100};
if (count > MAXIMUM_COUNT) {
ERROR_BLOCK();
check_not_tuple(ctx, &operand);
error_line("\tMultiple return valued parameters in a range statement are limited to a maximum of %td usable values with a trailing boolean for the conditional, got %td\n", MAXIMUM_COUNT, count);
break;
}
Type *cond_type = t->Tuple.variables[count-1]->type; Type *cond_type = t->Tuple.variables[count-1]->type;
if (!is_type_boolean(cond_type)) { if (!is_type_boolean(cond_type)) {
gbString s = type_to_string(cond_type); gbString s = type_to_string(cond_type);
@@ -1683,24 +1713,23 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags)
break; break;
} }
max_val_count = count;
for (Entity *e : t->Tuple.variables) { for (Entity *e : t->Tuple.variables) {
array_add(&vals, e->type); array_add(&vals, e->type);
} }
is_possibly_addressable = false; is_possibly_addressable = false;
if (rs->vals.count > 1 && rs->vals[1] != nullptr && count < 3) { bool do_break = false;
gbString s = type_to_string(t); for (isize i = rs->vals.count-1; i >= 0; i--) {
error(operand.expr, "Expected a 3-valued expression on the rhs, got (%s)", s); if (rs->vals[i] != nullptr && count < i+2) {
gb_string_free(s); gbString s = type_to_string(t);
break; error(operand.expr, "Expected a %td-valued expression on the rhs, got (%s)", i+2, s);
} gb_string_free(s);
do_break = true;
if (rs->vals.count > 0 && rs->vals[0] != nullptr && count < 2) { break;
gbString s = type_to_string(t); }
error(operand.expr, "Expected at least a 2-valued expression on the rhs, got (%s)", s);
gb_string_free(s);
break;
} }
if (is_reverse) { if (is_reverse) {
+21 -10
View File
@@ -1898,8 +1898,7 @@ gb_internal void add_type_info_type_internal(CheckerContext *c, Type *t) {
add_type_info_dependency(c->info, c->decl, t); add_type_info_dependency(c->info, c->decl, t);
MUTEX_GUARD_BLOCK(&c->info->type_info_mutex) { MUTEX_GUARD_BLOCK(&c->info->type_info_mutex) {
MapFindResult fr; auto found = map_get(&c->info->type_info_map, t);
auto found = map_try_get(&c->info->type_info_map, t, &fr);
if (found != nullptr) { if (found != nullptr) {
// Types have already been added // Types have already been added
return; return;
@@ -1923,7 +1922,7 @@ gb_internal void add_type_info_type_internal(CheckerContext *c, Type *t) {
ti_index = c->info->type_info_types.count; ti_index = c->info->type_info_types.count;
array_add(&c->info->type_info_types, t); array_add(&c->info->type_info_types, t);
} }
map_set_internal_from_try_get(&c->checker->info.type_info_map, t, ti_index, fr); map_set(&c->checker->info.type_info_map, t, ti_index);
if (prev) { if (prev) {
// NOTE(bill): If a previous one exists already, no need to continue // NOTE(bill): If a previous one exists already, no need to continue
@@ -2194,7 +2193,7 @@ gb_internal void add_min_dep_type_info(Checker *c, Type *t) {
// IMPORTANT NOTE(bill): this must be copied as `map_set` takes a const ref // IMPORTANT NOTE(bill): this must be copied as `map_set` takes a const ref
// and effectively assigns the `+1` of the value // and effectively assigns the `+1` of the value
isize const count = set->count; isize const count = set->count;
if (map_set_if_not_previously_exists(set, ti_index, count)) { if (map_set_if_not_previously_exists(set, ti_index+1, count)) {
// Type already exists; // Type already exists;
return; return;
} }
@@ -2537,6 +2536,11 @@ gb_internal void generate_minimum_dependency_set_internal(Checker *c, Entity *st
is_init = false; is_init = false;
} }
if ((e->flags & EntityFlag_Disabled) != 0) {
warning(e->token, "This @(init) procedure is disabled; you must call it manually");
is_init = false;
}
if (is_init) { if (is_init) {
add_dependency_to_set(c, e); add_dependency_to_set(c, e);
array_add(&c->info.init_procedures, e); array_add(&c->info.init_procedures, e);
@@ -2927,6 +2931,8 @@ gb_internal void init_core_type_info(Checker *c) {
return; return;
} }
Entity *type_info_entity = find_core_entity(c, str_lit("Type_Info")); Entity *type_info_entity = find_core_entity(c, str_lit("Type_Info"));
GB_ASSERT(type_info_entity != nullptr);
GB_ASSERT(type_info_entity->type != nullptr);
t_type_info = type_info_entity->type; t_type_info = type_info_entity->type;
t_type_info_ptr = alloc_type_pointer(t_type_info); t_type_info_ptr = alloc_type_pointer(t_type_info);
@@ -4310,17 +4316,22 @@ gb_internal bool correct_single_type_alias(CheckerContext *c, Entity *e) {
gb_internal bool correct_type_alias_in_scope_backwards(CheckerContext *c, Scope *s) { gb_internal bool correct_type_alias_in_scope_backwards(CheckerContext *c, Scope *s) {
bool correction = false; bool correction = false;
u32 n = s->elements.count; for (u32 n = s->elements.count, i = n-1; i < n; i--) {
for (u32 i = n-1; i < n; i--) { auto const &entry = s->elements.entries[i];
correction |= correct_single_type_alias(c, s->elements.entries[i].value); Entity *e = entry.value;
if (entry.hash && e != nullptr) {
correction |= correct_single_type_alias(c, e);
}
} }
return correction; return correction;
} }
gb_internal bool correct_type_alias_in_scope_forwards(CheckerContext *c, Scope *s) { gb_internal bool correct_type_alias_in_scope_forwards(CheckerContext *c, Scope *s) {
bool correction = false; bool correction = false;
u32 n = s->elements.count; for (auto const &entry : s->elements) {
for (isize i = 0; i < n; i++) { Entity *e = entry.value;
correction |= correct_single_type_alias(c, s->elements.entries[i].value); if (e != nullptr) {
correction |= correct_single_type_alias(c, entry.value);
}
} }
return correction; return correction;
} }
+5 -6
View File
@@ -987,9 +987,8 @@ gb_internal void odin_doc_update_entities(OdinDocWriter *w) {
auto entities = array_make<Entity *>(heap_allocator(), 0, w->entity_cache.count); auto entities = array_make<Entity *>(heap_allocator(), 0, w->entity_cache.count);
defer (array_free(&entities)); defer (array_free(&entities));
for (u32 i = 0; i < w->entity_cache.count; i++) { for (auto const &entry : w->entity_cache) {
Entity *e = w->entity_cache.entries[i].key; array_add(&entities, entry.key);
array_add(&entities, e);
} }
for (Entity *e : entities) { for (Entity *e : entities) {
GB_ASSERT(e != nullptr); GB_ASSERT(e != nullptr);
@@ -998,9 +997,9 @@ gb_internal void odin_doc_update_entities(OdinDocWriter *w) {
} }
} }
for (u32 i = 0; i < w->entity_cache.count; i++) { for (auto const &entry : w->entity_cache) {
Entity *e = w->entity_cache.entries[i].key; Entity *e = entry.key;
OdinDocEntityIndex entity_index = w->entity_cache.entries[i].value; OdinDocEntityIndex entity_index = entry.value;
OdinDocTypeIndex type_index = odin_doc_type(w, e->type); OdinDocTypeIndex type_index = odin_doc_type(w, e->type);
OdinDocEntityIndex foreign_library = 0; OdinDocEntityIndex foreign_library = 0;
+3 -1
View File
@@ -376,11 +376,11 @@ gb_internal void error_out_coloured(char const *str, TerminalStyle style, Termin
gb_internal void error_va(TokenPos const &pos, TokenPos end, char const *fmt, va_list va) { gb_internal void error_va(TokenPos const &pos, TokenPos end, char const *fmt, va_list va) {
global_error_collector.count.fetch_add(1); global_error_collector.count.fetch_add(1);
mutex_lock(&global_error_collector.mutex);
if (global_error_collector.count > MAX_ERROR_COLLECTOR_COUNT()) { if (global_error_collector.count > MAX_ERROR_COLLECTOR_COUNT()) {
print_all_errors(); print_all_errors();
gb_exit(1); gb_exit(1);
} }
mutex_lock(&global_error_collector.mutex);
push_error_value(pos, ErrorValue_Error); push_error_value(pos, ErrorValue_Error);
// NOTE(bill): Duplicate error, skip it // NOTE(bill): Duplicate error, skip it
@@ -403,6 +403,8 @@ gb_internal void error_va(TokenPos const &pos, TokenPos end, char const *fmt, va
error_out("\n"); error_out("\n");
show_error_on_line(pos, end); show_error_on_line(pos, end);
} else { } else {
global_error_collector.curr_error_value = {};
global_error_collector.curr_error_value_set.store(false);
global_error_collector.count.fetch_sub(1); global_error_collector.count.fetch_sub(1);
} }
try_pop_error_value(); try_pop_error_value();
+25 -12
View File
@@ -55,36 +55,49 @@ gb_internal uintptr hash_exact_value(ExactValue v) {
mutex_lock(&hash_exact_value_mutex); mutex_lock(&hash_exact_value_mutex);
defer (mutex_unlock(&hash_exact_value_mutex)); defer (mutex_unlock(&hash_exact_value_mutex));
uintptr res = 0;
switch (v.kind) { switch (v.kind) {
case ExactValue_Invalid: case ExactValue_Invalid:
return 0; return 0;
case ExactValue_Bool: case ExactValue_Bool:
return gb_fnv32a(&v.value_bool, gb_size_of(v.value_bool)); res = gb_fnv32a(&v.value_bool, gb_size_of(v.value_bool));
break;
case ExactValue_String: case ExactValue_String:
return gb_fnv32a(v.value_string.text, v.value_string.len); res = gb_fnv32a(v.value_string.text, v.value_string.len);
break;
case ExactValue_Integer: case ExactValue_Integer:
{ {
u32 key = gb_fnv32a(v.value_integer.dp, gb_size_of(*v.value_integer.dp) * v.value_integer.used); u32 key = gb_fnv32a(v.value_integer.dp, gb_size_of(*v.value_integer.dp) * v.value_integer.used);
u8 last = (u8)v.value_integer.sign; u8 last = (u8)v.value_integer.sign;
return (key ^ last) * 0x01000193; res = (key ^ last) * 0x01000193;
break;
} }
case ExactValue_Float: case ExactValue_Float:
return gb_fnv32a(&v.value_float, gb_size_of(v.value_float)); res = gb_fnv32a(&v.value_float, gb_size_of(v.value_float));
break;
case ExactValue_Pointer: case ExactValue_Pointer:
return ptr_map_hash_key(v.value_pointer); res = ptr_map_hash_key(v.value_pointer);
break;
case ExactValue_Complex: case ExactValue_Complex:
return gb_fnv32a(v.value_complex, gb_size_of(Complex128)); res = gb_fnv32a(v.value_complex, gb_size_of(Complex128));
break;
case ExactValue_Quaternion: case ExactValue_Quaternion:
return gb_fnv32a(v.value_quaternion, gb_size_of(Quaternion256)); res = gb_fnv32a(v.value_quaternion, gb_size_of(Quaternion256));
break;
case ExactValue_Compound: case ExactValue_Compound:
return ptr_map_hash_key(v.value_compound); res = ptr_map_hash_key(v.value_compound);
break;
case ExactValue_Procedure: case ExactValue_Procedure:
return ptr_map_hash_key(v.value_procedure); res = ptr_map_hash_key(v.value_procedure);
break;
case ExactValue_Typeid: case ExactValue_Typeid:
return ptr_map_hash_key(v.value_typeid); res = ptr_map_hash_key(v.value_typeid);
break;
default:
res = gb_fnv32a(&v, gb_size_of(ExactValue));
} }
return gb_fnv32a(&v, gb_size_of(ExactValue)); return res & 0x7fffffff;
} }
+1 -1
View File
@@ -167,7 +167,7 @@ gb_internal i32 linker_stage(LinkerData *gen) {
if (has_asm_extension(lib)) { if (has_asm_extension(lib)) {
if (!string_set_update(&asm_files, lib)) { if (!string_set_update(&asm_files, lib)) {
String asm_file = asm_files.entries[i].value; String asm_file = lib;
String obj_file = concatenate_strings(permanent_allocator(), asm_file, str_lit(".obj")); String obj_file = concatenate_strings(permanent_allocator(), asm_file, str_lit(".obj"));
String obj_format = str_lit("win64"); String obj_format = str_lit("win64");
#if defined(GB_ARCH_32_BIT) #if defined(GB_ARCH_32_BIT)
+1 -1
View File
@@ -140,7 +140,7 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) {
} }
gen->default_module.gen = gen; gen->default_module.gen = gen;
map_set(&gen->modules, cast(void *)nullptr, &gen->default_module); map_set(&gen->modules, cast(void *)1, &gen->default_module);
lb_init_module(&gen->default_module, c); lb_init_module(&gen->default_module, c);
+44 -9
View File
@@ -802,8 +802,19 @@ gb_internal void lb_build_range_enum(lbProcedure *p, Type *enum_type, Type *val_
if (done_) *done_ = done; if (done_) *done_ = done;
} }
gb_internal void lb_build_range_tuple(lbProcedure *p, Ast *expr, Type *val0_type, Type *val1_type, gb_internal void lb_build_range_tuple(lbProcedure *p, AstRangeStmt *rs, Scope *scope) {
lbValue *val0_, lbValue *val1_, lbBlock **loop_, lbBlock **done_) { Ast *expr = unparen_expr(rs->expr);
Type *expr_type = type_of_expr(expr);
Type *et = base_type(type_deref(expr_type));
GB_ASSERT(et->kind == Type_Tuple);
i32 value_count = cast(i32)et->Tuple.variables.count;
lbValue *values = gb_alloc_array(permanent_allocator(), lbValue, value_count);
lb_open_scope(p, scope);
lbBlock *loop = lb_create_block(p, "for.tuple.loop"); lbBlock *loop = lb_create_block(p, "for.tuple.loop");
lb_emit_jump(p, loop); lb_emit_jump(p, loop);
lb_start_block(p, loop); lb_start_block(p, loop);
@@ -821,11 +832,26 @@ gb_internal void lb_build_range_tuple(lbProcedure *p, Ast *expr, Type *val0_type
lb_emit_if(p, cond, body, done); lb_emit_if(p, cond, body, done);
lb_start_block(p, body); lb_start_block(p, body);
for (i32 i = 0; i < value_count; i++) {
values[i] = lb_emit_tuple_ev(p, tuple_value, i);
}
if (val0_) *val0_ = lb_emit_tuple_ev(p, tuple_value, 0); GB_ASSERT(rs->vals.count <= value_count);
if (val1_) *val1_ = lb_emit_tuple_ev(p, tuple_value, 1); for (isize i = 0; i < rs->vals.count; i++) {
if (loop_) *loop_ = loop; Ast *val = rs->vals[i];
if (done_) *done_ = done; if (val != nullptr) {
lb_store_range_stmt_val(p, val, values[i]);
}
}
lb_push_target_list(p, rs->label, done, loop, nullptr);
lb_build_stmt(p, rs->body);
lb_close_scope(p, lbDeferExit_Default, nullptr);
lb_pop_target_list(p);
lb_emit_jump(p, loop);
lb_start_block(p, done);
} }
gb_internal void lb_build_range_stmt_struct_soa(lbProcedure *p, AstRangeStmt *rs, Scope *scope) { gb_internal void lb_build_range_stmt_struct_soa(lbProcedure *p, AstRangeStmt *rs, Scope *scope) {
@@ -968,6 +994,17 @@ gb_internal void lb_build_range_stmt(lbProcedure *p, AstRangeStmt *rs, Scope *sc
} }
} }
TypeAndValue tav = type_and_value_of_expr(expr);
if (tav.mode != Addressing_Type) {
Type *expr_type = type_of_expr(expr);
Type *et = base_type(type_deref(expr_type));
if (et->kind == Type_Tuple) {
lb_build_range_tuple(p, rs, scope);
return;
}
}
lb_open_scope(p, scope); lb_open_scope(p, scope);
Ast *val0 = rs->vals.count > 0 ? lb_strip_and_prefix(rs->vals[0]) : nullptr; Ast *val0 = rs->vals.count > 0 ? lb_strip_and_prefix(rs->vals[0]) : nullptr;
@@ -986,7 +1023,6 @@ gb_internal void lb_build_range_stmt(lbProcedure *p, AstRangeStmt *rs, Scope *sc
lbBlock *loop = nullptr; lbBlock *loop = nullptr;
lbBlock *done = nullptr; lbBlock *done = nullptr;
bool is_map = false; bool is_map = false;
TypeAndValue tav = type_and_value_of_expr(expr);
if (tav.mode == Addressing_Type) { if (tav.mode == Addressing_Type) {
lb_build_range_enum(p, type_deref(tav.type), val0_type, &val, &key, &loop, &done); lb_build_range_enum(p, type_deref(tav.type), val0_type, &val, &key, &loop, &done);
@@ -1062,8 +1098,7 @@ gb_internal void lb_build_range_stmt(lbProcedure *p, AstRangeStmt *rs, Scope *sc
break; break;
} }
case Type_Tuple: case Type_Tuple:
lb_build_range_tuple(p, expr, val0_type, val1_type, &val, &key, &loop, &done); GB_PANIC("Should be handled already");
break;
case Type_BitSet: { case Type_BitSet: {
lbModule *m = p->module; lbModule *m = p->module;
+1 -1
View File
@@ -2,7 +2,7 @@ gb_internal isize lb_type_info_index(CheckerInfo *info, Type *type, bool err_on_
auto *set = &info->minimum_dependency_type_info_set; auto *set = &info->minimum_dependency_type_info_set;
isize index = type_info_index(info, type, err_on_not_found); isize index = type_info_index(info, type, err_on_not_found);
if (index >= 0) { if (index >= 0) {
auto *found = map_get(set, index); auto *found = map_get(set, index+1);
if (found) { if (found) {
GB_ASSERT(*found >= 0); GB_ASSERT(*found >= 0);
return *found + 1; return *found + 1;
+213 -242
View File
@@ -16,23 +16,21 @@ struct MapFindResult {
}; };
enum : MapIndex { MAP_SENTINEL = ~(MapIndex)0 }; enum : MapIndex { MAP_SENTINEL = ~(MapIndex)0 };
static void *const MAP_TOMBSTONE = (void *)~(uintptr)0;
template <typename K, typename V> template <typename K, typename V>
struct PtrMapEntry { struct PtrMapEntry {
static_assert(sizeof(K) == sizeof(void *), "Key size must be pointer size"); static_assert(sizeof(K) == sizeof(void *), "Key size must be pointer size");
K key; K key;
V value; V value;
MapIndex next;
}; };
template <typename K, typename V> template <typename K, typename V>
struct PtrMap { struct PtrMap {
MapIndex * hashes;
usize hashes_count;
PtrMapEntry<K, V> *entries; PtrMapEntry<K, V> *entries;
u32 count; u32 count;
u32 entries_capacity; u32 capacity;
}; };
@@ -69,7 +67,6 @@ template <typename K, typename V> gb_internal void map_grow (PtrMap<
template <typename K, typename V> gb_internal void map_rehash (PtrMap<K, V> *h, isize new_count); template <typename K, typename V> gb_internal void map_rehash (PtrMap<K, V> *h, isize new_count);
template <typename K, typename V> gb_internal void map_reserve (PtrMap<K, V> *h, isize cap); template <typename K, typename V> gb_internal void map_reserve (PtrMap<K, V> *h, isize cap);
#if PTR_MAP_ENABLE_MULTI_MAP
// Mutlivalued map procedure // Mutlivalued map procedure
template <typename K, typename V> gb_internal PtrMapEntry<K, V> * multi_map_find_first(PtrMap<K, V> *h, K key); template <typename K, typename V> gb_internal PtrMapEntry<K, V> * multi_map_find_first(PtrMap<K, V> *h, K key);
template <typename K, typename V> gb_internal PtrMapEntry<K, V> * multi_map_find_next (PtrMap<K, V> *h, PtrMapEntry<K, V> *e); template <typename K, typename V> gb_internal PtrMapEntry<K, V> * multi_map_find_next (PtrMap<K, V> *h, PtrMapEntry<K, V> *e);
@@ -79,7 +76,6 @@ template <typename K, typename V> gb_internal void multi_map_get_all (PtrMap<
template <typename K, typename V> gb_internal void multi_map_insert (PtrMap<K, V> *h, K key, V const &value); template <typename K, typename V> gb_internal void multi_map_insert (PtrMap<K, V> *h, K key, V const &value);
template <typename K, typename V> gb_internal void multi_map_remove (PtrMap<K, V> *h, K key, PtrMapEntry<K, V> *e); template <typename K, typename V> gb_internal void multi_map_remove (PtrMap<K, V> *h, K key, PtrMapEntry<K, V> *e);
template <typename K, typename V> gb_internal void multi_map_remove_all(PtrMap<K, V> *h, K key); template <typename K, typename V> gb_internal void multi_map_remove_all(PtrMap<K, V> *h, K key);
#endif
gb_internal gbAllocator map_allocator(void) { gb_internal gbAllocator map_allocator(void) {
return heap_allocator(); return heap_allocator();
@@ -94,170 +90,141 @@ gb_internal gb_inline void map_init(PtrMap<K, V> *h, isize capacity) {
template <typename K, typename V> template <typename K, typename V>
gb_internal gb_inline void map_destroy(PtrMap<K, V> *h) { gb_internal gb_inline void map_destroy(PtrMap<K, V> *h) {
gbAllocator a = map_allocator(); gbAllocator a = map_allocator();
gb_free(a, h->hashes);
gb_free(a, h->entries); gb_free(a, h->entries);
} }
template <typename K, typename V>
gb_internal void map__resize_hashes(PtrMap<K, V> *h, usize count) {
h->hashes_count = cast(u32)resize_array_raw(&h->hashes, map_allocator(), h->hashes_count, count, MAP_CACHE_LINE_SIZE);
}
template <typename K, typename V> template <typename K, typename V>
gb_internal void map__reserve_entries(PtrMap<K, V> *h, usize capacity) { gb_internal void map__insert(PtrMap<K, V> *h, K key, V const &value) {
h->entries_capacity = cast(u32)resize_array_raw(&h->entries, map_allocator(), h->entries_capacity, capacity, MAP_CACHE_LINE_SIZE); if (h->count+1 >= h->capacity) {
} map_grow(h);
template <typename K, typename V>
gb_internal MapIndex map__add_entry(PtrMap<K, V> *h, K key) {
PtrMapEntry<K, V> e = {};
e.key = key;
e.next = MAP_SENTINEL;
if (h->count+1 >= h->entries_capacity) {
map__reserve_entries(h, gb_max(h->entries_capacity*2, 4));
}
h->entries[h->count++] = e;
return cast(MapIndex)(h->count-1);
}
template <typename K, typename V>
gb_internal MapFindResult map__find(PtrMap<K, V> *h, K key) {
MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
if (h->hashes_count == 0) {
return fr;
} }
u32 hash = ptr_map_hash_key(key); u32 hash = ptr_map_hash_key(key);
fr.hash_index = cast(MapIndex)(hash & (h->hashes_count-1)); u32 mask = h->capacity-1;
fr.entry_index = h->hashes[fr.hash_index]; MapIndex index = hash & mask;
while (fr.entry_index != MAP_SENTINEL) { MapIndex original_index = index;
auto *entry = &h->entries[fr.entry_index]; do {
if (entry->key == key) { auto *entry = h->entries+index;
return fr; if (!entry->key || entry->key == cast(K)MAP_TOMBSTONE) {
entry->key = key;
entry->value = value;
h->count += 1;
return;
} }
fr.entry_prev = fr.entry_index; index = (index+1)&mask;
fr.entry_index = entry->next; } while (index != original_index);
}
return fr;
}
template <typename K, typename V> GB_PANIC("FAILED TO INSERT");
gb_internal MapFindResult map__find_from_entry(PtrMap<K, V> *h, PtrMapEntry<K, V> *e) {
MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
if (h->hashes_count == 0) {
return fr;
}
u32 hash = ptr_map_hash_key(e->key);
fr.hash_index = cast(MapIndex)(hash & (h->hashes_count-1));
fr.entry_index = h->hashes[fr.hash_index];
while (fr.entry_index != MAP_SENTINEL) {
if (&h->entries[fr.entry_index] == e) {
return fr;
}
fr.entry_prev = fr.entry_index;
fr.entry_index = h->entries[fr.entry_index].next;
}
return fr;
} }
template <typename K, typename V> template <typename K, typename V>
gb_internal b32 map__full(PtrMap<K, V> *h) { gb_internal b32 map__full(PtrMap<K, V> *h) {
return 0.75f * h->hashes_count <= h->count; return 0.75f * h->capacity <= h->count;
} }
template <typename K, typename V> template <typename K, typename V>
gb_internal gb_inline void map_grow(PtrMap<K, V> *h) { gb_internal gb_inline void map_grow(PtrMap<K, V> *h) {
isize new_count = gb_max(h->hashes_count<<1, 16); isize new_capacity = gb_max(h->capacity<<1, 16);
map_rehash(h, new_count); map_reserve(h, new_capacity);
} }
template <typename K, typename V> template <typename K, typename V>
gb_internal void map_reset_entries(PtrMap<K, V> *h) { gb_internal void try_map_grow(PtrMap<K, V> *h) {
for (usize i = 0; i < h->hashes_count; i++) { if (h->capacity == 0 || map__full(h)) {
h->hashes[i] = MAP_SENTINEL; map_grow(h);
}
for (usize i = 0; i < h->count; i++) {
MapFindResult fr;
PtrMapEntry<K, V> *e = &h->entries[i];
e->next = MAP_SENTINEL;
fr = map__find_from_entry(h, e);
if (fr.entry_prev == MAP_SENTINEL) {
h->hashes[fr.hash_index] = cast(MapIndex)i;
} else {
h->entries[fr.entry_prev].next = cast(MapIndex)i;
}
} }
} }
template <typename K, typename V> template <typename K, typename V>
gb_internal void map_reserve(PtrMap<K, V> *h, isize cap) { gb_internal void map_reserve(PtrMap<K, V> *h, isize cap) {
if (h->count*2 < h->hashes_count) { if (cap < h->capacity) {
return; return;
} }
map__reserve_entries(h, cap); cap = next_pow2_isize(cap);
map__resize_hashes(h, cap*2); typedef PtrMapEntry<K, V> EntryType;
map_reset_entries(h);
}
PtrMap<K, V> new_h = {};
new_h.count = 0;
new_h.capacity = cast(u32)cap;
new_h.entries = gb_alloc_array(map_allocator(), EntryType, new_h.capacity);
template <typename K, typename V> if (h->count) {
gb_internal void map_rehash(PtrMap<K, V> *h, isize new_count) { for (u32 i = 0; i < h->capacity; i++) {
map_reserve(h, new_count); auto *entry = h->entries+i;
if (entry->key &&
entry->key != cast(K)MAP_TOMBSTONE) {
map__insert(&new_h, entry->key, entry->value);
}
}
}
map_destroy(h);
*h = new_h;
} }
template <typename K, typename V> template <typename K, typename V>
gb_internal V *map_get(PtrMap<K, V> *h, K key) { gb_internal V *map_get(PtrMap<K, V> *h, K key) {
MapIndex hash_index = MAP_SENTINEL; if (h->count == 0) {
MapIndex entry_prev = MAP_SENTINEL; return nullptr;
MapIndex entry_index = MAP_SENTINEL;
if (h->hashes_count != 0) {
u32 hash = ptr_map_hash_key(key);
hash_index = cast(MapIndex)(hash & (h->hashes_count-1));
entry_index = h->hashes[hash_index];
while (entry_index != MAP_SENTINEL) {
auto *entry = &h->entries[entry_index];
if (entry->key == key) {
return &entry->value;
}
entry_prev = entry_index;
entry_index = entry->next;
}
} }
if (key == 0) {
GB_PANIC("0 key");
}
u32 hash = ptr_map_hash_key(key);
u32 mask = (h->capacity-1);
u32 index = hash & mask;
u32 original_index = index;
do {
auto *entry = h->entries+index;
if (!entry->key) {
// NOTE(bill): no found, but there isn't any key removal for this hash map
return nullptr;
} else if (entry->key == key) {
return &entry->value;
}
index = (index+1) & mask;
} while (original_index != index);
return nullptr; return nullptr;
} }
template <typename K, typename V> template <typename K, typename V>
gb_internal V *map_try_get(PtrMap<K, V> *h, K key, MapFindResult *fr_) { gb_internal V *map_try_get(PtrMap<K, V> *h, K key, MapIndex *found_index_) {
MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; if (found_index_) *found_index_ = ~(MapIndex)0;
if (h->hashes_count != 0) {
u32 hash = ptr_map_hash_key(key); if (h->count == 0) {
fr.hash_index = cast(MapIndex)(hash & (h->hashes_count-1)); return nullptr;
fr.entry_index = h->hashes[fr.hash_index]; }
while (fr.entry_index != MAP_SENTINEL) { if (key == 0) {
auto *entry = &h->entries[fr.entry_index]; GB_PANIC("0 key");
if (entry->key == key) { }
return &entry->value;
} u32 hash = ptr_map_hash_key(key);
fr.entry_prev = fr.entry_index; u32 mask = (h->capacity-1);
fr.entry_index = entry->next; u32 index = hash & mask;
u32 original_index = index;
do {
auto *entry = h->entries+index;
if (!entry->key) {
// NOTE(bill): no found, but there isn't any key removal for this hash map
return nullptr;
} else if (entry->key == key) {
if (found_index_) *found_index_ = index;
return &entry->value;
} }
} index = (index+1) & mask;
if (h->hashes_count == 0 || map__full(h)) { } while (original_index != index);
map_grow(h);
}
if (fr_) *fr_ = fr;
return nullptr; return nullptr;
} }
template <typename K, typename V> template <typename K, typename V>
gb_internal void map_set_internal_from_try_get(PtrMap<K, V> *h, K key, V const &value, MapFindResult const &fr) { gb_internal void map_set_internal_from_try_get(PtrMap<K, V> *h, K key, V const &value, MapIndex found_index) {
MapIndex index = map__add_entry(h, key); if (found_index != MAP_SENTINEL) {
if (fr.entry_prev != MAP_SENTINEL) { GB_ASSERT(h->entries[found_index].key == key);
h->entries[fr.entry_prev].next = index; h->entries[found_index].value = value;
} else { } else {
h->hashes[fr.hash_index] = index; map_set(h, key, value);
} }
h->entries[index].value = value;
} }
template <typename K, typename V> template <typename K, typename V>
@@ -269,116 +236,83 @@ gb_internal V &map_must_get(PtrMap<K, V> *h, K key) {
template <typename K, typename V> template <typename K, typename V>
gb_internal void map_set(PtrMap<K, V> *h, K key, V const &value) { gb_internal void map_set(PtrMap<K, V> *h, K key, V const &value) {
MapIndex index; GB_ASSERT(key != 0);
MapFindResult fr; try_map_grow(h);
if (h->hashes_count == 0) { auto *found = map_get(h, key);
map_grow(h); if (found) {
} *found = value;
fr = map__find(h, key); return;
if (fr.entry_index != MAP_SENTINEL) {
index = fr.entry_index;
} else {
index = map__add_entry(h, key);
if (fr.entry_prev != MAP_SENTINEL) {
h->entries[fr.entry_prev].next = index;
} else {
h->hashes[fr.hash_index] = index;
}
}
h->entries[index].value = value;
if (map__full(h)) {
map_grow(h);
} }
map__insert(h, key, value);
} }
// returns true if it previously existed // returns true if it previously existed
template <typename K, typename V> template <typename K, typename V>
gb_internal bool map_set_if_not_previously_exists(PtrMap<K, V> *h, K key, V const &value) { gb_internal bool map_set_if_not_previously_exists(PtrMap<K, V> *h, K key, V const &value) {
MapIndex index; try_map_grow(h);
MapFindResult fr; auto *found = map_get(h, key);
if (h->hashes_count == 0) { if (found) {
map_grow(h);
}
fr = map__find(h, key);
if (fr.entry_index != MAP_SENTINEL) {
return true; return true;
} else {
index = map__add_entry(h, key);
if (fr.entry_prev != MAP_SENTINEL) {
h->entries[fr.entry_prev].next = index;
} else {
h->hashes[fr.hash_index] = index;
}
}
h->entries[index].value = value;
if (map__full(h)) {
map_grow(h);
} }
map__insert(h, key, value);
return false; return false;
} }
template <typename K, typename V>
gb_internal void map__erase(PtrMap<K, V> *h, MapFindResult const &fr) {
MapFindResult last;
if (fr.entry_prev == MAP_SENTINEL) {
h->hashes[fr.hash_index] = h->entries[fr.entry_index].next;
} else {
h->entries[fr.entry_prev].next = h->entries[fr.entry_index].next;
}
if (fr.entry_index == h->count-1) {
h->count--;
return;
}
h->entries[fr.entry_index] = h->entries[h->count-1];
h->count--;
last = map__find(h, h->entries[fr.entry_index].key);
if (last.entry_prev != MAP_SENTINEL) {
h->entries[last.entry_prev].next = fr.entry_index;
} else {
h->hashes[last.hash_index] = fr.entry_index;
}
}
template <typename K, typename V> template <typename K, typename V>
gb_internal void map_remove(PtrMap<K, V> *h, K key) { gb_internal void map_remove(PtrMap<K, V> *h, K key) {
MapFindResult fr = map__find(h, key); MapIndex found_index = 0;
if (fr.entry_index != MAP_SENTINEL) { if (map_try_get(h, key, &found_index)) {
map__erase(h, fr); h->entries[found_index].key = cast(K)MAP_TOMBSTONE;
h->count -= 1;
} }
} }
template <typename K, typename V> template <typename K, typename V>
gb_internal gb_inline void map_clear(PtrMap<K, V> *h) { gb_internal gb_inline void map_clear(PtrMap<K, V> *h) {
h->count = 0; h->count = 0;
for (usize i = 0; i < h->hashes_count; i++) { gb_zero_array(h->entries, h->capacity);
h->hashes[i] = MAP_SENTINEL;
}
} }
#if PTR_MAP_ENABLE_MULTI_MAP #if PTR_MAP_ENABLE_MULTI_MAP
template <typename K, typename V> template <typename K, typename V>
gb_internal PtrMapEntry<K, V> *multi_map_find_first(PtrMap<K, V> *h, K key) { gb_internal PtrMapEntry<K, V> *multi_map_find_first(PtrMap<K, V> *h, K key) {
MapIndex i = map__find(h, key).entry_index; if (h->count == 0) {
if (i == MAP_SENTINEL) {
return nullptr; return nullptr;
} }
return &h->entries[i]; u32 hash = ptr_map_hash_key(key);
u32 mask = (h->capacity-1);
u32 index = hash & mask;
u32 original_index = index;
do {
auto *entry = h->entries+index;
if (!entry->key) {
// NOTE(bill): no found, but there isn't any key removal for this hash map
return nullptr;
} else if (entry->key == key) {
return entry;
}
index = (index+1) & mask;
} while (original_index != index);
return nullptr;
} }
template <typename K, typename V> template <typename K, typename V>
gb_internal PtrMapEntry<K, V> *multi_map_find_next(PtrMap<K, V> *h, PtrMapEntry<K, V> *e) { gb_internal PtrMapEntry<K, V> *multi_map_find_next(PtrMap<K, V> *h, PtrMapEntry<K, V> *e) {
MapIndex i = e->next; u32 mask = h->capacity-1;
while (i != MAP_SENTINEL) { MapIndex index = cast(MapIndex)(e - h->entries);
if (h->entries[i].key == e->key) { MapIndex original_index = index;
return &h->entries[i]; do {
index = (index+1)&mask;
auto *entry = h->entries+index;
if (!entry->key) {
return nullptr;
} }
i = h->entries[i].next; if (entry->key == e->key) {
} return entry;
}
} while (original_index != index);
return nullptr; return nullptr;
} }
@@ -405,34 +339,16 @@ gb_internal void multi_map_get_all(PtrMap<K, V> *h, K key, V *items) {
template <typename K, typename V> template <typename K, typename V>
gb_internal void multi_map_insert(PtrMap<K, V> *h, K key, V const &value) { gb_internal void multi_map_insert(PtrMap<K, V> *h, K key, V const &value) {
MapFindResult fr; try_map_grow(h);
MapIndex i; map__insert(h, key, value);
if (h->hashes_count == 0) {
map_grow(h);
}
// Make
fr = map__find(h, key);
i = map__add_entry(h, key);
if (fr.entry_prev == MAP_SENTINEL) {
h->hashes[fr.hash_index] = i;
} else {
h->entries[fr.entry_prev].next = i;
}
h->entries[i].next = fr.entry_index;
h->entries[i].value = value;
// Grow if needed
if (map__full(h)) {
map_grow(h);
}
} }
template <typename K, typename V> // template <typename K, typename V>
gb_internal void multi_map_remove(PtrMap<K, V> *h, K key, PtrMapEntry<K, V> *e) { // gb_internal void multi_map_remove(PtrMap<K, V> *h, K key, PtrMapEntry<K, V> *e) {
MapFindResult fr = map__find_from_entry(h, e); // if (fr.entry_index != MAP_SENTINEL) {
if (fr.entry_index != MAP_SENTINEL) { // map__erase(h, fr);
map__erase(h, fr); // }
} // }
}
template <typename K, typename V> template <typename K, typename V>
gb_internal void multi_map_remove_all(PtrMap<K, V> *h, K key) { gb_internal void multi_map_remove_all(PtrMap<K, V> *h, K key) {
@@ -443,22 +359,77 @@ gb_internal void multi_map_remove_all(PtrMap<K, V> *h, K key) {
#endif #endif
template <typename K, typename V>
gb_internal PtrMapEntry<K, V> *begin(PtrMap<K, V> &m) {
return m.entries;
}
template <typename K, typename V>
gb_internal PtrMapEntry<K, V> const *begin(PtrMap<K, V> const &m) {
return m.entries;
}
template <typename K, typename V> template <typename K, typename V>
gb_internal PtrMapEntry<K, V> *end(PtrMap<K, V> &m) { struct PtrMapIterator {
return m.entries + m.count; PtrMap<K, V> *map;
MapIndex index;
PtrMapIterator<K, V> &operator++() noexcept {
for (;;) {
++index;
if (map->capacity == index) {
return *this;
}
PtrMapEntry<K, V> *entry = map->entries+index;
if (entry->key && entry->key != cast(K)MAP_TOMBSTONE) {
return *this;
}
}
}
bool operator==(PtrMapIterator<K, V> const &other) const noexcept {
return this->map == other->map && this->index == other->index;
}
operator PtrMapEntry<K, V> *() const {
return map->entries+index;
}
};
template <typename K, typename V>
gb_internal PtrMapIterator<K, V> end(PtrMap<K, V> &m) noexcept {
return PtrMapIterator<K, V>{&m, m.capacity};
} }
template <typename K, typename V> template <typename K, typename V>
gb_internal PtrMapEntry<K, V> const *end(PtrMap<K, V> const &m) { gb_internal PtrMapIterator<K, V> const end(PtrMap<K, V> const &m) noexcept {
return m.entries + m.count; return PtrMapIterator<K, V>{&m, m.capacity};
}
template <typename K, typename V>
gb_internal PtrMapIterator<K, V> begin(PtrMap<K, V> &m) noexcept {
if (m.count == 0) {
return end(m);
}
MapIndex index = 0;
while (index < m.capacity) {
auto key = m.entries[index].key;
if (key && key != cast(K)MAP_TOMBSTONE) {
break;
}
index++;
}
return PtrMapIterator<K, V>{&m, index};
}
template <typename K, typename V>
gb_internal PtrMapIterator<K, V> const begin(PtrMap<K, V> const &m) noexcept {
if (m.count == 0) {
return end(m);
}
MapIndex index = 0;
while (index < m.capacity) {
auto key = m.entries[index].key;
if (key && key != cast(K)MAP_TOMBSTONE) {
break;
}
index++;
}
return PtrMapIterator<K, V>{&m, index};
} }
+136 -168
View File
@@ -2,8 +2,8 @@ GB_STATIC_ASSERT(sizeof(MapIndex) == sizeof(u32));
struct StringHashKey { struct StringHashKey {
u32 hash;
String string; String string;
u32 hash;
operator String() const noexcept { operator String() const noexcept {
return this->string; return this->string;
@@ -13,7 +13,8 @@ struct StringHashKey {
} }
}; };
gb_internal gb_inline u32 string_hash(String const &s) { gb_internal gb_inline u32 string_hash(String const &s) {
return fnv32a(s.text, s.len) & 0x7fffffff; u32 res = fnv32a(s.text, s.len) & 0x7fffffff;
return res | (res == 0);
} }
gb_internal gb_inline StringHashKey string_hash_string(String const &s) { gb_internal gb_inline StringHashKey string_hash_string(String const &s) {
@@ -25,19 +26,16 @@ gb_internal gb_inline StringHashKey string_hash_string(String const &s) {
template <typename T> template <typename T>
struct StringMapEntry { struct StringMapEntry {
String key; String key;
u32 hash; u32 hash;
MapIndex next; T value;
T value;
}; };
template <typename T> template <typename T>
struct StringMap { struct StringMap {
MapIndex * hashes;
usize hashes_count;
StringMapEntry<T> *entries; StringMapEntry<T> *entries;
u32 count; u32 count;
u32 entries_capacity; u32 capacity;
}; };
@@ -73,127 +71,91 @@ gb_internal gb_inline void string_map_init(StringMap<T> *h, usize capacity) {
template <typename T> template <typename T>
gb_internal gb_inline void string_map_destroy(StringMap<T> *h) { gb_internal gb_inline void string_map_destroy(StringMap<T> *h) {
gb_free(string_map_allocator(), h->hashes);
gb_free(string_map_allocator(), h->entries); gb_free(string_map_allocator(), h->entries);
} }
template <typename T> template <typename T>
gb_internal void string_map__resize_hashes(StringMap<T> *h, usize count) { gb_internal void string_map__insert(StringMap<T> *h, u32 hash, String const &key, T const &value) {
h->hashes_count = cast(u32)resize_array_raw(&h->hashes, string_map_allocator(), h->hashes_count, count, MAP_CACHE_LINE_SIZE); if (h->count+1 >= h->capacity) {
} string_map_grow(h);
template <typename T>
gb_internal void string_map__reserve_entries(StringMap<T> *h, usize capacity) {
h->entries_capacity = cast(u32)resize_array_raw(&h->entries, string_map_allocator(), h->entries_capacity, capacity, MAP_CACHE_LINE_SIZE);
}
template <typename T>
gb_internal MapIndex string_map__add_entry(StringMap<T> *h, u32 hash, String const &key) {
StringMapEntry<T> e = {};
e.key = key;
e.hash = hash;
e.next = MAP_SENTINEL;
if (h->count+1 >= h->entries_capacity) {
string_map__reserve_entries(h, gb_max(h->entries_capacity*2, 4));
} }
h->entries[h->count++] = e; GB_ASSERT(h->count+1 < h->capacity);
return cast(MapIndex)(h->count-1);
}
template <typename T> u32 mask = h->capacity-1;
gb_internal MapFindResult string_map__find(StringMap<T> *h, u32 hash, String const &key) { MapIndex index = hash & mask;
MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; MapIndex original_index = index;
if (h->hashes_count != 0) { do {
fr.hash_index = cast(MapIndex)(hash & (h->hashes_count-1)); auto *entry = h->entries+index;
fr.entry_index = h->hashes[fr.hash_index]; if (entry->hash == 0) {
while (fr.entry_index != MAP_SENTINEL) { entry->key = key;
auto *entry = &h->entries[fr.entry_index]; entry->hash = hash;
if (entry->hash == hash && entry->key == key) { entry->value = value;
return fr;
} h->count += 1;
fr.entry_prev = fr.entry_index; return;
fr.entry_index = entry->next;
} }
} index = (index+1)&mask;
return fr; } while (index != original_index);
}
template <typename T> GB_PANIC("Full map");
gb_internal MapFindResult string_map__find_from_entry(StringMap<T> *h, StringMapEntry<T> *e) {
MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL};
if (h->hashes_count != 0) {
fr.hash_index = cast(MapIndex)(e->hash & (h->hashes_count-1));
fr.entry_index = h->hashes[fr.hash_index];
while (fr.entry_index != MAP_SENTINEL) {
auto *entry = &h->entries[fr.entry_index];
if (entry == e) {
return fr;
}
fr.entry_prev = fr.entry_index;
fr.entry_index = entry->next;
}
}
return fr;
} }
template <typename T> template <typename T>
gb_internal b32 string_map__full(StringMap<T> *h) { gb_internal b32 string_map__full(StringMap<T> *h) {
return 0.75f * h->hashes_count <= h->count; return 0.75f * h->count <= h->capacity;
} }
template <typename T> template <typename T>
gb_inline void string_map_grow(StringMap<T> *h) { gb_inline void string_map_grow(StringMap<T> *h) {
isize new_count = gb_max(h->hashes_count<<1, 16); isize new_capacity = gb_max(h->capacity<<1, 16);
string_map_reserve(h, new_count); string_map_reserve(h, new_capacity);
} }
template <typename T>
gb_internal void string_map_reset_entries(StringMap<T> *h) {
for (u32 i = 0; i < h->hashes_count; i++) {
h->hashes[i] = MAP_SENTINEL;
}
for (isize i = 0; i < h->count; i++) {
MapFindResult fr;
StringMapEntry<T> *e = &h->entries[i];
e->next = MAP_SENTINEL;
fr = string_map__find_from_entry(h, e);
if (fr.entry_prev == MAP_SENTINEL) {
h->hashes[fr.hash_index] = cast(MapIndex)i;
} else {
h->entries[fr.entry_prev].next = cast(MapIndex)i;
}
}
}
template <typename T> template <typename T>
gb_internal void string_map_reserve(StringMap<T> *h, usize cap) { gb_internal void string_map_reserve(StringMap<T> *h, usize cap) {
if (h->count*2 < h->hashes_count) { if (cap < h->capacity) {
return; return;
} }
string_map__reserve_entries(h, cap); cap = next_pow2_isize(cap);
string_map__resize_hashes(h, cap*2);
string_map_reset_entries(h); StringMap<T> new_h = {};
new_h.count = 0;
new_h.capacity = cast(u32)cap;
new_h.entries = gb_alloc_array(string_map_allocator(), StringMapEntry<T>, new_h.capacity);
if (h->count) {
for (u32 i = 0; i < h->capacity; i++) {
auto *entry = h->entries+i;
if (entry->hash) {
string_map__insert(&new_h, entry->hash, entry->key, entry->value);
}
}
}
string_map_destroy(h);
*h = new_h;
} }
template <typename T> template <typename T>
gb_internal T *string_map_get(StringMap<T> *h, u32 hash, String const &key) { gb_internal T *string_map_get(StringMap<T> *h, u32 hash, String const &key) {
MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; if (h->count == 0) {
if (h->hashes_count != 0) { return nullptr;
fr.hash_index = cast(MapIndex)(hash & (h->hashes_count-1));
fr.entry_index = h->hashes[fr.hash_index];
while (fr.entry_index != MAP_SENTINEL) {
auto *entry = &h->entries[fr.entry_index];
if (entry->hash == hash && entry->key == key) {
return &entry->value;
}
fr.entry_prev = fr.entry_index;
fr.entry_index = entry->next;
}
} }
u32 mask = (h->capacity-1);
u32 index = hash & mask;
u32 original_index = index;
do {
auto *entry = h->entries+index;
u32 curr_hash = entry->hash;
if (curr_hash == 0) {
// NOTE(bill): no found, but there isn't any key removal for this hash map
return nullptr;
} else if (curr_hash == hash && entry->key == key) {
return &entry->value;
}
index = (index+1) & mask;
} while (original_index != index);
return nullptr; return nullptr;
} }
@@ -216,9 +178,9 @@ gb_internal gb_inline T *string_map_get(StringMap<T> *h, char const *key) {
template <typename T> template <typename T>
gb_internal T &string_map_must_get(StringMap<T> *h, u32 hash, String const &key) { gb_internal T &string_map_must_get(StringMap<T> *h, u32 hash, String const &key) {
isize index = string_map__find(h, hash, key).entry_index; T *found = string_map_get(h, hash, key);
GB_ASSERT(index != MAP_SENTINEL); GB_ASSERT(found != nullptr);
return h->entries[index].value; return *found;
} }
template <typename T> template <typename T>
@@ -239,27 +201,15 @@ gb_internal gb_inline T &string_map_must_get(StringMap<T> *h, char const *key) {
template <typename T> template <typename T>
gb_internal void string_map_set(StringMap<T> *h, u32 hash, String const &key, T const &value) { gb_internal void string_map_set(StringMap<T> *h, u32 hash, String const &key, T const &value) {
MapIndex index; if (h->count == 0) {
MapFindResult fr;
if (h->hashes_count == 0) {
string_map_grow(h); string_map_grow(h);
} }
fr = string_map__find(h, hash, key); auto *found = string_map_get(h, hash, key);
if (fr.entry_index != MAP_SENTINEL) { if (found) {
index = fr.entry_index; *found = value;
} else { return;
index = string_map__add_entry(h, hash, key);
if (fr.entry_prev != MAP_SENTINEL) {
h->entries[fr.entry_prev].next = index;
} else {
h->hashes[fr.hash_index] = index;
}
}
h->entries[index].value = value;
if (string_map__full(h)) {
string_map_grow(h);
} }
string_map__insert(h, hash, key, value);
} }
template <typename T> template <typename T>
@@ -278,62 +228,80 @@ gb_internal gb_inline void string_map_set(StringMap<T> *h, StringHashKey const &
} }
// template <typename T>
// gb_internal void string_map__erase(StringMap<T> *h, MapFindResult const &fr) {
// MapFindResult last;
// if (fr.entry_prev == MAP_SENTINEL) {
// h->hashes[fr.hash_index] = h->entries[fr.entry_index].next;
// } else {
// h->entries[fr.entry_prev].next = h->entries[fr.entry_index].next;
// }
// if (fr.entry_index == h->count-1) {
// array_pop(&h->entries);
// return;
// }
// h->entries[fr.entry_index] = h->entries[h->count-1];
// last = string_map__find(h, h->entries[fr.entry_index].key);
// if (last.entry_prev != MAP_SENTINEL) {
// h->entries[last.entry_prev].next = fr.entry_index;
// } else {
// h->hashes[last.hash_index] = fr.entry_index;
// }
// }
// template <typename T>
// gb_internal void string_map_remove(StringMap<T> *h, StringHashKey const &key) {
// MapFindResult fr = string_map__find(h, key);
// if (fr.entry_index != MAP_SENTINEL) {
// string_map__erase(h, fr);
// }
// }
template <typename T> template <typename T>
gb_internal gb_inline void string_map_clear(StringMap<T> *h) { gb_internal gb_inline void string_map_clear(StringMap<T> *h) {
h->count = 0; h->count = 0;
for (u32 i = 0; i < h->hashes_count; i++) { gb_zero_array(h->entries, h->capacity);
h->hashes[i] = MAP_SENTINEL; }
template <typename T>
struct StringMapIterator {
StringMap<T> *map;
MapIndex index;
StringMapIterator<T> &operator++() noexcept {
for (;;) {
++index;
if (map->capacity == index) {
return *this;
}
StringMapEntry<T> *entry = map->entries+index;
if (entry->hash != 0) {
return *this;
}
}
} }
bool operator==(StringMapIterator<T> const &other) const noexcept {
return this->map == other->map && this->index == other->index;
}
operator StringMapEntry<T> *() const {
return map->entries+index;
}
};
template <typename T>
gb_internal StringMapIterator<T> end(StringMap<T> &m) noexcept {
return StringMapIterator<T>{&m, m.capacity};
}
template <typename T>
gb_internal StringMapIterator<T> const end(StringMap<T> const &m) noexcept {
return StringMapIterator<T>{&m, m.capacity};
} }
template <typename T> template <typename T>
gb_internal StringMapEntry<T> *begin(StringMap<T> &m) noexcept { gb_internal StringMapIterator<T> begin(StringMap<T> &m) noexcept {
return m.entries; if (m.count == 0) {
return end(m);
}
MapIndex index = 0;
while (index < m.capacity) {
if (m.entries[index].hash) {
break;
}
index++;
}
return StringMapIterator<T>{&m, index};
} }
template <typename T> template <typename T>
gb_internal StringMapEntry<T> const *begin(StringMap<T> const &m) noexcept { gb_internal StringMapIterator<T> const begin(StringMap<T> const &m) noexcept {
return m.entries; if (m.count == 0) {
} return end(m);
}
MapIndex index = 0;
template <typename T> while (index < m.capacity) {
gb_internal StringMapEntry<T> *end(StringMap<T> &m) { if (m.entries[index].hash) {
return m.entries + m.count; break;
} }
index++;
template <typename T> }
gb_internal StringMapEntry<T> const *end(StringMap<T> const &m) noexcept { return StringMapIterator<T>{&m, index};
return m.entries + m.count;
} }
+3 -1
View File
@@ -208,7 +208,9 @@ gb_internal void string_set__erase(StringSet *s, MapFindResult fr) {
} }
auto *entry = &s->entries[fr.entry_index]; auto *entry = &s->entries[fr.entry_index];
*entry = s->entries[s->entries.count-1]; *entry = s->entries[s->entries.count-1];
StringHashKey key = {entry->hash, entry->value}; StringHashKey key;
key.hash = entry->hash;
key.string = entry->value;
last = string_set__find(s, key); last = string_set__find(s, key);
if (last.entry_prev != MAP_SENTINEL) { if (last.entry_prev != MAP_SENTINEL) {
s->entries[last.entry_prev].next = fr.entry_index; s->entries[last.entry_prev].next = fr.entry_index;
+1 -2
View File
@@ -767,9 +767,8 @@ gb_internal void tokenizer_get_token(Tokenizer *t, Token *token, int repeat=0) {
} }
} }
// TODO(bill): Better Error Handling
if (valid && n != 1) { if (valid && n != 1) {
tokenizer_err(t, "Invalid rune literal"); tokenizer_err(t, token->pos, "Invalid rune literal");
} }
token->string.len = t->curr - token->string.text; token->string.len = t->curr - token->string.text;
goto semicolon_check; goto semicolon_check;
+1 -1
View File
@@ -277,7 +277,7 @@ test_chacha20poly1305 :: proc(t: ^testing.T) {
test_rand_bytes :: proc(t: ^testing.T) { test_rand_bytes :: proc(t: ^testing.T) {
tc.log(t, "Testing rand_bytes") tc.log(t, "Testing rand_bytes")
if !crypto.has_rand_bytes() { if !crypto.HAS_RAND_BYTES {
tc.log(t, "rand_bytes not supported - skipping") tc.log(t, "rand_bytes not supported - skipping")
return return
} }