Live data from Hacker News

Writing C software without the standard library

weeb.ddns.net

111–120 of 188 posts

Re: Writing C software without the standard library

#111

The comment section where gcc puts in ident info can be omitted with -fno-ident and syscall(2) is usually a very thin wrapper[0]. If you follow the musl syscall(2) it simply maps errors to errno[1] and uses the fancy count-args-in-macro[2] to call off the respective $arch/syscall_arch.h[3] syscall$n numbered functions. [0] https://git.musl-libc.org/cgit/musl/tree/src/misc/syscall.c [1] https://git.musl-libc.org/cgit/…

Why does syscall use varags then assume that there will be 7 arguments?

Re: Writing C software without the standard library

#112
post #59

It's interesting to read the sources[1] of lots of djb's[2] code, as he often works around problems with (or perhaps dislikes the style of) standard libraries by re-implementing parts. [1] https://github.com/abh/djbdns/blob/master/str_len.c [2] http://cr.yp.to/djb.html

On a side note, isn't the choice of exactly four unrolls very architecture specific? As in, it works, but may be sub-optimal for your specific machine. I've done the exact same thing myself, and IIRC its performance varied a lot between which ISA it was compiled for.

This is almost what Duff's device solves, except then you need to know the length beforehand.

Re: Writing C software without the standard library

#113

The guy is definitely a fan of old-school minimalism: http://weeb.ddns.net/0/articles/modern_software_is_at_its_wo... I have to say I miss the old days of Gopher, too. It was so much easier to focus on the content back then.

I find myself agreeing with him 100%. Not just on Gopher (although i did write a Gopher client some time ago - http://runtimeterror.com/tools/gopher/ ) but on the entire rant about wasting resources, UIs that waste screen real estate and become unusable in smaller resolutions, fonts that only look good with anti-aliasing and have weird misplaced pixels with antialiased disabled (which i also do), websites that make r…

> The only bit i'd disagree would be with games, at least on AAA games since i have some experience there and -at least at the engine level- there is still a lot of low level wizardry being done there.

He explicitly calls out people "writing poorly performing code on top of pre-made engines". I'm pretty sure he's aiming most of his ire at non-indies using Unity3D or Unreal.

Re: Writing C software without the standard library

#114

Earlier quoted context omitted.

I find myself agreeing with him 100%. Not just on Gopher (although i did write a Gopher client some time ago - http://runtimeterror.com/tools/gopher/ ) but on the entire rant about wasting resources, UIs that waste screen real estate and become unusable in smaller resolutions, fonts that only look good with anti-aliasing and have weird misplaced pixels with antialiased disabled (which i also do), websites that make r…

> wasting resources I don't consider variable-width fonts, nice margins and dynamic word wrapping to be a waste of resources. There's a sane middle ground between Electron and the 70s.

That ground is mostly unexplored. IMHO, the sane middle ground between Electron and the 70s isn't shoving an entire browser inside a desktop app. It's using modern programming and compiler techniques to get close to the metal without having to deal with all of the shortcomings of C/C++. It's also using UI libraries that make creating desktop UIs as easy as making a website, but with better performance.

Sadly, neither of the things I mentioned above exist yet. People are working on languages that fit that space, but most of them aren't done yet. Nim is the only one that's officially released.

As for desktop UI libraries, your choice these days seem to be between bad and worse. I'm working on my own desktop UI library, but I don't know what the hell I'm doing. It'll be a learning experience, if nothing else.

Re: Writing C software without the standard library

#115
post #94
post #85

Earlier quoted context omitted.

Yes, that was partly my point. You can get reduced executable size and a lack of dependencies on various msvc*.dlls, without giving up much of the functionality of the C standard library and without having to write it all yourself.

Fine, but you get this thanks to already being linked with these megagodzillas like kernel32.dll. Try without them.

On windows nt, ntdll gets loaded in all processes, it's hardcoded in the kernel (http://gate.upm.ro/os/LABs/Windows_OS_Internals_Curriculum_R...). That's where the loader resides, so you can't go without it. And that's where all the Zw/Nt* functions/ i.e. where the syscalls are.

So you can't really go without it.

Re: Writing C software without the standard library

#116
post #11

Earlier quoted context omitted.

Or demo coding on old hardware. I sometimes write demos for the Atari ST (68000-based home computer launched in 1985), and the modern way of doing that is to develop on a modern computer, and cross-compile to a native ST executable. The main loops are all assembler, but the support code is in C, but the C code is used as a more expressive assembler, and linking with libc requires way too much memory. All this means t…

I'm curious what the use case of memcpy is in highly-optimised software. Are there any scenarios where copying bytes is better than using a char*+length tuple?

Are you seriously asking if there are valid uses of memcpy? Serialization, concurrency, io to name some very specific uses.

Re: Writing C software without the standard library

#118

A value in the range between -4095 and -1 indicates an error, it is -errno. The syscall/errno stuff has always seemed unusual, inelegant, and inefficient --- instead of just returning a negative error code directly, the function returns the vague "an error has occurred" -1, and you have to then check errno separately after that. It only adds insult to injury when you realise that the kernel itself isn't doing it, but…

> At least on Linux the first few (i.e. the oldest, most common and useful) syscalls have not really moved around over the years IIRC raw syscalls are an officially supported kernel API, that's why you can have alternate libc implementations (e.g. musl), and Linux is an oddity in that, on most systems even if the syscalls are fairly stable there are no actual guarantees with respect to them, and the only officially s…

     IIRC raw syscalls are an officially supported kernel API
The term you are looking for is We don't break userland. Once a systemcall goes live it is engraved in stone. This is why if you look though the syscall table you see call, call2, call_ext.

Re: Writing C software without the standard library

#119

> xor rbp,rbp /* xoring a value with itself = 0 */ Is this faster than a (const) mov ?

Apart from the replies to your question here, there was another interesting sub-thread on HN on this topic recently:

"Also, I like how returning 0 is "xor eax, eax"."

https://news.ycombinator.com/item?id=13052503

That led to multiple replies on it.

Post reply on HN