From 2234bb665d989e9bc0065c15ad21f249b3a493c4 Mon Sep 17 00:00:00 2001 From: GangCao Date: Wed, 13 May 2020 16:48:56 -0400 Subject: [PATCH] Transport: allocate a global array of transports Currently the new transport is dynamically allocated and looks like not freed when the application exits. Trying to use the __attribute__((destructor)) function to free the allocated memory, it will not work in the case of user created thread as this function is called right after the "main" function while other operations may be still ongoing. In this case, add a global array of transports. Change-Id: I610b1e8114ba2e68abbd09ea5e02a9abce055e70 Signed-off-by: GangCao Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/2415 Community-CI: Mellanox Build Bot Community-CI: Broadcom CI Reviewed-by: Changpeng Liu Reviewed-by: Tomasz Zawadzki Reviewed-by: Aleksey Marchuk Reviewed-by: Xiaodong Liu Tested-by: SPDK CI Jenkins --- lib/nvme/nvme_transport.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/nvme/nvme_transport.c b/lib/nvme/nvme_transport.c index 7279badda..49d4263ec 100644 --- a/lib/nvme/nvme_transport.c +++ b/lib/nvme/nvme_transport.c @@ -38,6 +38,8 @@ #include "nvme_internal.h" #include "spdk/queue.h" +#define SPDK_MAX_NUM_OF_TRANSPORTS 16 + struct spdk_nvme_transport { struct spdk_nvme_transport_ops ops; TAILQ_ENTRY(spdk_nvme_transport) link; @@ -46,6 +48,9 @@ struct spdk_nvme_transport { TAILQ_HEAD(nvme_transport_list, spdk_nvme_transport) g_spdk_nvme_transports = TAILQ_HEAD_INITIALIZER(g_spdk_nvme_transports); +struct spdk_nvme_transport g_spdk_transports[SPDK_MAX_NUM_OF_TRANSPORTS] = {}; +int g_current_transport_index = 0; + const struct spdk_nvme_transport * nvme_get_first_transport(void) { @@ -101,12 +106,12 @@ void spdk_nvme_transport_register(const struct spdk_nvme_transport_ops *ops) assert(false); } - new_transport = calloc(1, sizeof(*new_transport)); - if (new_transport == NULL) { - SPDK_ERRLOG("Unable to allocate memory to register new NVMe transport.\n"); + if (g_current_transport_index == SPDK_MAX_NUM_OF_TRANSPORTS) { + SPDK_ERRLOG("Unable to register new NVMe transport.\n"); assert(false); return; } + new_transport = &g_spdk_transports[g_current_transport_index++]; new_transport->ops = *ops; TAILQ_INSERT_TAIL(&g_spdk_nvme_transports, new_transport, link);