aboutsummaryrefslogtreecommitdiff
path: root/src/include/lib/dshash.h
diff options
context:
space:
mode:
authorNathan Bossart <nathan@postgresql.org>2024-02-26 15:47:13 -0600
committerNathan Bossart <nathan@postgresql.org>2024-02-26 15:47:13 -0600
commit42a1de3013eac394c3a170ce728f0280a62187bd (patch)
treec708f6e852903207ee4f0d090e90c02facea08e9 /src/include/lib/dshash.h
parent5fe08c006c826da4a7f5db2a79327477599edbc6 (diff)
downloadpostgresql-42a1de3013eac394c3a170ce728f0280a62187bd.tar.gz
postgresql-42a1de3013eac394c3a170ce728f0280a62187bd.zip
Add helper functions for dshash tables with string keys.
Presently, string keys are not well-supported for dshash tables. The dshash code always copies key_size bytes into new entries' keys, and dshash.h only provides compare and hash functions that forward to memcmp() and tag_hash(), both of which do not stop at the first NUL. This means that callers must pad string keys so that the data beyond the first NUL does not adversely affect the results of copying, comparing, and hashing the keys. To better support string keys in dshash tables, this commit does a couple things: * A new copy_function field is added to the dshash_parameters struct. This function pointer specifies how the key should be copied into new table entries. For example, we only want to copy up to the first NUL byte for string keys. A dshash_memcpy() helper function is provided and used for all existing in-tree dshash tables without string keys. * A set of helper functions for string keys are provided. These helper functions forward to strcmp(), strcpy(), and string_hash(), all of which ignore data beyond the first NUL. This commit also adjusts the DSM registry's dshash table to use the new helper functions for string keys. Reviewed-by: Andy Fan Discussion: https://postgr.es/m/20240119215941.GA1322079%40nathanxps13
Diffstat (limited to 'src/include/lib/dshash.h')
-rw-r--r--src/include/lib/dshash.h19
1 files changed, 18 insertions, 1 deletions
diff --git a/src/include/lib/dshash.h b/src/include/lib/dshash.h
index 91f70ac2b5f..2ff1ba6c24d 100644
--- a/src/include/lib/dshash.h
+++ b/src/include/lib/dshash.h
@@ -37,6 +37,10 @@ typedef int (*dshash_compare_function) (const void *a, const void *b,
typedef dshash_hash (*dshash_hash_function) (const void *v, size_t size,
void *arg);
+/* A function type for copying keys. */
+typedef void (*dshash_copy_function) (void *dest, const void *src, size_t size,
+ void *arg);
+
/*
* The set of parameters needed to create or attach to a hash table. The
* members tranche_id and tranche_name do not need to be initialized when
@@ -55,6 +59,7 @@ typedef struct dshash_parameters
size_t entry_size; /* Total size of entry */
dshash_compare_function compare_function; /* Compare function */
dshash_hash_function hash_function; /* Hash function */
+ dshash_copy_function copy_function; /* Copy function */
int tranche_id; /* The tranche ID to use for locks */
} dshash_parameters;
@@ -105,9 +110,21 @@ extern void *dshash_seq_next(dshash_seq_status *status);
extern void dshash_seq_term(dshash_seq_status *status);
extern void dshash_delete_current(dshash_seq_status *status);
-/* Convenience hash and compare functions wrapping memcmp and tag_hash. */
+/*
+ * Convenience hash, compare, and copy functions wrapping memcmp, tag_hash, and
+ * memcpy.
+ */
extern int dshash_memcmp(const void *a, const void *b, size_t size, void *arg);
extern dshash_hash dshash_memhash(const void *v, size_t size, void *arg);
+extern void dshash_memcpy(void *dest, const void *src, size_t size, void *arg);
+
+/*
+ * Convenience hash, compare, and copy functions wrapping strcmp, string_hash,
+ * and strcpy.
+ */
+extern int dshash_strcmp(const void *a, const void *b, size_t size, void *arg);
+extern dshash_hash dshash_strhash(const void *v, size_t size, void *arg);
+extern void dshash_strcpy(void *dest, const void *src, size_t size, void *arg);
/* Debugging support. */
extern void dshash_dump(dshash_table *hash_table);