6 ms·
Just a correction: most std C functions don’t allocate. strdup does but it was only recently adopted into the standard, it was previously an extension. Similar
by alchemio 3y ago
Just a correction: most std C functions don’t allocate. strdup does but it was only recently adopted into the standard, it was previously an extension.
Similarly zig’s stdlib shouldn’t allocate behind your back, except for thread spawn where it does:
https://github.com/ziglang/zig/blob/5cd7fef17faa2a40c8da23f0ef2485df0af39ed4/lib/std/Thread.zig#L666 https://github.com/ziglang/zig/blob/5cd7fef17faa2a40c8da23f0...
Generally speaking, it’s as mentioned just a convention. A zig library might not allow its users to pass allocators for example.
In C++, stl containers can take an allocator as a template parameter. Recent C++ versions also provide several polymorphic allocators in the stdlib. You can also override the global allocator or a specific class’ allocator (override placement new).
- AndyKelley 3y agoFor spawning a thread you're literally asking for the stack of the thread to be memory mapped. I fail to see how this is "behind your back". You also linked specifically to the POSIX threads implementation of thread spawning, which is by definition supposed to play nicely with the libc posix threads API, which expects you to use the libc allocator in combination with POSIX threads API, so that's what it does. You might as well accuse the mmap() function in the zig standard library of allocating behind your back.
- MindSpunk 3y agoThe behind your back part is probably referring to the Args payload bouncing through a heap allocation. It isn't explicit on the signature it's making an allocation. The function has no choice though unless you leave it up to the user to keep the payload allocation live until the thread terminates.
- alchemio 3y agoExactly
- alchemio 3y agoIt’s something Zig touts when compared to other languages(1). The idea is that in the end it’s a convention that an allocator needs to be passed to indicate that the function allocates, which not even the stdlib adheres religiously to. I’m fine with it since I do believe a library writer should know best what works with their library. 1. https://ziglang.org/learn/why_zig_rust_d_cpp/#no-hidden-allocations https://ziglang.org/learn/why_zig_rust_d_cpp/#no-hidden-allo...
- fsckboy 3y agohe was pointing out that it does, and he was not applying to it the label "behind your back, that's why he said "except for". the wording makes perfect sense.
- rootlocus 3y ago> Similarly zig’s stdlib shouldn’t allocate behind your back, except for thread spawn where it does shouldn't X, exceptor for Y where it does [X] It makes perfect sense that GP was saying "thread spawn allocates behind your back".
- jcelerier 3y ago> strdup does but it was only recently adopted into the standard, it was previously an extension. Does it really matter when people were already writing code with strdup when the zig and rust creators were in middle school ? It was already there in BSD 4.3 (1986) apparently.