Libgit2 khash extensions #7

Merged
merged 5 commits into from Sep 5, 2012

Projects

None yet

2 participants

@arrbee

Hey there!

For https://github.com/libgit2/libgit2 we replaced our hashtable implementation with khash a few months ago and it has been great for us! Thank you! 😍

We had to make a few changes to allow us to embed khash into libgit2 and I thought they might be useful to push back upstream. I've split this into 5 separate commits, but there are really 3 main things we were trying to achieve:

  1. make khash easier to embed with separate macros for typedefs, function prototypes, memory allocation and inlining
  2. add allocation failure checking (which changes the return value of kh_resize_ ## name)
  3. add a couple of foreach macros for iterating over the keys and values stored in the hash

You are welcome to take all, some, or none of this, as you like. If you'd prefer me to split this into separate pull requests or change the way it's written or anything else, just let me know, but I thought it might be easier to show everything in one place at first.

arrbee added some commits Aug 31, 2012
@arrbee arrbee Reorganize khash decl macros to separate typedef
This separates the khash declaration macros into three pieces: one
to declare the type, one for the function prototypes, and one for
the function declarations.  This makes it easier to instantiate a
khash type one time and have a header with just the typedef and
prototypes.

The old macros are redefined in terms of the new ones.
cea612f
@arrbee arrbee Add allocation wrappers and error checking
When embedding khash in another project, it is convenient to be
able to replace the use of malloc/calloc/free with alternatives.
This wraps those calls with kmalloc/kcalloc/kfree, etc. macros
that can be easily redefined by the khash user to override the
allocators.

Additionally, the return value from allocations was not being
checked.  This adds a check whenever an allocation is performed
and a return code of -1 to indicate an allocation failure.
6a7e6f9
@arrbee arrbee Add khash wrapper around inline
As with allocation functions, when embedding khash inside another
library, the use of the non-portable "inline" keyword can be tricky.
Instead of adding a #define of inline, this adds "kh_inline" which
can be defined as needed inside khash with better isolation.
bfb7a74
@arrbee arrbee Add type casts to avoid warnings
On some build platforms, the assignments in the hash string fn
were generating warning messages.  This clears those up.
24c0fe5
@arrbee arrbee Add khash foreach macros
This adds two convenient macros for iterating over all the keys
or all the values in a khash.
e65db95
@lh3 lh3 merged commit a98af46 into attractivechaos:master Sep 5, 2012
@lh3
Collaborator
lh3 commented Sep 5, 2012

Thanks a lot. These are all nice additions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment