Hacker News new | ask | show | jobs
by smj-edison 22 days ago
I'm currently working on my own Zig project (an interpreter) and right now I'm working on designing a C interface for it. Seeing things mentioned like having objects as opaque handles, or enum-keyed getters and setters, made me think about how clunky my current API is, since I leak too many details to callers.

What really caught my attention was this: "So from the start, I took inspiration from C libraries I enjoyed working with and designed the API to have a strong, stable ABI that allows for future additions." What are good examples of this? I feel like I just don't have enough experience with well-designed C libraries to know what to look for.

4 comments

One example that comes to my mind is libopus: https://github.com/xiph/opus

If you have structs that might need to be extended, you can use "versioned structs". One popular technique is to include the struct size as the first field. The struct size is set to `offsetof(mystruct, lastmember) + sizeof(lastmember)` and the receiver of the struct can use this to check if a member exists. The ARA API is a good example: https://github.com/Celemony/ARA_API/blob/65ec5c43b943a48cb54...

If you do version and size as the first two fields, you're basically there to backwards and forwards compatibility. Callers can request the size of the struct, allocate a buffer, and read out the parts they need. They can also check specific versions for what they support and ignore newer versions.

It also helps if you only append to the end of the struct for newer fields.

If you only access members, the version is not necessary. The receiver of the struct checks which members are present and ignores extra members that it does not know about. That's backward and forward compatibility.

The version could be useful if some members change their meaning across versions. But this would mean that you couldn't use a struct made from a future version because you wouldn't know about potentially changes.

Personally, I have only seen version or size, but not both. Do you have an example?

> It also helps if you only append to the end of the struct for newer fields.

This is actually required because changing or reordering existing members is an ABI break. Unless every version has its own dedicated struct. (This is often done in COM like APIs where interfaces are extended by adding a new interface. The client queries the must recent interface it knows about; if not supported, it tries the previous interface, and so on until it succeeds.)

Yeah, I guess you don't always need both.

Microsoft includes both for some structs: https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns...

For variable sized structs that you plan on potentially extending, you do need both: https://learn.microsoft.com/en-us/cpp/build/x64-unwind-infor...

My job requires thinking about things like this (well, more senior and experienced people actually drive the final decisions, but I still need to understand them). Seeing the level of rigor that went into the development of a stable C abi for this is insane, it hits on pretty much all the checklists (and more!) that we have internally.
take a look at hpy, which is explicitly designing a better API for python C extensions. https://hpyproject.org/
Ooh, will take a look at this, since it's directly applicable!
maybe libgit2 and libuv? they are designed from the start to be used by others.