libgit2 is a fabulous library for working with git programatically. Earlier in the year during my winter break I was looking around for a project to work on. I decided to take a stab at fixing an issue in libgit2. The issue in question was The pool allocator should support being deactivated for debug. The crux of the issue was that internal to libgit2 there is a pool allocator which is used to speed up allocation of large numbers of tiny objects. However with this pool allocator enabled it was hard to track down memory leaks. To address this the issue requested a compile time flag be added to disable the pool allocator.
The pool allocator works by allocating a single large chunk of memory and then handing out bits of it on request. This cuts down on the number of actual calls to the OS for more memory, and can help speed up allocation intensive activities.
My solution was pretty simple. I added a flag to cmake, and then gated the debug
allocator using ifdef’s. The actual debug allocator was also simple. In order
to keep the existing interface of the pool allocator I kept track of every
allocation using a
git_vector is a structure provided by libgit2
which acts as an expandable list. The actual allocation is passed to libgit2’s
internal malloc wrapper. When a deallocation of the pool is requested the
debug allocator traverses the vector and does a free.
My changes were all together pretty small, they can be viewed on the closed pull request, Add a new build flag to disable the pool allocator. My actual work is a bit less than shown there due to having to move some code around to work within the ifdefs I added. It was a fun little project, and a nice way to give back to a library I use.