function lfds700_hash_a_init_valid_on_current_logical_core

From liblfds.org
Jump to navigation Jump to search

Source Files

└───liblfds700
    ├───inc
    │   └───liblfds700
    │           lfds700_hash_addonly.h
    └───src
        └───llfds700_hash_addonly
                lfds700_hash_addonly_init.c

Enums

enum lfds700_hash_a_existing_key
{
  LFDS700_HASH_A_EXISTING_KEY_OVERWRITE,
  LFDS700_HASH_A_EXISTING_KEY_FAIL
};

Opaque Structures

struct lfds700_btree_au_state;
struct lfds700_hash_a_state;

Prototype

void lfds700_hash_a_init_valid_on_current_logical_core( struct lfds700_hash_a_state *has,
                                                        struct lfds700_btree_au_state *baus_array,
                                                        lfds700_uint_t array_size,
                                                        int (*key_compare_function)(void const *new_key, void const *existing_key),
                                                        void (*key_hash_function)(void const *key, lfds700_uint_t *hash),
                                                        enum lfds700_hash_a_existing_key existing_key,
                                                        void *user_state );

Parameters

struct lfds700_hash_a_state *has

A pointer to a user-allocated LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES aligned struct lfds700_hash_a_state. Stack declared variables will automatically be correctly aligned by the compiler, due to the information in the structure definitions; nothing has to be done. Heap allocated variables however will by no means be correctly aligned and an aligned malloc must be used.

struct lfds700_btree_au_state *baus_array

An array of uninitialized user-allocated LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES aligned struct lfds700_btree_au_states. These are the hash buckets. Stack declared variables will automatically be correctly aligned by the compiler, due to the information in the structure definitions; nothing has to be done. Heap allocated variables however will by no means be correctly aligned and an aligned malloc must be used.

lfds700_uint_t array_size

The number of elements in baus_array.

int (*key_compare_function)(void const *new_key, void const *existing_key)

A callback used by the hash to compare elements.

void (*key_hash_function)(void const *key, lfds700_uint_t *hash)

A callback used by the hash to hash elements.

enum lfds700_hash_a_existing_key existing_key

This argument specifies how the hash should behave when attempting to put a key which already exists.

void *user_state

A pointer to void, supplied by the user, which is returned to the user in various callback functions, permitting the user to pass his own state into those functions. This argument can be NULL.

Notes

As the function name indicates, the initialization work performed on the hash state is only valid on the current logical core. To make this work valid on other logical cores, threads on other cores must call LFDS700_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE.

This function instantiates a hash by initializing the hash state. The caller is responsible for all memory allocation and, after lfds700_hash_a_cleanup is called, for all deallocation.

Example

#include <stdio.h>
#include <string.h>
#include "liblfds700.h"

struct test_data
{
  int long long unsigned
    unique_id;

  char
    payload[64];

  struct lfds700_hash_a_element
    hae;
};

int key_compare_function( void const *new_key, void const *existing_key )
{
  int
    cr = 0;

  int long long unsigned
    *new_key = (int long long unsigned *) new_key,
    *existing_key  = (int long long unsigned *) existing_key;

  if( *new_key > *existing_key )
    cr = 1;

  if( *new_key < *existing_key )
    cr = -1;

  return( cr );
}

void key_hash_function( void const *key, lfds700_pal_uint_t *hash )
{
  int unsigned
    temp_hash = 0;

  LFDS700_HASH_A_32BIT_HASH_FUNCTION( key, sizeof(int long long unsigned), temp_hash );

  *hash = temp_hash;

  return;
}

int main()
{
  struct lfds700_btree_au_state
    baus_array[10];

  struct lfds700_hash_a_state
    has;

  struct lfds700_misc_prng_state
    ps;

  lfds700_misc_library_init_valid_on_current_logical_core();

  lfds700_misc_prng_init( &ps );

  lfds700_hash_a_init_valid_on_current_logical_core( &has, baus_array, 10, key_compare_function, key_hash_function, LFDS700_HASH_A_EXISTING_KEY_FAIL, NULL );

  lfds700_hash_a_cleanup( &has );

  lfds700_misc_library_cleanup();

  return( EXIT_SUCCESS );
}

See Also