From 327cb50d8370833bad9d260b9dc8f5c70fb5e2ce Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Sat, 15 Jul 2017 14:40:07 +0530 Subject: swrap: Internal reorganization of core socket_info structures The change basically splits socket_info structure into two structures, namely, - socket_info: to store the core information corresponding to a socket entry. - socket_info_container: wrapping structure to hold the socket_info data as well as metadata(currently refcount and next_free). Pair-Programmed-With: Anoop C S Signed-off-by: Michael Adam Signed-off-by: Anoop C S Reviewed-by: Andreas Schneider --- src/socket_wrapper.c | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/socket_wrapper.c b/src/socket_wrapper.c index 4db4bb9..8daa75e 100644 --- a/src/socket_wrapper.c +++ b/src/socket_wrapper.c @@ -185,6 +185,9 @@ enum swrap_dbglvl_e { # define SWRAP_UNLOCK_ALL \ SWRAP_UNLOCK(libc_symbol_binding); \ +#define SOCKET_INFO_CONTAINER(si) \ + (struct socket_info_container *)(si) + #define SWRAP_DLIST_ADD(list,item) do { \ if (!(list)) { \ (item)->prev = NULL; \ @@ -301,10 +304,6 @@ int first_free; struct socket_info { - unsigned int refcount; - - int next_free; - int family; int type; int protocol; @@ -329,7 +328,14 @@ struct socket_info } io; }; -static struct socket_info *sockets; +struct socket_info_container +{ + struct socket_info info; + unsigned int refcount; + int next_free; +}; + +static struct socket_info_container *sockets; static size_t max_sockets = 0; /* @@ -1200,27 +1206,35 @@ static struct socket_info *swrap_get_socket_info(int si_index) static int swrap_get_refcount(struct socket_info *si) { - return si->refcount; + struct socket_info_container *sic = SOCKET_INFO_CONTAINER(si); + return sic->refcount; } static void swrap_inc_refcount(struct socket_info *si) { - si->refcount += 1; + struct socket_info_container *sic = SOCKET_INFO_CONTAINER(si); + + sic->refcount += 1; } static void swrap_dec_refcount(struct socket_info *si) { - si->refcount -= 1; + struct socket_info_container *sic = SOCKET_INFO_CONTAINER(si); + + sic->refcount -= 1; } static int swrap_get_next_free(struct socket_info *si) { - return si->next_free; + struct socket_info_container *sic = SOCKET_INFO_CONTAINER(si); + + return sic->next_free; } static void swrap_set_next_free(struct socket_info *si, int next_free) { - si->next_free = next_free; + struct socket_info_container *sic = SOCKET_INFO_CONTAINER(si); + sic->next_free = next_free; } static const char *socket_wrapper_dir(void) @@ -1313,8 +1327,8 @@ static void socket_wrapper_init_sockets(void) max_sockets = socket_wrapper_max_sockets(); - sockets = (struct socket_info *)calloc(max_sockets, - sizeof(struct socket_info)); + sockets = (struct socket_info_container *)calloc(max_sockets, + sizeof(struct socket_info_container)); if (sockets == NULL) { SWRAP_LOG(SWRAP_LOG_ERROR, @@ -1325,10 +1339,11 @@ static void socket_wrapper_init_sockets(void) first_free = 0; for (i = 0; i < max_sockets; i++) { - swrap_set_next_free(&sockets[i], i+1); + swrap_set_next_free(&sockets[i].info, i+1); } - swrap_set_next_free(&sockets[max_sockets-1], -1); + /* mark the end of the free list */ + swrap_set_next_free(&sockets[max_sockets-1].info, -1); } bool socket_wrapper_enabled(void) -- cgit