1 /*-------------------------------------------------------------------------
4 * Concurrent hash tables backed by dynamic shared memory areas.
6 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/lib/dshash.h
12 *-------------------------------------------------------------------------
17 #include "utils/dsa.h"
19 /* The opaque type representing a hash table. */
21 typedef struct dshash_table dshash_table
;
23 /* A handle for a dshash_table which can be shared with other processes. */
24 typedef dsa_pointer dshash_table_handle
;
26 /* The type for hash values. */
27 typedef uint32 dshash_hash
;
29 /* A function type for comparing keys. */
30 typedef int (*dshash_compare_function
) (const void *a
, const void *b
,
31 size_t size
, void *arg
);
33 /* A function type for computing hash values for keys. */
34 typedef dshash_hash (*dshash_hash_function
) (const void *v
, size_t size
,
38 * The set of parameters needed to create or attach to a hash table. The
39 * members tranche_id and tranche_name do not need to be initialized when
40 * attaching to an existing hash table.
42 * Compare and hash functions must be supplied even when attaching, because we
43 * can't safely share function pointers between backends in general. Either
44 * the arg variants or the non-arg variants should be supplied; the other
45 * function pointers should be NULL. If the arg variants are supplied then the
46 * user data pointer supplied to the create and attach functions will be
47 * passed to the hash and compare functions.
49 typedef struct dshash_parameters
51 size_t key_size
; /* Size of the key (initial bytes of entry) */
52 size_t entry_size
; /* Total size of entry */
53 dshash_compare_function compare_function
; /* Compare function */
54 dshash_hash_function hash_function
; /* Hash function */
55 int tranche_id
; /* The tranche ID to use for locks */
58 /* Forward declaration of private types for use only by dshash.c. */
59 struct dshash_table_item
;
60 typedef struct dshash_table_item dshash_table_item
;
62 /* Creating, sharing and destroying from hash tables. */
63 extern dshash_table
*dshash_create(dsa_area
*area
,
64 const dshash_parameters
*params
,
66 extern dshash_table
*dshash_attach(dsa_area
*area
,
67 const dshash_parameters
*params
,
68 dshash_table_handle handle
,
70 extern void dshash_detach(dshash_table
*hash_table
);
71 extern dshash_table_handle
dshash_get_hash_table_handle(dshash_table
*hash_table
);
72 extern void dshash_destroy(dshash_table
*hash_table
);
74 /* Finding, creating, deleting entries. */
75 extern void *dshash_find(dshash_table
*hash_table
,
76 const void *key
, bool exclusive
);
77 extern void *dshash_find_or_insert(dshash_table
*hash_table
,
78 const void *key
, bool *found
);
79 extern bool dshash_delete_key(dshash_table
*hash_table
, const void *key
);
80 extern void dshash_delete_entry(dshash_table
*hash_table
, void *entry
);
81 extern void dshash_release_lock(dshash_table
*hash_table
, void *entry
);
83 /* Convenience hash and compare functions wrapping memcmp and tag_hash. */
84 extern int dshash_memcmp(const void *a
, const void *b
, size_t size
, void *arg
);
85 extern dshash_hash
dshash_memhash(const void *v
, size_t size
, void *arg
);
87 /* Debugging support. */
88 extern void dshash_dump(dshash_table
*hash_table
);