rayonix has quit [Remote host closed the connection]
rayonix has joined #zig
very-mediocre has joined #zig
_whitelogger has joined #zig
rayonix has quit [Quit: rayonix]
rayonix has joined #zig
_whitelogger has joined #zig
_whitelogger has joined #zig
_whitelogger has joined #zig
avoidr has quit [Quit: leaving]
adrusi has joined #zig
_whitelogger has joined #zig
Sahnvour has joined #zig
_whitelogger has joined #zig
Zaab1t has joined #zig
qazo has quit [Ping timeout: 258 seconds]
* daurnimator
waves
wootehfoot has joined #zig
rivten has joined #zig
shritesh has joined #zig
marmotini_ has joined #zig
marmotini_ has quit [Max SendQ exceeded]
marmotini_ has joined #zig
wootehfoot has quit [Ping timeout: 250 seconds]
qazo has joined #zig
qazo has quit [Read error: Connection reset by peer]
<rivten>
hey everybody ! I hope you are doing fine :) I'm pretty new to Zig and I've got a quick question (hope this is a good place to ask :P). I'm debugging some memory allocation (with FixedBufferAllocator) and, in the process of understanding what's going on, I'm stuck on understandling this lign of the allocator : https://github.com/ziglang/zig/blob/master/std/mem.zig#L105. Like : why using undefined parameter and what is even ([*]u8)(undefined)[0..0] ? Thanks a
<rivten>
lot :)
<Sahnvour>
rivten, the underlying allocator interface is based on two functions: `reallocFn` and `shrinkFn`. reallocFn is used to increase the allocation size (which is potentially 0 for a new allocation) and shrink to reduce it. in this case, as `alignedAlloc` is known to be called when the user wants a new allocation, this calls reallocFn to query for a new allocation of size `byte_count` but has to pass the "old" (in this case,
<Sahnvour>
undefined) allocation to satisfy the interface
<Sahnvour>
and this is a good place to ask :)
<daurnimator>
rivten: passing undefined like that means we don't care what we pass as that parameter
<daurnimator>
note that in debug builds, your program will panic if it tries to use (e.g. compare/add) an undefined value.
<daurnimator>
rivten: `([*]u8)(undefined)[0..0]` is a way to get a 0 length slice.
<daurnimator>
to decompose it: `[*]u8(undefined)` an undefined pointer to an unknown number of u8 values. => `[0..0]` => take a 0 length slice of it. so now we have a slice of u8 with an undefined pointer, and a length of 0.
strmpnk has left #zig ["Closing Window"]
strmpnk has joined #zig
Zaab1t has quit [Quit: bye bye friends]
jjido has joined #zig
<rivten>
ok thanks everyone ! what's weird is that : if undefined means "it shouldn't be used in the called function", then the realloc, on a FixedBufferAllocator, we do use the old_mem and old_align params which are passed as undefined https://github.com/ziglang/zig/blob/master/std/heap.zig#L314 hmmmm
<scientes>
rivten, llvm will optimize out those things
<scientes>
when it know val is undefined
<rivten>
oh ok. So if my program fails on the OutOfMemory error, then it's the compile time pass that was not able to properly resolve the memory allocation ? (otherwise it would have been optimized out ?)
<scientes>
undefined behavior is undefined
<Sahnvour>
rivten, OutOfMemory error is more likely to come from a programming mistake, I don't think the optimization pass should have any impact on the observable result of your code
<daurnimator>
huh? no
<daurnimator>
rivten: you get OutOfMemory if there wasn't enough memory available to satisfy the request; whether that's because your fixedbuffer is too small; your OS failed the mmap() call; or if everything is just too fragmented
<daurnimator>
OutOfMemory is rarely if ever a programming mistake
<Sahnvour>
asking too much memory from a fixed buffer is a programming mistake
<rivten>
hmm ok ! I will keep reviewing my code then :P it's weird but I do give a _very_ high fixed buffer size, but still get an error. Will keep debugging. Thanks a lot everybody ! :)
<jjido>
rivten using newruntime?
<jjido>
oops wrong channel
qazo has joined #zig
shritesh has quit [Ping timeout: 246 seconds]
marmotini_ has quit [Ping timeout: 246 seconds]
scientes has quit [Ping timeout: 250 seconds]
wootehfoot has joined #zig
marmotini_ has joined #zig
return0e has quit [Remote host closed the connection]
jjido has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
qazo has quit [Ping timeout: 246 seconds]
<donpdonp>
im trying to alloc ram for a new struct, but gettting an error I dont understand
<donpdonp>
pub const Actor = struct {...}; if (allocator.alloc(Actor, 1)) |actor| { ...} => error: expected optional type, found '@typeOf(std.mem.Allocator.alloc).ReturnType.ErrorSet![]thread.Actor'
<donpdonp>
A!B type seems like it is an 'optional type'
return0e has joined #zig
<tyler569>
?T is the optional type (a value of T or null), A!B is the error union type (a value of type B or an error of set A)
<donpdonp>
isnt expr in 'if(expr) |value| { }' expected to be an error union?
<tyler569>
I think you might have to provide an else clause for error unions, since zig makes strong guarantees about handling error conditions
<tyler569>
it could just be else { unreachable } if you want to assert that that will never happen and get a stack trace in debug mode if it does
<donpdonp>
ah that was the problem, thx!
a_chou has joined #zig
a_chou has quit [Client Quit]
jjido has joined #zig
qazo has joined #zig
<tgschultz>
generally we use `try` and `catch` to unwrap error unions: `const instance = try allocator.alloc(Actor, 1);` or `const instance = allocator.alloc(Actor, 1) catch unreachable;` also, for single instances you can use alloc.create(T) and alloc.destroy(T);
jjido has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
rivten has quit [Remote host closed the connection]
<donpdonp>
nod. thx.
marmotini_ has quit [Ping timeout: 246 seconds]
<donpdonp>
my struct has a field of []u8. im passing in a []u8 and warn says the addr is "[]const u8@204b40" which seems reasonable, but when I set my_struct.array = my_u8_arry, and warn/print the address of &my_struct.array, I get "[]const u8@7fff345cc5b8"
<donpdonp>
i figured the address by my_struct.array would be the first address. also surprised by the difference in width of the address
rayonix has quit [Quit: rayonix]
<tgschultz>
[]u8 is a slice of u8 (ptr + len), not an array
<tgschultz>
&my_struct.array gives the address of the slice, not what it points to
<tgschultz>
my_struct.ptr would be where the slice points
* donpdonp
hmms
qazo has quit [Ping timeout: 250 seconds]
<tgschultz>
er: my_struct.array.ptr
mindfv has joined #zig
<donpdonp>
fn myfun(data: []const u8) how do i tell if data is an array or a slice?
<donpdonp>
and similarly, struct { payload: []const u8 }, is that an array or a slice?
<Sahnvour>
array types have their size inside the brackets, it is known at compile time
<donpdonp>
how do i get the type of a variable? @typeName(my_var) complains my_var is not a type, which is understandable, im just not sure how to get at the type
<emekankurumeh[m]>
@typeName(@typeOf(my_var))
<donpdonp>
thx!
<donpdonp>
warn("{} {}", actor, &actor); both print the struct. im trying to get the address of the struct
<emekankurumeh[m]>
`warn("{} {p}", actor, &actor);` should work
<emekankurumeh[m]>
`warn("{} {*}", actor, &actor);` should print the type and the address.
<tyler569>
std isn't super well documented, but std/fmt.zig is pretty eazy to follow in my opinion. it implements a state machine, so you just look for the character to State mapping and follow that
<donpdonp>
emekankurumeh[m]: {*} is great. looks like ruby.
Ichorio has joined #zig
return0e has quit [Remote host closed the connection]
<donpdonp>
here's another puzzler. doit(number) => error: expected type 'c_ulong', found '*c_ulong'
<donpdonp>
doit(*number) => error: expected type 'type', found '*c_ulong'
<donpdonp>
oh i just remembered seeing in the docs, its number.*
scientes has joined #zig
qazo has quit [Read error: Connection reset by peer]
wootehfoot has quit [Read error: Connection reset by peer]
<donpdonp>
here's another one. im passing a struct to pthread_create, and the callback func is getting the right address in the void ptr, but when i look at the contents, its all zeros
<donpdonp>
var actor = Actor@7ffedb6d2248 Actor{ .thread_id = c_ulong@7ffedb6d2290 ...}; pthread_create(..., &actor); thats fine
<donpdonp>
callback is fn go(data: ?*c_void); Actor@7ffedb6d2248 Actor{ .thread_id = c_ulong@0 ...} (all data zeroed out)
<donpdonp>
i had to use var data8 = @alignCast(@alignOf(thread.Actor), data); var actor = @ptrCast(*thread.Actor, data8);
<donpdonp>
so maybe im not doing something right there.
qazo has joined #zig
Sahnvour has quit [Read error: Connection reset by peer]
<bketelsen>
shritesh: epic wasm, my friend. That's awesome.
return0e has joined #zig
ltriant has joined #zig
return0e has quit [Remote host closed the connection]
hio has quit [Quit: Connection closed for inactivity]