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 <tomasz.zawadzki@intel.com>
Change-Id: I46ac88aedebffe0cbc1f4616dc1fcfaf7f950b05
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/10726
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Community-CI: Mellanox Build Bot
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Aleksey Marchuk <alexeymar@mellanox.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Tomasz Zawadzki 2021-12-16 15:39:09 +01:00
parent 91f2725291
commit 1ca04a1d7a

View File

@ -66,51 +66,66 @@ sock_get_group_impl_from_group(struct spdk_sock *sock, struct spdk_sock_group *g
return NULL; 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 int
spdk_sock_map_insert(struct spdk_sock_map *map, int placement_id, spdk_sock_map_insert(struct spdk_sock_map *map, int placement_id,
struct spdk_sock_group_impl *group) struct spdk_sock_group_impl *group)
{ {
struct spdk_sock_placement_id_entry *entry; struct spdk_sock_placement_id_entry *entry;
int rc = 0;
pthread_mutex_lock(&map->mtx); pthread_mutex_lock(&map->mtx);
STAILQ_FOREACH(entry, &map->entries, link) { STAILQ_FOREACH(entry, &map->entries, link) {
if (placement_id == entry->placement_id) { if (placement_id == entry->placement_id) {
/* Can't set group to NULL if it is already not-NULL */ /* Can't set group to NULL if it is already not-NULL */
if (group == NULL) { if (group == NULL) {
pthread_mutex_unlock(&map->mtx); rc = (entry->group == NULL) ? 0 : -EINVAL;
return (entry->group == NULL) ? 0 : -EINVAL; goto end;
} }
if (entry->group == NULL) { if (entry->group == NULL) {
entry->group = group; entry->group = group;
} else if (entry->group != group) { } else if (entry->group != group) {
pthread_mutex_unlock(&map->mtx); rc = -EINVAL;
return -EINVAL; goto end;
} }
entry->ref++; entry->ref++;
pthread_mutex_unlock(&map->mtx); goto end;
return 0;
} }
} }
entry = calloc(1, sizeof(*entry)); entry = _sock_map_entry_alloc(map, placement_id);
if (!entry) { if (entry == NULL) {
SPDK_ERRLOG("Cannot allocate an entry for placement_id=%u\n", placement_id); rc = -ENOMEM;
pthread_mutex_unlock(&map->mtx); goto end;
return -ENOMEM;
} }
entry->placement_id = placement_id;
if (group) { if (group) {
entry->group = group; entry->group = group;
entry->ref++; entry->ref++;
} }
end:
STAILQ_INSERT_TAIL(&map->entries, entry, link);
pthread_mutex_unlock(&map->mtx); pthread_mutex_unlock(&map->mtx);
return 0; return rc;
} }
void void