Skip to content

Commit

Permalink
Split hashmap into .c and .h file (#451)
Browse files Browse the repository at this point in the history
This fixes compilation warnings I had with `gcc` 8.5 using `-Wall`. Many
of the static functions in `sundials_hashmap.h` were unused by files
that included the header. After discussion with @balos1 moving the
implementations into a .c file made more sense than adding the `inline`
keyword to all the functions.

---------

Co-authored-by: David Gardner <gardner48@llnl.gov>
Co-authored-by: Cody Balos <balos1@llnl.gov>
  • Loading branch information
3 people authored Mar 29, 2024
1 parent 4989837 commit 6ce388b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 43 deletions.
1 change: 1 addition & 0 deletions src/sundials/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ set(sundials_SOURCES
sundials_dense.c
sundials_direct.c
sundials_errors.c
sundials_hashmap.c
sundials_iterative.c
sundials_linearsolver.c
sundials_logger.c
Expand Down
57 changes: 16 additions & 41 deletions src/sundials/sundials_hashmap.h → src/sundials/sundials_hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,19 @@
* SPDX-License-Identifier: BSD-3-Clause
* SUNDIALS Copyright End
* -----------------------------------------------------------------
* A simple header-only hashmap implementation for char* keys and
* A simple hashmap implementation for char* keys and
* void* values. Uses linear probing to resolve collisions.
* The values can be anything, but will be freed by
* the hash map upon its destruction.
* -----------------------------------------------------------------*/

#ifndef _SUNDIALS_HASHMAP_H
#define _SUNDIALS_HASHMAP_H

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "sundials/sundials_errors.h"
#include "sundials/sundials_types.h"
#include "sundials_hashmap_impl.h"

static const uint64_t HASH_PRIME = 14695981039346656037U;
static const uint64_t HASH_OFFSET_BASIS = 1099511628211U;
Expand All @@ -45,23 +43,6 @@ static uint64_t fnv1a_hash(const char* str)
return hash;
}

typedef struct _SUNHashMapKeyValue* SUNHashMapKeyValue;

struct _SUNHashMapKeyValue
{
const char* key;
void* value;
};

typedef struct _SUNHashMap* SUNHashMap;

struct _SUNHashMap
{
int size; /* current number of entries */
int max_size; /* max number of entries */
SUNHashMapKeyValue* buckets;
};

/*
This function creates a new SUNHashMap object allocated to hold
up to 'max_size' entries.
Expand All @@ -74,14 +55,14 @@ struct _SUNHashMap
**Returns:**
* A SUNErrCode indicating success or a failure
*/
static SUNErrCode SUNHashMap_New(int max_size, SUNHashMap* map)
SUNErrCode SUNHashMap_New(int max_size, SUNHashMap* map)
{
int i;

if (max_size <= 0) { return SUN_ERR_ARG_OUTOFRANGE; }

*map = NULL;
*map = (SUNHashMap)malloc(sizeof(struct _SUNHashMap));
*map = (SUNHashMap)malloc(sizeof(**map));

if (!map) { return SUN_ERR_MALLOC_FAIL; }

Expand All @@ -90,7 +71,7 @@ static SUNErrCode SUNHashMap_New(int max_size, SUNHashMap* map)

(*map)->buckets = NULL;
(*map)->buckets =
(SUNHashMapKeyValue*)malloc(max_size * sizeof(SUNHashMapKeyValue));
(SUNHashMapKeyValue*)malloc(max_size * sizeof(*((*map)->buckets)));

if (!(*map)->buckets)
{
Expand All @@ -115,8 +96,7 @@ static SUNErrCode SUNHashMap_New(int max_size, SUNHashMap* map)
**Returns:**
* A SUNErrCode indicating success or a failure
*/
static SUNErrCode SUNHashMap_Destroy(SUNHashMap* map,
void (*freevalue)(void* ptr))
SUNErrCode SUNHashMap_Destroy(SUNHashMap* map, void (*freevalue)(void* ptr))
{
int i;

Expand Down Expand Up @@ -159,9 +139,8 @@ static SUNErrCode SUNHashMap_Destroy(SUNHashMap* map,
* ``>=0`` -- the index at which the iteration stopped
* ``<-1`` -- an error occurred
*/
static int SUNHashMap_Iterate(SUNHashMap map, int start,
int (*yieldfn)(int, SUNHashMapKeyValue, void*),
void* ctx)
int SUNHashMap_Iterate(SUNHashMap map, int start,
int (*yieldfn)(int, SUNHashMapKeyValue, void*), void* ctx)
{
int i;

Expand Down Expand Up @@ -201,7 +180,7 @@ static int sunHashMapLinearProbeInsert(int idx, SUNHashMapKeyValue kv, void* ctx
* ``-1`` -- an error occurred
* ``-2`` -- the map is full
*/
static int SUNHashMap_Insert(SUNHashMap map, const char* key, void* value)
int SUNHashMap_Insert(SUNHashMap map, const char* key, void* value)
{
int idx;
int retval;
Expand All @@ -224,7 +203,7 @@ static int SUNHashMap_Insert(SUNHashMap map, const char* key, void* value)
}

/* Create the key-value pair */
kvp = (SUNHashMapKeyValue)malloc(sizeof(struct _SUNHashMapKeyValue));
kvp = (SUNHashMapKeyValue)malloc(sizeof(*kvp));
if (kvp == NULL) { return (-1); }

kvp->key = key;
Expand Down Expand Up @@ -264,7 +243,7 @@ static int sunHashMapLinearProbeGet(int idx, SUNHashMapKeyValue kv, void* key)
* ``-1`` -- an error occurred
* ``-2`` -- key not found
*/
static int SUNHashMap_GetValue(SUNHashMap map, const char* key, void** value)
int SUNHashMap_GetValue(SUNHashMap map, const char* key, void** value)
{
int idx;
int retval;
Expand Down Expand Up @@ -308,15 +287,14 @@ static int SUNHashMap_GetValue(SUNHashMap map, const char* key, void** value)
**Returns:**
* A SUNErrCode indicating success or a failure
*/
static SUNErrCode SUNHashMap_Sort(SUNHashMap map, SUNHashMapKeyValue** sorted,
int (*compar)(const void*, const void*))
SUNErrCode SUNHashMap_Sort(SUNHashMap map, SUNHashMapKeyValue** sorted,
int (*compar)(const void*, const void*))
{
int i;

if (!map || !compar) { return SUN_ERR_ARG_CORRUPT; }

*sorted =
(SUNHashMapKeyValue*)malloc(map->max_size * sizeof(SUNHashMapKeyValue));
*sorted = (SUNHashMapKeyValue*)malloc(map->max_size * sizeof(**sorted));
if (!(*sorted)) { return SUN_ERR_MALLOC_FAIL; }

/* Copy the buckets into a new array */
Expand All @@ -339,15 +317,14 @@ static SUNErrCode SUNHashMap_Sort(SUNHashMap map, SUNHashMapKeyValue** sorted,
* A SUNErrCode indicating success or a failure
*/
#if SUNDIALS_MPI_ENABLED
static SUNErrCode SUNHashMap_Values(SUNHashMap map, void*** values,
size_t value_size)
SUNErrCode SUNHashMap_Values(SUNHashMap map, void*** values, size_t value_size)
{
int i;
int count = 0;

if (!map) { return SUN_ERR_ARG_CORRUPT; }

*values = (void**)malloc(map->size * sizeof(value_size));
*values = (void**)malloc(map->size * value_size);
if (!values) { return SUN_ERR_MALLOC_FAIL; }

/* Copy the values into a new array */
Expand All @@ -359,5 +336,3 @@ static SUNErrCode SUNHashMap_Values(SUNHashMap map, void*** values,
return SUN_SUCCESS;
}
#endif

#endif
56 changes: 56 additions & 0 deletions src/sundials/sundials_hashmap_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* -----------------------------------------------------------------
* Programmer: Cody J. Balos @ LLNL
* -----------------------------------------------------------------
* SUNDIALS Copyright Start
* Copyright (c) 2002-2024, Lawrence Livermore National Security
* and Southern Methodist University.
* All rights reserved.
*
* See the top-level LICENSE and NOTICE files for details.
*
* SPDX-License-Identifier: BSD-3-Clause
* SUNDIALS Copyright End
* -----------------------------------------------------------------
* A simple hashmap implementation for char* keys and
* void* values. Uses linear probing to resolve collisions.
* The values can be anything, but will be freed by
* the hash map upon its destruction.
* -----------------------------------------------------------------*/

#ifndef _SUNDIALS_HASHMAP_IMPL_H
#define _SUNDIALS_HASHMAP_IMPL_H

#include <stdlib.h>
#include <sundials/sundials_types.h>

typedef struct SUNHashMapKeyValue_* SUNHashMapKeyValue;

struct SUNHashMapKeyValue_
{
const char* key;
void* value;
};

typedef struct SUNHashMap_* SUNHashMap;

struct SUNHashMap_
{
int size; /* current number of entries */
int max_size; /* max number of entries */
SUNHashMapKeyValue* buckets;
};

SUNErrCode SUNHashMap_New(int max_size, SUNHashMap* map);
SUNErrCode SUNHashMap_Destroy(SUNHashMap* map, void (*freevalue)(void* ptr));
int SUNHashMap_Iterate(SUNHashMap map, int start,
int (*yieldfn)(int, SUNHashMapKeyValue, void*), void* ctx);
int SUNHashMap_Insert(SUNHashMap map, const char* key, void* value);
int SUNHashMap_GetValue(SUNHashMap map, const char* key, void** value);
SUNErrCode SUNHashMap_Sort(SUNHashMap map, SUNHashMapKeyValue** sorted,
int (*compar)(const void*, const void*));

#if SUNDIALS_MPI_ENABLED
SUNErrCode SUNHashMap_Values(SUNHashMap map, void*** values, size_t value_size);
#endif

#endif
2 changes: 1 addition & 1 deletion src/sundials/sundials_logger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <sundials/sundials_logger.h>
#include <sundials/sundials_types.h>

#include "sundials_hashmap.h"
#include "sundials_hashmap_impl.h"
#include "sundials_utils.h"

#define SUNDIALS_LOGGING_ERROR 1
Expand Down
3 changes: 2 additions & 1 deletion src/sundials/sundials_profiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* SUNDIALS Copyright End
* -----------------------------------------------------------------*/

#include <string.h>
#include <sundials/priv/sundials_errors_impl.h>
#include <sundials/sundials_config.h>

Expand Down Expand Up @@ -39,7 +40,7 @@
#include <sundials/sundials_types.h>

#include "sundials_debug.h"
#include "sundials_hashmap.h"
#include "sundials_hashmap_impl.h"

#define SUNDIALS_ROOT_TIMER ((const char*)"From profiler epoch")

Expand Down

0 comments on commit 6ce388b

Please sign in to comment.