From d5099efc47d4e6ac60816b5381a5f607ab03f06e Mon Sep 17 00:00:00 2001 From: Michal Schmidt Date: Wed, 13 Aug 2014 01:00:18 +0200 Subject: hashmap: introduce hash_ops to make struct Hashmap smaller It is redundant to store 'hash' and 'compare' function pointers in struct Hashmap separately. The functions always comprise a pair. Store a single pointer to struct hash_ops instead. systemd keeps hundreds of hashmaps, so this saves a little bit of memory. --- src/test/test-hashmap.c | 50 +++++++++++++++++++++++------------------------ src/test/test-install.c | 2 +- src/test/test-prioq.c | 7 ++++++- src/test/test-unit-file.c | 2 +- 4 files changed, 33 insertions(+), 28 deletions(-) (limited to 'src/test') diff --git a/src/test/test-hashmap.c b/src/test/test-hashmap.c index ccef61f55c..95a7f8379d 100644 --- a/src/test/test-hashmap.c +++ b/src/test/test-hashmap.c @@ -26,7 +26,7 @@ static void test_hashmap_replace(void) { Hashmap *m; char *val1, *val2, *val3, *val4, *val5, *r; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); val1 = strdup("val1"); assert_se(val1); @@ -73,7 +73,7 @@ static void test_hashmap_copy(void) { val4 = strdup("val4"); assert_se(val4); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); hashmap_put(m, "key 1", val1); hashmap_put(m, "key 2", val2); @@ -109,7 +109,7 @@ static void test_hashmap_get_strv(void) { val4 = strdup("val4"); assert_se(val4); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); hashmap_put(m, "key 1", val1); hashmap_put(m, "key 2", val2); @@ -141,8 +141,8 @@ static void test_hashmap_move_one(void) { val4 = strdup("val4"); assert_se(val4); - m = hashmap_new(string_hash_func, string_compare_func); - n = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); + n = hashmap_new(&string_hash_ops); hashmap_put(m, "key 1", val1); hashmap_put(m, "key 2", val2); @@ -168,7 +168,7 @@ static void test_hashmap_next(void) { Hashmap *m; char *val1, *val2, *val3, *val4, *r; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); val1 = strdup("val1"); assert_se(val1); val2 = strdup("val2"); @@ -199,7 +199,7 @@ static void test_hashmap_update(void) { Hashmap *m; char *val1, *val2, *r; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); val1 = strdup("old_value"); assert_se(val1); val2 = strdup("new_value"); @@ -222,7 +222,7 @@ static void test_hashmap_put(void) { Hashmap *m; int valid_hashmap_put; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); valid_hashmap_put = hashmap_put(m, "key 1", (void*) (const char *) "val 1"); assert_se(valid_hashmap_put == 1); @@ -236,7 +236,7 @@ static void test_hashmap_remove_and_put(void) { int valid; char *r; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); assert_se(m); valid = hashmap_remove_and_put(m, "unvalid key", "new key", NULL); @@ -261,9 +261,9 @@ static void test_hashmap_ensure_allocated(void) { Hashmap *m; int valid_hashmap; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); - valid_hashmap = hashmap_ensure_allocated(&m, string_hash_func, string_compare_func); + valid_hashmap = hashmap_ensure_allocated(&m, &string_hash_ops); assert_se(valid_hashmap == 0); assert_se(m); @@ -282,7 +282,7 @@ static void test_hashmap_foreach_key(void) { "key 3\0" "key 4\0"; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); NULSTR_FOREACH(key, key_table) hashmap_put(m, key, (void*) (const char*) "my dummy val"); @@ -319,7 +319,7 @@ static void test_hashmap_foreach(void) { val4 = strdup("my val4"); assert_se(val4); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); hashmap_put(m, "Key 1", val1); hashmap_put(m, "Key 2", val2); @@ -358,7 +358,7 @@ static void test_hashmap_foreach_backwards(void) { val4 = strdup("my val4"); assert_se(val4); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); hashmap_put(m, "Key 1", val1); hashmap_put(m, "Key 2", val2); hashmap_put(m, "Key 3", val3); @@ -395,8 +395,8 @@ static void test_hashmap_merge(void) { val4 = strdup("my val4"); assert_se(val4); - n = hashmap_new(string_hash_func, string_compare_func); - m = hashmap_new(string_hash_func, string_compare_func); + n = hashmap_new(&string_hash_ops); + m = hashmap_new(&string_hash_ops); hashmap_put(m, "Key 1", val1); hashmap_put(m, "Key 2", val2); @@ -422,7 +422,7 @@ static void test_hashmap_contains(void) { val1 = strdup("my val"); assert_se(val1); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); assert_se(!hashmap_contains(m, "Key 1")); hashmap_put(m, "Key 1", val1); @@ -439,7 +439,7 @@ static void test_hashmap_isempty(void) { val1 = strdup("my val"); assert_se(val1); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); assert_se(hashmap_isempty(m)); hashmap_put(m, "Key 1", val1); @@ -462,7 +462,7 @@ static void test_hashmap_size(void) { val4 = strdup("my val"); assert_se(val4); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); hashmap_put(m, "Key 1", val1); hashmap_put(m, "Key 2", val2); @@ -482,7 +482,7 @@ static void test_hashmap_get(void) { val = strdup("my val"); assert_se(val); - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); hashmap_put(m, "Key 1", val); @@ -499,7 +499,7 @@ static void test_hashmap_many(void) { #define N_ENTRIES 100000 - assert_se(h = hashmap_new(NULL, NULL)); + assert_se(h = hashmap_new(NULL)); for (i = 1; i < N_ENTRIES*3; i+=3) { assert_se(hashmap_put(h, UINT_TO_PTR(i), UINT_TO_PTR(i)) >= 0); @@ -520,7 +520,7 @@ static void test_hashmap_many(void) { static void test_hashmap_first_key(void) { _cleanup_hashmap_free_ Hashmap *m = NULL; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); assert_se(m); assert_se(!hashmap_first_key(m)); @@ -535,7 +535,7 @@ static void test_hashmap_first_key(void) { static void test_hashmap_last(void) { _cleanup_hashmap_free_ Hashmap *m = NULL; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); assert_se(m); assert_se(!hashmap_last(m)); @@ -550,7 +550,7 @@ static void test_hashmap_last(void) { static void test_hashmap_steal_first_key(void) { _cleanup_hashmap_free_ Hashmap *m = NULL; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); assert_se(m); assert_se(!hashmap_steal_first_key(m)); @@ -563,7 +563,7 @@ static void test_hashmap_steal_first_key(void) { static void test_hashmap_clear_free_free(void) { _cleanup_hashmap_free_ Hashmap *m = NULL; - m = hashmap_new(string_hash_func, string_compare_func); + m = hashmap_new(&string_hash_ops); assert_se(m); assert_se(hashmap_put(m, strdup("key 1"), NULL) == 1); diff --git a/src/test/test-install.c b/src/test/test-install.c index 099eb401d7..b0f77a18f3 100644 --- a/src/test/test-install.c +++ b/src/test/test-install.c @@ -51,7 +51,7 @@ int main(int argc, char* argv[]) { UnitFileChange *changes = NULL; unsigned n_changes = 0; - h = hashmap_new(string_hash_func, string_compare_func); + h = hashmap_new(&string_hash_ops); r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h); assert_se(r == 0); diff --git a/src/test/test-prioq.c b/src/test/test-prioq.c index cdb1e4ad52..dfedc9b8dc 100644 --- a/src/test/test-prioq.c +++ b/src/test/test-prioq.c @@ -98,6 +98,11 @@ static unsigned long test_hash(const void *a, const uint8_t hash_key[HASH_KEY_SI return (unsigned long) u; } +static const struct hash_ops test_hash_ops = { + .hash = test_hash, + .compare = test_compare +}; + static void test_struct(void) { Prioq *q; Set *s; @@ -109,7 +114,7 @@ static void test_struct(void) { q = prioq_new(test_compare); assert_se(q); - s = set_new(test_hash, test_compare); + s = set_new(&test_hash_ops); assert_se(s); for (i = 0; i < SET_SIZE; i++) { diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c index 34865729f2..89f5bdd4ed 100644 --- a/src/test/test-unit-file.c +++ b/src/test/test-unit-file.c @@ -44,7 +44,7 @@ static int test_unit_file_get_set(void) { Iterator i; UnitFileList *p; - h = hashmap_new(string_hash_func, string_compare_func); + h = hashmap_new(&string_hash_ops); assert(h); r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h); -- cgit v1.2.3-54-g00ecf