1
0
mirror of https://github.com/opencryptoki/opencryptoki.git synced 2026-10-24 05:39:17 +03:00
Files
opencryptoki/usr/lib/api/hashmap.h
Juergen Christ 1026d2a244 API: Add hashmap
Add a hashmap.  Hashmap uses hashing with chaining to store the different
key/value pairs.  The hash uses a power-of-2 size and simply uses the lower
bits of the hash value to place and locate the element.  Since, e.g.,
mechanisms are highly redundant at the lowest bits, the hash can be configured
to either full Jenkins hash or just the Jenkins mix function.  This is a
compile time option.

The values stored in the hash are designed as a union of a CK_ULONG or a
pointer.  This should allow for maximum flexibility of the user.  Memory
management of the value is left to the user.  The hashmap_free() function
takes a pointer to a user-provided free function that is applied to all values
in the hashmap.  The function can be NULL.

All other functions that would remove a value from the map (either
hashmap_delete or hashmap_add which overwrites the old value with the new
value if the key is already in the map) provide the old value to the user.  It
is then up to the user to free any memory associated with this value.

Signed-off-by: Juergen Christ <jchrist@linux.ibm.com>
2021-11-08 16:29:24 +01:00

32 lines
954 B
C

/*
* COPYRIGHT (c) International Business Machines Corp. 2021
*
* This program is provided under the terms of the Common Public License,
* version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
* software constitutes recipient's acceptance of CPL-1.0 terms which can be
* found in the file LICENSE file or at
* https://opensource.org/licenses/cpl1.0.php
*/
#ifndef OCK_HASHMAP_H
#define OCK_HASHMAP_H
#include <pkcs11types.h>
struct hashmap;
union hashmap_value {
CK_ULONG ulVal;
void *pVal;
};
typedef void (*freefunc_t)(union hashmap_value);
struct hashmap *hashmap_new(void);
void hashmap_free(struct hashmap *h, freefunc_t f);
int hashmap_find(struct hashmap *h, CK_ULONG key, union hashmap_value *val);
int hashmap_add(struct hashmap *h, CK_ULONG key, union hashmap_value val,
union hashmap_value *oldval);
int hashmap_delete(struct hashmap *h, CK_ULONG key, union hashmap_value *val);
#endif