5 ms·
Writing a C library, part 1: best practices for writing C libraries
- forgotusername 15y agoRusty Russell had a slightly tongue-in-cheek rant about such things recently, http://rusty.ozlabs.org/?p=140 http://rusty.ozlabs.org/?p=140 There's a bunch more recent articles on his blog covering similar topics
- smcl 15y agoWow, link overload in that post. Did he really feel the need to link to the US Government wikipedia page?
- gue5t 15y agoI didn't even notice that there were a large number of links in the post. I think the majority of them would be useful for someone unfamiliar with some of the topics described, though. The great thing about hypertext is that links are optional, so an abundance, as long as the linked content is of quality, hurts no one.
- spacemanaki 15y agoI disagree, if you link to something there's an implied recommendation, and also a possibility for subtle commentary. (For example, link to something critical of the anchor text.) When I see a link and I'm curious what's being linked, I often hover to see where it's going. If it turns out it's a link to Wikipedia for a mundane, off-topic article like the US Gov't, I think that kind of wastes brain cycles, and I'm a bit disappointed. I know this is a small, hair splitting issue, but I think it's valid. Some writers try to cut words where they can to make their writing concise and tight. I think this falls in the same category. The worst case I've seen is in one of the earlier Clojure (print) books (not Joy of...) that actually had a few footnotes with Wikipedia URLs in them, for CS topics.
- shaggyfrog 15y agoIn the section on memory management, I was surprised that there was no mention of providing a malloc/free method (or some other memory allocator) to the library to explicitly use. This is especially handy if you're working with limited memory environments prone to fragmentation, where you might want the library to call your custom allocator which is managing small memory pools.
- tptacek 15y agoWhat's a well-regarded C library of medium complexity (something that isn't going to be the entire kernel of an application) that does that? I can't think of an example off the top of my head.
- endgame 15y agoLua: http://www.lua.org/manual/5.1/manual.html#3.7 http://www.lua.org/manual/5.1/manual.html#3.7
- shaggyfrog 15y agoI would say it's not a common pattern, if only because most libraries aren't written with embedded systems with only a few megabytes (or even kilobytes) of available memory. I've at least seen it firsthand in C++ systems written for game consoles (PS2) from my time in that industry.
- sg2342 15y agoI would require this in every C library that provides a crypto framework or provides support functionality for such a framework. And to extend this: i would require this in every c library that touches cleartext (in an cryptographic application).
- tptacek 15y agoWhat popular encryption library does this? Also: what would you do specially in your allocator? It seems wrong to me that the users of a library should be expected to know (for instance) the precise incantation required to clear cache footprints on Core2 processors; that seems like an internal library concern.
- sigil 15y agoOr, instead of making C a more pleasant language for developing applications and middleware libraries, you could use a programming language better suited to those tasks. Call out to C only when necessary and at the lowest layers. Don't layer C on C. Even C++ is an order of magnitude more pleasant when it comes to serious application development and building large, layered systems. Would I write a C library that gives callers a transitive dependency on D-Bus, or contains an epoll event loop? No. When I write C, it's typically either (1) small djb-style applications that make a bunch of system calls and don't need any data structures beyond C arrays, or (2) libraries of essentially functional code that do performance critical computations. I love C, and it has its place: near the bare metal.
- unshift 15y agoi'd actually much prefer a lot of handy libraries (e.g., boto comes to mind) be written in C, and exported to higher level languages. that way, each language wouldn't have to duplicate every other language's effort. the end result would probably be more unified APIs and more complete libraries.
- burgerbrain 15y agoExactly. Imagine how much harder life would be if openssl were written in Perl instead of C. It might be a self fulfilling prophecy (C is good for that sort of thing because C is popular, largely in part because C is good for that sort of thing,....) but that's just how real life works sometimes.
- burgerbrain 15y agoSo because you don't use C commonly, you propose that we avoid making it easier to others to do so and collaborate? This is just language trolling in thin disguise.
- sigil 15y ago> So because you don't use C commonly Where did you get that idea? The author talks about writing C libraries that depend on GTK+, GLib, APR, D-Bus, and the like. There's no way in hell I'd write a C library that depends on that stuff. Above some layer, C is no longer the right tool for the job; you're better off switching to C++ or to a dynamic language. From the author's examples I see no appreciation for this fact.
- malkia 15y agoIf possible provide non-callback versions of your functions. It's very useful when trying to use the "C" api from a different language, and callbacks being very hard to put in. (C -> otherlang -> (callbacks again) C).