From fb92682e8b55b813fd4bcfad42b63e57cd9aae21 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 6 Jun 2016 13:50:39 +0930 Subject: [PATCH 1/1] htable: htable_type add htable_copy. Signed-off-by: Rusty Russell --- ccan/htable/htable_type.h | 12 +++++++++--- ccan/htable/test/run-type.c | 9 +++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/ccan/htable/htable_type.h b/ccan/htable/htable_type.h index ba33f139..9c20531f 100644 --- a/ccan/htable/htable_type.h +++ b/ccan/htable/htable_type.h @@ -21,8 +21,9 @@ * * It also defines initialization and freeing functions: * void _init(struct *); - * void _init_sized(struct *, size_t); + * bool _init_sized(struct *, size_t); * void _clear(struct *); + * bool _copy(struct *dst, const struct *src); * * Add function only fails if we run out of memory: * bool _add(struct *ht, const *e); @@ -63,15 +64,20 @@ { \ htable_init(&ht->raw, name##_hash, NULL); \ } \ - static inline UNNEEDED void name##_init_sized(struct name *ht, \ + static inline UNNEEDED bool name##_init_sized(struct name *ht, \ size_t s) \ { \ - htable_init_sized(&ht->raw, name##_hash, NULL, s); \ + return htable_init_sized(&ht->raw, name##_hash, NULL, s); \ } \ static inline UNNEEDED void name##_clear(struct name *ht) \ { \ htable_clear(&ht->raw); \ } \ + static inline UNNEEDED bool name##_copy(struct name *dst, \ + const struct name *src) \ + { \ + return htable_copy(&dst->raw, &src->raw); \ + } \ static inline bool name##_add(struct name *ht, const type *elem) \ { \ return htable_add(&ht->raw, hashfn(keyof(elem)), elem); \ diff --git a/ccan/htable/test/run-type.c b/ccan/htable/test/run-type.c index 51a85ff2..e1a78f49 100644 --- a/ccan/htable/test/run-type.c +++ b/ccan/htable/test/run-type.c @@ -110,13 +110,13 @@ static bool check_mask(struct htable *ht, const struct obj val[], unsigned num) int main(int argc, char *argv[]) { unsigned int i; - struct htable_obj ht; + struct htable_obj ht, ht2; struct obj val[NUM_VALS], *result; unsigned int dne; void *p; struct htable_obj_iter iter; - plan_tests(27); + plan_tests(29); for (i = 0; i < NUM_VALS; i++) val[i].key = i; dne = i; @@ -171,8 +171,12 @@ int main(int argc, char *argv[]) find_vals(&ht, val, NUM_VALS); ok1(!htable_obj_get(&ht, &dne)); + /* Check copy. */ + ok1(htable_obj_copy(&ht2, &ht)); + /* Delete them all by key. */ del_vals_bykey(&ht, val, NUM_VALS); + del_vals_bykey(&ht2, val, NUM_VALS); /* Write two of the same value. */ val[1] = val[0]; @@ -201,5 +205,6 @@ int main(int argc, char *argv[]) } htable_obj_clear(&ht); + htable_obj_clear(&ht2); return exit_status(); } -- 2.39.2