From 1ca04a1d7addd611881f26ee0fda2498be65a0ba Mon Sep 17 00:00:00 2001 From: Tomasz Zawadzki Date: Thu, 16 Dec 2021 15:39:09 +0100 Subject: [PATCH] lib/sock: refactor allocation of sock_map entry At this time only spdk_sock_map_insert() allocates entries for the sock_map. Next patch will introduce it to lookup too. So now this part is refactored out to separate function. This patch should not introduce any functional change. Signed-off-by: Tomasz Zawadzki Change-Id: I46ac88aedebffe0cbc1f4616dc1fcfaf7f950b05 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/10726 Community-CI: Broadcom CI Community-CI: Mellanox Build Bot Tested-by: SPDK CI Jenkins Reviewed-by: Aleksey Marchuk Reviewed-by: Jim Harris --- lib/sock/sock.c | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/lib/sock/sock.c b/lib/sock/sock.c index 0e22a8695..30f4b06dc 100644 --- a/lib/sock/sock.c +++ b/lib/sock/sock.c @@ -66,51 +66,66 @@ sock_get_group_impl_from_group(struct spdk_sock *sock, struct spdk_sock_group *g return NULL; } +/* Called under map->mtx lock */ +static struct spdk_sock_placement_id_entry * +_sock_map_entry_alloc(struct spdk_sock_map *map, int placement_id) +{ + struct spdk_sock_placement_id_entry *entry; + + entry = calloc(1, sizeof(*entry)); + if (!entry) { + SPDK_ERRLOG("Cannot allocate an entry for placement_id=%u\n", placement_id); + return NULL; + } + + entry->placement_id = placement_id; + + STAILQ_INSERT_TAIL(&map->entries, entry, link); + + return entry; +} + int spdk_sock_map_insert(struct spdk_sock_map *map, int placement_id, struct spdk_sock_group_impl *group) { struct spdk_sock_placement_id_entry *entry; + int rc = 0; pthread_mutex_lock(&map->mtx); STAILQ_FOREACH(entry, &map->entries, link) { if (placement_id == entry->placement_id) { /* Can't set group to NULL if it is already not-NULL */ if (group == NULL) { - pthread_mutex_unlock(&map->mtx); - return (entry->group == NULL) ? 0 : -EINVAL; + rc = (entry->group == NULL) ? 0 : -EINVAL; + goto end; } if (entry->group == NULL) { entry->group = group; } else if (entry->group != group) { - pthread_mutex_unlock(&map->mtx); - return -EINVAL; + rc = -EINVAL; + goto end; } entry->ref++; - pthread_mutex_unlock(&map->mtx); - return 0; + goto end; } } - entry = calloc(1, sizeof(*entry)); - if (!entry) { - SPDK_ERRLOG("Cannot allocate an entry for placement_id=%u\n", placement_id); - pthread_mutex_unlock(&map->mtx); - return -ENOMEM; + entry = _sock_map_entry_alloc(map, placement_id); + if (entry == NULL) { + rc = -ENOMEM; + goto end; } - - entry->placement_id = placement_id; if (group) { entry->group = group; entry->ref++; } - - STAILQ_INSERT_TAIL(&map->entries, entry, link); +end: pthread_mutex_unlock(&map->mtx); - return 0; + return rc; } void