Skip to content

Commit 1b374dd

Browse files
committed
tree: separate create host code from nvme_lookup_host
Split host creation code into a separate function to explicitly document on-demand object creation. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 0aad423 commit 1b374dd

2 files changed

Lines changed: 33 additions & 14 deletions

File tree

libnvme/src/nvme/tree.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ int nvme_host_get_ids(struct nvme_global_ctx *ctx,
195195
}
196196

197197
int nvme_host_get(struct nvme_global_ctx *ctx, const char *hostnqn,
198-
const char *hostid, nvme_host_t *hp)
198+
const char *hostid, nvme_host_t *host)
199199
{
200200
_cleanup_free_ char *hnqn = NULL;
201201
_cleanup_free_ char *hid = NULL;
@@ -207,10 +207,12 @@ int nvme_host_get(struct nvme_global_ctx *ctx, const char *hostnqn,
207207
return err;
208208

209209
h = nvme_lookup_host(ctx, hnqn, hid);
210+
if (!h)
211+
return -ENOMEM;
210212

211213
nvme_host_set_hostsymname(h, NULL);
212214

213-
*hp = h;
215+
*host = h;
214216
return 0;
215217
}
216218

@@ -762,13 +764,37 @@ void nvme_free_host(struct nvme_host *h)
762764
__nvme_free_host(h);
763765
}
764766

765-
struct nvme_host *nvme_lookup_host(struct nvme_global_ctx *ctx, const char *hostnqn,
766-
const char *hostid)
767+
static int nvme_create_host(struct nvme_global_ctx *ctx, const char *hostnqn,
768+
const char *hostid, struct nvme_host **host)
769+
{
770+
struct nvme_host *h;
771+
772+
h = calloc(1, sizeof(*h));
773+
if (!h)
774+
return -ENOMEM;
775+
776+
h->hostnqn = strdup(hostnqn);
777+
if (hostid)
778+
h->hostid = strdup(hostid);
779+
list_head_init(&h->subsystems);
780+
list_node_init(&h->entry);
781+
h->ctx = ctx;
782+
783+
list_add_tail(&ctx->hosts, &h->entry);
784+
785+
*host = h;
786+
787+
return 0;
788+
}
789+
790+
struct nvme_host *nvme_lookup_host(struct nvme_global_ctx *ctx,
791+
const char *hostnqn, const char *hostid)
767792
{
768793
struct nvme_host *h;
769794

770795
if (!hostnqn)
771796
return NULL;
797+
772798
nvme_for_each_host(ctx, h) {
773799
if (strcmp(h->hostnqn, hostnqn))
774800
continue;
@@ -777,16 +803,9 @@ struct nvme_host *nvme_lookup_host(struct nvme_global_ctx *ctx, const char *host
777803
continue;
778804
return h;
779805
}
780-
h = calloc(1,sizeof(*h));
781-
if (!h)
806+
807+
if (nvme_create_host(ctx, hostnqn, hostid, &h))
782808
return NULL;
783-
h->hostnqn = strdup(hostnqn);
784-
if (hostid)
785-
h->hostid = strdup(hostid);
786-
list_head_init(&h->subsystems);
787-
list_node_init(&h->entry);
788-
h->ctx = ctx;
789-
list_add_tail(&ctx->hosts, &h->entry);
790809

791810
return h;
792811
}

libnvme/test/tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct test_data {
3434
};
3535

3636
#define DEFAULT_HOSTID "9ba1651a-ed36-11f0-9858-6c1ff71ba506"
37-
#define DEFAULT_HOSTNQN "nqn.2014-08.org.nvmexpress:uuid:DEFAULT_HOSTID"
37+
#define DEFAULT_HOSTNQN "nqn.2014-08.org.nvmexpress:uuid:9ba1651a-ed36-11f0-9858-6c1ff71ba506"
3838
#define DEFAULT_SUBSYSNAME "subsysname"
3939
#define DEFAULT_SUBSYSNQN "subsysnqn"
4040
#define SRC_ADDR4 "192.168.56.100"

0 commit comments

Comments
 (0)