<keegans>
can't find references to the keys in the source either
<keegans>
ok nvm- got it. it's @ src-self-hosted/libc_installation.zig
frmdstryr has joined #zig
craigo has quit [Ping timeout: 244 seconds]
waleee-cl has quit [Quit: Connection closed for inactivity]
doesntgolf has quit [*.net *.split]
st4ll1 has quit [*.net *.split]
Cadey has quit [*.net *.split]
eddyb[legacy] has quit [*.net *.split]
dongcarl has quit [*.net *.split]
traviss has quit [*.net *.split]
corvid has quit [*.net *.split]
_whitelogger has joined #zig
HesamR has joined #zig
HesamR has quit [Quit: Connection closed]
WilhelmVonWeiner has quit [Read error: Connection reset by peer]
bgiannan has quit [Read error: Connection reset by peer]
marnix has quit [Ping timeout: 260 seconds]
Ashpool has joined #zig
traviss has quit [Remote host closed the connection]
dermetfan has joined #zig
_whitelogger has joined #zig
dddddd has joined #zig
waleee-cl has joined #zig
FireFox317 has joined #zig
xackus_ has joined #zig
craigo has joined #zig
marnix has quit [Read error: Connection reset by peer]
Ashpool_ has joined #zig
Ashpool has quit [Ping timeout: 246 seconds]
marnix has joined #zig
xackus_ has quit [Ping timeout: 260 seconds]
marnix has quit [Ping timeout: 265 seconds]
marnix has joined #zig
Ashpool_ has quit [Remote host closed the connection]
FireFox317 has quit [Ping timeout: 256 seconds]
<ikskuh>
hm
<ikskuh>
how can i write a file to an arbitrary directory from build.zig?
<ikskuh>
using addWriteFile doesn't look like it's build for this job
mikdusan has joined #zig
tsujp has joined #zig
doesntgolf has joined #zig
tsujp has quit [Quit: WeeChat 2.9]
tsujp has joined #zig
<ifreund>
ikskuh: inside the install prefix?
<ifreund>
installFile() takes a relative path
<ikskuh>
no, in the build directories
<ikskuh>
so relative to build.zig
<ifreund>
I suppose you just use std.fs then no?
<ikskuh>
yeah, but it's not a build step then :(
<ikskuh>
well, enough for now
KoljaKube has joined #zig
tsujp has quit [Quit: WeeChat 2.9]
tsujp has joined #zig
FireFox317 has joined #zig
<ikskuh>
damn you zig and your undefined behaviour sanitizer in C :D
waleee-cl has quit [Quit: Connection closed for inactivity]
<tsujp>
if I have a file `c.zig` which is nothing but c imports via `usingnamespace @cImport` how do I import those imports into another zig file and use them?
<ifreund>
tsujp: you need to do `pub usingnamespace ... `
<tsujp>
and sorry I have, then it's just `@import("path/to/file.zig");`?
<ifreund>
and then you can `const c = @import("c.zig"); c.foo();`
<ifreund>
paths for imports are relative
<tsujp>
gotcha
<ifreund>
also if you didn't realize yet, files in zig are structs
<tsujp>
good to know, still getting the hang of zig just found it a few days ago
<tsujp>
I'm curious, I'm going through the source code for that exmaple game oxid (here: https://github.com/dbandstra/oxid) and I can see the use of `zig-hunk` which is inspired from Quake's hunk
<tsujp>
Why did they need a stack allocated memory management library when you can use the Heap?
<tsujp>
Or is it that this is being alloocated on the heap (surely it is...?) and that this library gives you a "stack-like" data structure to use
marnix has quit [Ping timeout: 256 seconds]
bastienleonard has joined #zig
KoljaKube has quit [Ping timeout: 272 seconds]
<ifreund>
tsujp: not sure, probably something to do with wasm which seems to be the target for that
<tsujp>
I did some digging and apparently the hunk is a datastructure the quake engine used to keep heap memory allocated to it contiguous by allocating high and moving "inwards" as required
<tsujp>
ifreund: say there is a set of items { 1, 2, 3, 4, 5, 6 } the slice syntax `[1..]` means "index 1 until the end" right?
<ifreund>
sounds like a performance thing then
<ifreund>
tsujp: yep
KoljaKube has joined #zig
frmdstryr has quit [Ping timeout: 260 seconds]
Sahnvour has quit [Quit: WeeChat 2.8]
riba has joined #zig
marnix has joined #zig
Akuli has joined #zig
<KoljaKube>
Hm, Allocator does not work with non-slice pointers?
<keegans>
this functionality requires allocation, is that available in the compiler_rt at this time?
Sahnvour has joined #zig
<FireFox317>
keegans, hmm good question, im not sure actually
<keegans>
yeah, it's somewhat problematic since we need a GPA to expand TLS on demand
<keegans>
we could hypothetically allocate a big ELF section and cap it, but that is suboptimal
marnix has quit [Read error: Connection reset by peer]
marnix has joined #zig
<KoljaKube>
ikskuh: But that does not support reallocation, right?
<ikskuh>
no, as it's illogical to reallocate a single object ;)
<ikskuh>
single objects don't change size
<KoljaKube>
Ah, OK, didn't try it out but I intended to `create` a [N]u8, where reallocation would make sense (even if not strictly speaking)
<ikskuh>
err
<ikskuh>
that's just allocator.alloc(u8, N)
<ikskuh>
so allocate a slice of u8 instead of a fixed-size array
<ikskuh>
[N]u8 will always be N elements large
<ikskuh>
if you allocate multiple of them, you get another [N]u8
<ikskuh>
if you allocate a []u8, you get a dynamic sized type (slice)
<KoljaKube>
I'm trying to wrap a C library where the only memory allocation function that I can pass in is a realloc(c_void*, usize)
<fengb>
That’s not possible with naive Zig allocators
<fengb>
I’m actually working on a compat layer that can enable C style allocations
<ikskuh>
realloc() is fully capable and you can route a global allocator into those
<ikskuh>
so you have a realloc() function that accesses a global allocator
<fengb>
But most Zig allocators cannot take a pointer and reconstitute its size
<ikskuh>
realloc(NULL, size) is "alloc", realloc(ptr, size) is "realloc", alloc(ptr, 0)" is free
<KoljaKube>
I also have a pointer to an allocator instance in that call, I don't need it to be global
<ikskuh>
oh, neat
<KoljaKube>
I had hoped that I could somehow, inside my wrapper, bridge all the differences
<fengb>
Oh we might be working on something similar
<KoljaKube>
ikskuh: My PR isn't accepted upstream, but I added a c_void* userData ;-)
<KoljaKube>
Is the memory layout of a slice documented somewhere?
<fengb>
No it’s intentionally unspecified
ask6155 has joined #zig
<ask6155>
hello!
<KoljaKube>
fengb: So no hope of, say, extra-allocating two usizes extra at the start to store some more information about the following memory?
<KoljaKube>
ask6155: Hi there!
<ask6155>
hi!
<fengb>
You can allocate @sizeOf([]T). That’d be fixed but the layout isn’t
<KoljaKube>
By the way, I'm used to new/delete, but I'm assuming that reallocating stuff is usually done for complete arrays/struct/etc, not in the middle of them, right?
<fengb>
I’m not actually sure why though. I’d love to put slices into packed structs :(
<KoljaKube>
OK, so I can take the requested memory size, allocate a slice instead, and return the .ptr, right? And if it's time to realloc, I am passed said ptr. Can I then use some builtin to get the slice's address from that ptr?
<fengb>
A slice is a glorified pointer
<KoljaKube>
I thought it's a pointer + a size
<fengb>
Yeah
<keegans>
is there a string replacement impl in Zig std or do I roll my own?
<fengb>
There’s no real way of getting the address of reversing the ptr to slice though
<KoljaKube>
So that size must be stored somewhere. And if it's not a defined layout, I can't just assume "the usize before the ptr"
<KoljaKube>
Ah, OK
<KoljaKube>
Too bad
<fengb>
Right, I don’t think you can reliably get the data
<fengb>
You can create your own packed struct though
* KoljaKube
slaps his head
<KoljaKube>
duh
<fengb>
How I’m doing it is putting the size exactly 16 bytes before the pointer I return to the user
<KoljaKube>
So I guess I can rely on every pointer passed to realloc being something I initially returned from some allocation function, right?
<fengb>
That’s the expectation placed on the dev yes
<KoljaKube>
On the dev calling or implementing realloc?
<fengb>
Calling
<fengb>
We usually insert some asserts for sanity
<KoljaKube>
OK, good
<fengb>
Usually = sometimes
<ikskuh>
more asserts for all!
<KoljaKube>
How would I assert that without keeping a list?
<fengb>
Depends on metadata layout
<KoljaKube>
OK
<fengb>
My more complicated allocator has specific numbers in memory locations that it can be
<fengb>
So it’s semi trivial to do an extra check to make sure those are sane
<KoljaKube>
So it's 100% sure, but close enough?
<fengb>
Yeah there’s a pointer which needs to be aligned, and a size which can only be a power of 2
<fengb>
So it’s a useful sanity check, but can’t be completely relied upon
<fengb>
The debug allocator would eventually check for a lot of these cases
<KoljaKube>
OK, I think I'm getting somewhere. Thanks, you helped a lot!
<fengb>
np
<ask6155>
If I have a C struct which can have a field as null, can I set it as an undefined and get away with it?
<KoljaKube>
undefined should be uninitialized as far as I understand
riba has quit [Remote host closed the connection]
<fengb>
Yeah, it’s set to 0xaaaa in safe modes for debugging but it’s “probably” uninitialized otherwise
<KoljaKube>
Depends on what exactly you are doing, but I have been using a lot of std.mem.zeroes for large C structs lately
<tsujp>
zig docs state `Use undefined to leave variables uninitialized` for `master`
<KoljaKube>
(zero the whole struct, set what you want)
<ask6155>
It says missing field
<KoljaKube>
Not very zig-like, but initializing structs with dozens of fields at define-time is not practical
<fengb>
You need to assign something to the field. Undefined is fine if you’ll overwrite it later before using it
<KoljaKube>
Yeah, you can't just leave it out
<fengb>
There’s also mem.zeroInit, where it’ll zero out any field that wasn’t specified
FireFox317 has quit [Quit: Leaving]
<ask6155>
The field is a const char* but it can be NULL which means the library will detect it or something
<KoljaKube>
You bring up a good point, I had a similar problem earlier. To return NULL to C, I had to `return @intToPtr(*allowzero c_void, 0)` - that can't be the best way
<ask6155>
How do I convert 0x0001 to a []const u8?
<fengb>
Change the ptr definition to `?*c_void` and you can assign null
<ask6155>
what
<fengb>
That was meant for KoljaKube
<ask6155>
oh
<KoljaKube>
Heh, all those permutations of casts and return values, and I must have missed that one. Seems obvious, thought I checked that
<fengb>
ask6155: do you want a one element slice? “\x01” is the easiest as a constant
<fengb>
`[_]u8{0x01}` will build an array and you can slice that
<ask6155>
yeah I wanted a one element slice
<tsujp>
Is there a way to specify style with zig similar to how you can with clang (I think it is?)
<KoljaKube>
Arbitrary u16s should be castable to fixed-sized arrays before slicing, if I'm not mistaken
<KoljaKube>
tsujp: Are you talking about zig fmt?
<tsujp>
KoljaKube: I think so? I am using the sublime text Zig pacakage and it's changing the whitespace in my code etc
<KoljaKube>
There are no options there I think
<KoljaKube>
Yeah, that would be fmt
<fengb>
You can disable formatting but there’s no config for the formatter
<ask6155>
where is the documentation for "\x01" syntax?
<KoljaKube>
If you end lists with trailing commas, the lists will be multiline, without they will be fit in one line if possible, if that helps
<tsujp>
ask6155: specifically: `\xNN hexadecimal 8-bit character code (2 digits)`
<tsujp>
Is there an API for `fmt` at the moment? I could work on a PR to add options to it for instance
<KoljaKube>
I've really grown to like no-option formatters. I may not like the result, but at least there won't be a discussion about it :D
<fengb>
Intentionally no options
<tsujp>
Fair enough
<Akuli>
"it's a feature, not a bug" :D
<tsujp>
I guess I shall try and stick with it hohoh
<KoljaKube>
But if it would use tabs instead of spaces I couldn't use it ;-)
<tsujp>
Eww tabs gross
<tsujp>
Tabs aren't even allowed in source files are they
<KoljaKube>
Nope
<Akuli>
here we go again lol
<tsujp>
Take that tab vs spaces people
<fengb>
To quote Go fmt: it’s nobody’s friend. Therefore it’s everybody’s friend
<tsujp>
Actually the only reason I was curious about `fmt` is because I see in the docs the return of structs formatted as `return StructName {` whereas `fmt` is formatting my struct return as `return StructName{` (no space between StructName and {)
marnix has quit [Ping timeout: 246 seconds]
<tsujp>
Very, very, very, very trivial that single space but I was just curious
<fengb>
Hmm yeah I noticed that too. I dunno if there’s been discussion
<fengb>
fmt is open to change. We just don’t want configuration options :P
<tsujp>
I'll subscribe to that
marnix has joined #zig
dermetfan has quit [Ping timeout: 272 seconds]
<ask6155>
ok I passed an optional i32 which is set as NULL to my C function. There was no compile error but when I ran the program it said attempt to use null value. But that was the POINT!
marnix has quit [Ping timeout: 264 seconds]
<KoljaKube>
C doesn't know optional values
<ask6155>
so how do I pass a pointer to null?
<KoljaKube>
Oh, I misread, you weren't passing a value
<KoljaKube>
I think you should be able to pass null optionals as null ptrs. What's the function parameter type and the type of the variable exactly?
<ask6155>
"notcurses_init prepares the terminal for cursor-addressable (multiline) mode. The FILE provided as fp must be writable and attached to a terminal, or NULL. If it is NULL, /dev/tty will be opened. "
<KoljaKube>
And you are passing null for fp?
<tsujp>
What is u29?
<tsujp>
..29-bit integer?
<KoljaKube>
29 bit unsigned integer
<KoljaKube>
maximum size of an alignment paramtere
<tsujp>
why would 29 bit unsigned integer exist?
<tsujp>
ah
<ask6155>
I don't know how to pass NULL, I made an optional pointer to i32 and dereferenced it.
<KoljaKube>
Zig has arbitrary bit size integers
<KoljaKube>
Well, dereferencing an optional that is null should be an error
<tsujp>
what does this part of the docs concerning alignment mean?
<fengb>
Alignment must be u29 because it prevents accidentally passing in something else
<tsujp>
right so with alignment if you had a 3-byte sized data type, you'd have that aligned to mod 3 increments
<ask6155>
oof that means I still would have to do some wrapping to get stuff done.
<leeward>
ask6155: Most C libraries I've used in Zig can be used wrapper-free, but I find writing Zig wrappers around the parts I use to make it easier to write clean code.
<ask6155>
yeah but I still have to do good error handling which I guess is a good thing :)
<leeward>
yep
<leeward>
Converting a return code + ERRNO to a Zig error makes life really nice, and it's easy.
<zigusr>
where can I see examples for stdlib usage?
<leeward>
zigusr: Can you be more specific? The vast majority of Zig code uses the standard library.
<zigusr>
yeah, I'm coding my first line in zig, and I want to push / pull a struct from an TailQueue
<zigusr>
so duckduckgo TailQueue examples doesn't help me :)
<tsujp>
is using `.ptr` safe? the docs have me a tad bit confused, if I'm reading this correctly `.ptr` on an array gives the runtime-only-known pointer length
<tsujp>
is this effectively the size of the array?
<tsujp>
size as in size in memory not elements?
<leeward>
zigusr: zig/lib/std/linked_list.zig has a test called "TailQueue concatenation"
<cren>
there are example programs in the std library which I find immensely helpful
<leeward>
tsujp: There isn't a point to `_ = 10;`
<leeward>
tsujp: But, say you don't care about printf's return value. You can do `_ = c.printf(...);`
<tsujp>
oh wait hang on, say I had a function that returned `u29` for example, and I wanted the function to run but as you say I didn't care about what it returned at all I'd then use `_` right? `_ = runThatu29FunctionPlease()`?
<leeward>
tsujp: Exactly.
<tsujp>
And this keeps the typing of functions but allows them to effectively work as `void` (from C) if I so desire
<leeward>
It says "this function is being run for its side effects" to anyone reading the code, which should raise alarms.
<leeward>
yep
<leeward>
The only value you can ignore is `{}` which is the void value.
<pixelherodev>
But that doesn't stop the normal _start from being defined, does it?
<leeward>
Easy to find out!
<pixelherodev>
I know
<pixelherodev>
I'm looking rn ;P
<pixelherodev>
It works, yeah
<pixelherodev>
start.zig exports start only if the root file doesn't have a start function
<leeward>
hmm, didn't let override the return value from main though
<cren>
I can't build a hello world with pub fn main() noreturn {...}
<cren>
`expected type 'noreturn', found 'void'
<leeward>
That's because you returned from main.
<leeward>
You probably want void, not noreturn.
<cren>
I was just experimenting
<leeward>
Try putting `std.os.abort()` at the end of your main function. That'll let it be noreturn.
<cren>
cool. 'zsh: abort bin/main'
<cren>
appears to return with status 134
<cren>
is there going to be a context management statement in zig like python's 'with'? there's 'defer' in zig and I was just wondering whether that's supposed to replace 'with'/'using' or not
<leeward>
I don't think there's a plan to put context managers in the language, though they wouldn't be hard to implement in userspace.
<fengb>
Not currently in the plans. There’s a parent issue about resource management though
<cren>
how can I see the type of a thing while debugging? using print with format strings just turns the type into the word "type" apparently
<fengb>
@typeNam3
<fengb>
@typeName
<leeward>
Ordering delivery during a pandemic is not always good. 2 hours later, the order is cancelled.
<cren>
a belated thank you fengb
<cren>
oh
<cren>
'error: expected type 'type' found '@TypeOf(std.net.Address.parseIp).ReturnType.ErrorSet!std.net.Address'?
<cren>
the thing I'm trying to print is the returned value from net.Address.parseIp
<leeward>
Forget a try?
<cren>
leeward: that's probably it
keegans has quit [Quit: WeeChat 2.7]
nvmd has joined #zig
marnix has joined #zig
nvmd has quit [Quit: Later nerds.]
KoljaKube has quit [Ping timeout: 246 seconds]
waleee-cl has joined #zig
marnix has quit [Ping timeout: 240 seconds]
opDispatch has quit [Quit: Konversation terminated!]
Ashpool has quit [Read error: Connection reset by peer]
Ashpool has joined #zig
marnix has joined #zig
jjido has joined #zig
cren has quit [Ping timeout: 246 seconds]
xackus_ has quit [Ping timeout: 240 seconds]
jjido has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
marnix has quit [Ping timeout: 260 seconds]
craigo has quit [Ping timeout: 240 seconds]
cole-h has quit [Quit: Goodbye]
frett27 has quit [Ping timeout: 240 seconds]
Akuli has quit [Quit: Leaving]
nikita` has quit [Quit: leaving]
doublex has quit [Read error: Connection reset by peer]
traviss has joined #zig
_whitelogger has joined #zig
keegans has joined #zig
Sahnvour has quit [Quit: WeeChat 2.9]
bastienleonard has quit [Ping timeout: 272 seconds]
<ronsor>
Well, I have a bit of a new issue. Calling C code from zig (target: x86_64, UEFI, MSVC ABI) that attempts to use struct assignment (`struct A a, b; a = b;`) results in #06 Invalid Opcode (QEMU 2.11.1, KVM, -cpu max). This *always* happens.
<keegans>
that's a really old QEMU?
<ronsor>
I just used what came with my package manager. Hmm.