From 4722e6a61fdeda303b609087270ea6c5f20e2724 Mon Sep 17 00:00:00 2001 From: Judah Caruso Date: Fri, 9 Jan 2026 16:54:10 -0700 Subject: [PATCH] additions --- containerx/xar/xar.go | 1 - mem/mem.go | 7 +++++++ osthread/osthread.go | 10 ++++++---- xx.go | 38 +++++++++++++++++++++++++++++++------- 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/containerx/xar/xar.go b/containerx/xar/xar.go index fd2b40b..c533c4d 100644 --- a/containerx/xar/xar.go +++ b/containerx/xar/xar.go @@ -63,7 +63,6 @@ func (x *Xar[T]) AppendMany(values ...T) *T { } first := x.Append(values[0]) - if len(values) > 1 { for _, v := range values[1:] { x.Append(v) diff --git a/mem/mem.go b/mem/mem.go index 08e143b..2ca7509 100644 --- a/mem/mem.go +++ b/mem/mem.go @@ -40,6 +40,13 @@ func BitCast[TOut any, TIn any](value *TIn) TOut { return *((*TOut)(unsafe.Pointer(value))) } +func BitCastValue[TOut any, TIn any](value TIn) TOut { + if Sizeof[TOut]() != Sizeof[TIn]() { + panic("bitcast: sizes of types must match") + } + return *((*TOut)(unsafe.Pointer(&value))) +} + // Copy copies size number of bytes from src into dst. // // Returns dst. diff --git a/osthread/osthread.go b/osthread/osthread.go index a10f1b6..15a630a 100644 --- a/osthread/osthread.go +++ b/osthread/osthread.go @@ -14,7 +14,9 @@ // } package osthread -import "runtime" +import ( + "runtime" +) // Start allows arbitrary functions to be run on the main operating system thread. // @@ -24,14 +26,14 @@ func Start(entrypoint func()) { done := make(chan any) - // Run entrypoint in a separate goroutine. - go func() { + // Immediately queue entrypoint + Go(func() { defer func() { done <- nil }() entrypoint() - }() + }) // Call functions in our queue until entrypoint returns. // These functions are called on the main operating system thread. diff --git a/xx.go b/xx.go index 92a4a8a..13e2c7e 100644 --- a/xx.go +++ b/xx.go @@ -1,18 +1,13 @@ package xx import ( + "runtime" + "strings" "unsafe" "git.brut.systems/judah/xx/mem" ) -// New returns a newly allocated value with an initial value. -func New[T any](expr T) *T { - p := new(T) - *p = expr - return p -} - // Copy copies src number of bytes into dst. // Returns dst. // @@ -34,3 +29,32 @@ func Clone[T any](value *T) *T { func BoolUint(b bool) uint { return uint(*(*uint8)(unsafe.Pointer(&b))) } + +// CallerLocation returns the source location of the function CallerLocation is called in. +func CallerLocation() (file string, line int) { + _, file, line, _ = runtime.Caller(2) + + // @todo: I'm sure there's a better way to do this + // Special-case when CallerLocation is called from main + if strings.Contains(file, "runtime") && strings.Contains(file, "proc.go") { + _, file, line, _ = runtime.Caller(1) + } + + return +} + +// HashLocation returns a hash of file and line, most likely returned from [CallerLocation]. +func HashLocation(file string, line int) uint64 { + const ( + FNV64_PRIME uint64 = 0x100000001B3 + FNV64_BIAS uint64 = 0xCBF29CE484222325 + ) + + h := FNV64_BIAS + for _, c := range file { + h = (h ^ uint64(c)) * FNV64_PRIME + } + + h = (h ^ uint64(line)) * FNV64_PRIME + return h +}