Skip to content

Commit 605b4c4

Browse files
committed
kvs: use zhashx for roothash
Problem: The primary roothash is a zhash_t, however it is more common to use a zhashx_t in flux-core since the initial memory footprint is much smaller. Convert roothash to use a zhashx_t over a zhash_t.
1 parent 41df35c commit 605b4c4

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

src/modules/kvs/kvsroot.c

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,26 @@
2929
#include "kvsroot.h"
3030

3131
struct kvsroot_mgr {
32-
zhash_t *roothash;
32+
zhashx_t *roothash;
3333
zlist_t *removelist;
3434
bool iterating_roots;
3535
flux_t *h;
3636
void *arg;
3737
};
3838

39+
static void kvsroot_destroy (void **data);
40+
3941
kvsroot_mgr_t *kvsroot_mgr_create (flux_t *h, void *arg)
4042
{
4143
kvsroot_mgr_t *krm = NULL;
4244

4345
if (!(krm = calloc (1, sizeof (*krm))))
4446
goto error;
45-
if (!(krm->roothash = zhash_new ())) {
47+
if (!(krm->roothash = zhashx_new ())) {
4648
errno = ENOMEM;
4749
goto error;
4850
}
51+
zhashx_set_destructor (krm->roothash, kvsroot_destroy);
4952
if (!(krm->removelist = zlist_new ())) {
5053
errno = ENOMEM;
5154
goto error;
@@ -65,7 +68,7 @@ void kvsroot_mgr_destroy (kvsroot_mgr_t *krm)
6568
if (krm) {
6669
int save_errno = errno;
6770
if (krm->roothash)
68-
zhash_destroy (&krm->roothash);
71+
zhashx_destroy (&krm->roothash);
6972
if (krm->removelist)
7073
zlist_destroy (&krm->removelist);
7174
free (krm);
@@ -75,13 +78,14 @@ void kvsroot_mgr_destroy (kvsroot_mgr_t *krm)
7578

7679
int kvsroot_mgr_root_count (kvsroot_mgr_t *krm)
7780
{
78-
return zhash_size (krm->roothash);
81+
return zhashx_size (krm->roothash);
7982
}
8083

81-
static void kvsroot_destroy (void *data)
84+
/* zhashx_destructor_fn */
85+
static void kvsroot_destroy (void **data)
8286
{
8387
if (data) {
84-
struct kvsroot *root = data;
88+
struct kvsroot *root = *data;
8589
int save_errno = errno;
8690
if (root->ns_name)
8791
free (root->ns_name);
@@ -93,7 +97,7 @@ static void kvsroot_destroy (void *data)
9397
zlist_destroy (&root->wait_version_list);
9498
if (root->setroot_queue)
9599
flux_msglist_destroy (root->setroot_queue);
96-
free (data);
100+
free (root);
97101
errno = save_errno;
98102
}
99103
}
@@ -105,6 +109,8 @@ static void flux_msg_decref_wrapper (void **item)
105109
flux_msg_decref (msg);
106110
}
107111

112+
/* zhashx_destructor_fn */
113+
108114
struct kvsroot *kvsroot_mgr_create_root (kvsroot_mgr_t *krm,
109115
struct cache *cache,
110116
const char *hash_name,
@@ -113,7 +119,6 @@ struct kvsroot *kvsroot_mgr_create_root (kvsroot_mgr_t *krm,
113119
int flags)
114120
{
115121
struct kvsroot *root;
116-
int save_errnum;
117122

118123
/* Don't modify hash while iterating */
119124
if (krm->iterating_roots) {
@@ -163,25 +168,17 @@ struct kvsroot *kvsroot_mgr_create_root (kvsroot_mgr_t *krm,
163168
root->flags = flags;
164169
root->remove = false;
165170

166-
if (zhash_insert (krm->roothash, ns, root) < 0) {
171+
if (zhashx_insert (krm->roothash, ns, root) < 0) {
167172
errno = EEXIST;
168-
flux_log_error (krm->h, "zhash_insert");
169-
goto error;
170-
}
171-
172-
if (!zhash_freefn (krm->roothash, ns, kvsroot_destroy)) {
173-
flux_log_error (krm->h, "zhash_freefn");
174-
save_errnum = errno;
175-
zhash_delete (krm->roothash, ns);
176-
errno = save_errnum;
173+
flux_log_error (krm->h, "zhashx_insert");
177174
goto error;
178175
}
179176

180177
list_node_init (&root->work_queue_node);
181178
return root;
182179

183180
error:
184-
kvsroot_destroy (root);
181+
kvsroot_destroy ((void **)&root);
185182
return NULL;
186183
}
187184

@@ -204,14 +201,14 @@ int kvsroot_mgr_remove_root (kvsroot_mgr_t *krm, const char *ns)
204201
}
205202
}
206203
else
207-
zhash_delete (krm->roothash, ns);
204+
zhashx_delete (krm->roothash, ns);
208205
return 0;
209206
}
210207

211208
struct kvsroot *kvsroot_mgr_lookup_root (kvsroot_mgr_t *krm,
212209
const char *ns)
213210
{
214-
return zhash_lookup (krm->roothash, ns);
211+
return zhashx_lookup (krm->roothash, ns);
215212
}
216213

217214
struct kvsroot *kvsroot_mgr_lookup_root_safe (kvsroot_mgr_t *krm,
@@ -233,7 +230,7 @@ int kvsroot_mgr_iter_roots (kvsroot_mgr_t *krm, kvsroot_root_f cb, void *arg)
233230

234231
krm->iterating_roots = true;
235232

236-
root = zhash_first (krm->roothash);
233+
root = zhashx_first (krm->roothash);
237234
while (root) {
238235
int ret;
239236

@@ -243,7 +240,7 @@ int kvsroot_mgr_iter_roots (kvsroot_mgr_t *krm, kvsroot_root_f cb, void *arg)
243240
if (ret == 1)
244241
break;
245242

246-
root = zhash_next (krm->roothash);
243+
root = zhashx_next (krm->roothash);
247244
}
248245

249246
krm->iterating_roots = false;

0 commit comments

Comments
 (0)