News for hackers. Stuff that matters.
Re:How I Wrote a Modern C++ Library in Rust by cryptonector on Monday December 03, 2018 @11:17AM (#18589902)
C ABI to the rescue!

That may be the one thing left of C in 50 years' time.

Re:How I Wrote a Modern C++ Library in Rust by MaxBarraclough on Monday December 03, 2018 @11:49AM (#18590211)
A dead lingua franca, Like Latin. Could happen, but I suspect embedded programmers will stick with C forever.
Re:How I Wrote a Modern C++ Library in Rust by kccqzy on Monday December 03, 2018 @12:05PM (#18590360)
This is one reason I don't really like embedded programming. There seems to be generally a fear of newer systems languages like Rust and a fear of newer features in C++. An embedded programmer told me he doesn't use the C++ STL because he doesn't want functions allocating stuff on his back. Doesn't seem very convincing as you can always redefine the global operator new.
Re:How I Wrote a Modern C++ Library in Rust by rcxdude on Monday December 03, 2018 @12:44PM (#18590890)
There is a fair amount of interest in Rust from embedded programmers, though at the moment you can only really use it for ARM cores (which are probably among the most common embedded cores but there are a huge number of alternatives). There is a dedicated working group within the rust community for embedded which is pushing towards having a good user experience for those wanting to use Rust in such an environment.

But even within embedded rust you won't find much appetite for dynamic memory allocation, and a fair amount of the work has been stabilizing the mechanisms by which you can build rust code which does not use its standard library ('no_std'). This has nothing to do with the fear of the new and lots to do with predictability of code. Using a single heap for all allocations is not at all ideal in an embedded context: your allocations become much harder to predict (both in terms of failure and in terms of time taken), errors become harder to recover from, and it becomes harder to reason about the amount of memory your system will use (especially in edge cases). For embedded work you will generally try very hard to allocate everything statically, and use some kind of pool allocation if you cannot (and often you combine the allocation and whatever datastructure you are placing the objects in).

Re:How I Wrote a Modern C++ Library in Rust by josephg on Monday December 03, 2018 @11:35PM (#18596300)
> Using a single heap for all allocations is not at all ideal in an embedded context

The same is true in game development. Using a per-frame arena allocator for short lived objects can make a big difference to performance. And Rust’s lifetime rules can be used to make this sort of thing completely safe.

It’s a shame that rust std completely relies on the hidden global allocator instead of accepting an allocator as a parameter. It means that while you can write your arena allocator, you can’t use it with any of the built in Box/Vec/HashSet/etc types.

I actually really like Zig’s answer here of just passing in an allocator as an argument in all data structures. I’d love to see that in rust!

Re:How I Wrote a Modern C++ Library in Rust by steveklabnik on Monday December 03, 2018 @11:43PM (#18596349)
We didn’t have an allocator API, or we would have liked to.

Global allocators have finally stabilized. Working on it!

Re:How I Wrote a Modern C++ Library in Rust by kstenerud on Monday December 03, 2018 @12:26PM (#18590624)
It's not a problem of how things are allocated, but rather when. In an embedded environment, you need to be very aware of what is being used at any given time and how, and complex templates like in STL have a tendency to allocate and deallocate in ways that can be difficult to follow, or might have a worst case memory footprint that blows through your RAM.
Re:How I Wrote a Modern C++ Library in Rust by foundry27 on Monday December 03, 2018 @01:26PM (#18591403)
Not disagreeing with your point about STL classes having a tendency to make egregious allocations (heck, a lot of them are fundamentally designed around using allocators for pretty much everything), but the fact that they’re often complicated templates is totally orthogonal to that, and I think it’s disingenuous to say that there’s a causal relationship between the two. If anything, templating encourages statically-verifiable, compiler-enforced code invariants and outputs, which can be pretty darn nice for embedded.
Re:How I Wrote a Modern C++ Library in Rust by rkangel on Monday December 03, 2018 @02:06PM (#18591817)
> There seems to be generally a fear of newer systems languages like Rust

It's not fear of newer languages (most of the time). I would love to use Rust for embedded in a professional environment but it loses out quickly based on supported platforms.

As leader of an embedded development team, I can't choose a language that will limit the processor parts we can use when we get closer to manufacturing and cost reduction. At volume, many millions spent on software development is dwarfed by cost savings from swapping in the right part.

Re:How I Wrote a Modern C++ Library in Rust by oblio on Tuesday December 04, 2018 @04:28AM (#18597818)
I've always been curious about this, could you give more details? It would be interesting to know what a 8-bit chip costs vs a 32-bit one, for example.
Re:How I Wrote a Modern C++ Library in Rust by rkangel on Tuesday December 04, 2018 @06:19AM (#18598378)
8 Bit chips tend be anywhere from 1 cent to $3 in volume. 32 bit chips are usually be from $1 to $20.

It doesn't tend to be that extreme a part swap - if you're doing signal processing and UI then you're probably always going to use a 32 bit part. If you're just doing some basic logic with a couple of digital inputs then you're probably always going to use an 8 bit part. You might move 32 bit ARM to 16 bit MSP430 to hit a power budget or similar.

Leaving aside processor architecture, the factors that affect cost from most to least (approx) are: Flash size, number of peripherals/features, clock speed, ram size. Basically, sometimes another manufacturer will have a part that meets your feature requirements much cheaper, and then you port the code. The rest of the time, you're just squeezing program space, memory usage and processor usage to reduce the required specs.

Re:How I Wrote a Modern C++ Library in Rust by shaklee3 on Monday December 03, 2018 @08:01PM (#18594859)
From what I recall, SpaceX doesn't allow any external libraries in their embedded rocket code.
Re:How I Wrote a Modern C++ Library in Rust by pjmlp on Monday December 03, 2018 @01:32PM (#18591466)
Yeah, most of them are still on C89, even if the compilers do C11.
Re:How I Wrote a Modern C++ Library in Rust by MaxBarraclough on Tuesday December 04, 2018 @08:20PM (#18605019)
Real C programmers use C89, huh.
Re:How I Wrote a Modern C++ Library in Rust by Cyph0n on Monday December 03, 2018 @02:51PM (#18592262)
As someone who works on a large OS consisting of ~200m lines of C: I really don't think so.
Re:How I Wrote a Modern C++ Library in Rust by pjmlp on Monday December 03, 2018 @01:30PM (#18591448)
On modern OSes C ABI is largely irrelevant.

ChromeOS, Android, Windows/UWP, Fuchsia, ...

C ABI is a actually the OS ABI, when they happen to be written in C and expose syscalls in C.

As for C in 50 years, it depends how much we keep on using UNIX.

Re:How I Wrote a Modern C++ Library in Rust by int_19h on Monday December 03, 2018 @06:03PM (#18594056)
And how many of those are defined in terms of the underlying C ABI?

For example, WinRT - used by Windows/UWP - can be expressed entirely in terms of C structs and function pointers.

Re:How I Wrote a Modern C++ Library in Rust by pjmlp on Tuesday December 04, 2018 @02:03AM (#18597008)
By exploiting how Windows C++ compilers layout VMT tables.

Yet this is much less relevant in UWP, which allows for richer type system.

Additionally the COM/UWP ABI requires type libraries for properly accessing the objects and marshaling.

Re:How I Wrote a Modern C++ Library in Rust by int_19h on Tuesday December 04, 2018 @03:37AM (#18597536)
COM is a C ABI first and foremost - somewhere in MSDN, there's actually a page that exactly specifies vtable layout. It's not really surprising that it's C++-compatible, given that it's the most obvious way to do a vtable when you have just a single base class. But yes, of course, Win32 C++ implementations would maintain that compatibility - you could consider it the C++ bindings to COM, in a sense.

And yes, WinRT does require its metadata for marshaling (COM doesn't - you can compile proxies/stubs from IDL). It does not require that for accessing objects, and there are many WinRT components which, in fact, cannot be marshaled at all. But all that doesn't make it any less of a C ABI - the metadata format is documented, and the standard WinRT APIs used to access it are themselves WinRT-compliant, and therefore the whole thing can be consumed from C, if painfully.

Re:How I Wrote a Modern C++ Library in Rust by pjmlp on Tuesday December 04, 2018 @04:56AM (#18597978)
That is like telling anything can be consumed from Assembly, thus it has an Assembly ABI, even if painful.
Re:How I Wrote a Modern C++ Library in Rust by int_19h on Tuesday December 04, 2018 @04:50PM (#18603891)
Any ABI is assembly ABI, by definition, since you are not constrained at all in assembly.

Similarly, the definition of a C ABI is "ABI that can be expressed in C terms" (not "ABI that is easy to code against in C") - i.e. an ABI that any language with C FFI can use. That last part is what makes this categorization useful.

And it has some significant implications beyond "assembly ABI" - for example, C doesn't have guaranteed optimized tailcalls, and therefore any such ABI can't provide such a guarantee, either. Or - C doesn't have exceptions, and so COM and WinRT have to use error signalling mechanisms that are expressible in C (HRESULTs and thread-local error description objects). Or take async - WinRT offers CPS-style futures via IAsyncResult, and because that's just a WinRT interface, any language that can do C FFI can do WinRT async (unlike fibers, goroutines etc, which require intrinsic support).

Re:How I Wrote a Modern C++ Library in Rust by usrusr on Monday December 03, 2018 @02:18PM (#18591943)
The parts of Android that don't use the C ABI are not an OS though.
Re:How I Wrote a Modern C++ Library in Rust by pjmlp on Monday December 03, 2018 @02:22PM (#18591993)
Try to use that C ABI and see how far you go writing an Android application, an user space driver in Android Things, or a Treble driver.