5 ms·
In 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
by shaggyfrog 15y ago
In 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.
- sg2342 15y agoon top of my head: libgmp (not a crypto library but used by some), libgcrypt, openssl (requires a dynamic engine to do this), libssh2, libmcrypt ... IMHO the user of an encryption library (the developer of a crypto application) should at least be able to ensure that every bit of heap that was touched by the library is zeroed after it was freed.
- jallmann 15y agolibstrophe (xmpp) allows you to use your own allocator, but it's pretty niche.
- defen 15y agolibpng lets you specify your own malloc/free functions.
- tptacek 15y agoThat's a great example. Thanks.
- cpeterso 15y agozlib and libpng.
- deleted 15y ago[deleted]
- shaggyfrog 15y agoStupid me, I forgot about libxml2. I even asked their mailing list about it 3 years ago: http://mail.gnome.org/archives/xml/2008-June/msg00085.html http://mail.gnome.org/archives/xml/2008-June/msg00085.html
- asher 15y agoExactly. Also, if the library uses a hash function, let the caller override that.