Skip to main content
gamedev.net
🔒 Locked

Different memory allocator

Started by zaerl Apr 13, 2007 at 4:31 AM 0 replies 950+ views
Original Post
zaerl
zaerl
HI there I've build up a little library (written in plain C) that handle my internal archive file format (something like Blizzard MPQ but far more complex). Now it's all finished and I would like to share my work with all programmers out there. I have added all the features I need (cryptography, multiple hash, internationalization, editor for win/mac/lin and others) but then I'd want to let the user to choose a custom memory allocator in order to easily integrate the library in external projects. The question is: is that a good thing? I mean, if the library is compiled as a static one I know well that there are no problems. But what happens if a library is a dynamic one? Using an external function (with external I mean from another module) could that strategy slow down everything? I have not found any document that explain that in a clear way. Today my library can be compiled over gcc 3.3/4 on Mac and visual studio 2003/2005/Intel 8 on win32. Thanks in advance.
--"Low level programming is good for the programmer's soul" -- John Carmack
ldeej
ldeej
You want to provide allocation callback functions to allow others manage the memory that your library uses.

You can take a look at how zlib does it, it is usually done with function pointers, and that is probably the best choice if you want to strive for maximum portability and flexibility.

In terms of why you need this, there are two main reasons:
1. Users of your library might have specific memory requirements (i.e. specific memory manager, specific memory pool, etc) and want to manage the memory that your library uses.
2. Memory allocations and deallocations across dll boundaries have several problems, if your library returns a pointer and you expect that the client code releases the pointer then you end up having problems because you have two memory managers running and one is not aware of how the other one is tracking memory, things will blow up.

Hope this helps

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.