I hit a wall recently with mod-boot. Basically, when I parse a script file, I need to be able to store all the data given. However, I don't know before-hand how many dependencies the script will have, or how many features it provides. I could use a standard array, but that would be inefficient since I would have to allocate like 25 memory slots of which I might only use 3, and if something needs more then 25 slots, well, that's tricky now ain't it? I could use a GArray, which is a handy little feature of GLib, but unless I'm wrong, whenever you exceed your allocated memory, it has to allocate more and copy everything over, which gets inefficient, and if it works like the C++ STL vector template, then I'm still wasting a lot of memory. I could use a linked list, but that's slow to loop through.
Hence the need for blockList. Basically, it's a doubly linked list implementation where each node has an array. When you first create the blockList struct, you give it a number of bytes per array element (as with GArray), and a default number of elements per blockListElement (basically a node; I should rename it). It then keeps track of the number of blocks currently allocated, and the address of the first and last nodes, so that it can get to any given element as quickly as possible. The programmer can of course manually allocate a block with X elements, but that should also be handled transparently when an element is appended. It's currently not yet finished, but it's getting there. And it's fully 100% memory leak free, too!
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment