Skip to content

Releases: JacksonAllan/Verstable

Extended custom allocator support, destructor bug fix

06 Feb 14:28
Compare
Choose a tag to compare

Version 2.0.0 improves support for custom memory allocators via the introduction of an optional ctx (context) member in the hash table struct.

To enable and specify the type of the ctx member, define CTX_TY before including verstable.h.

When the ctx member is enabled, the custom allocation and free functions supplied via MALLOC_FN and FREE_FN should each take an extra argument, namely a pointer to the table's ctx member: void *( size_t size, CTX_TY *ctx ) for MALLOC_FN and void ( void *ptr, size_t size, CTX_TY *ctx ) and for FREE_FN.

Likewise, init and init_clone should each take an extra argument that sets the ctx member on the newly initialized table struct.

Here is how enabling and using the ctx member might look in practice:

// Example context data as a struct.
typedef struct
{
  // Context data...
} context;

void *malloc_with_ctx( size_t size, context *ctx )
{
  // Allocate size bytes, taking into account ctx, and return a pointer to the allocated memory or NULL in the case of
  // failure...
}

void free_with_ctx( void *ptr, size_t size, context *ctx )
{
  // Free size bytes, taking into account ctx...
}

#define NAME int_int_map
#define KEY_TY int
#define VAL_TY int
#define CTX_TY context
#include "verstable.h"

int main( void )
{
  context our_ctx;

  // Initialize our_ctx...

  int_int_map our_map;
  vt_init( &our_map, our_ctx );

  // Do things with our_map...

  vt_cleanup( &our_map );
}

This version also introduces various optimizations affecting insertions and lookups and fixes a bug that caused a key to be used after destruction during erasure.

Initial release

12 Dec 14:54
cec58e7
Compare
Choose a tag to compare

This initial release includes the header, two sets of tests, and preliminary benchmarks against several high-performance C++ hash tables.