From: seantywork Date: Wed, 12 Mar 2025 23:29:57 +0000 (+0900) Subject: add: freeall X-Git-Url: https://git.feebdaed.xyz/?a=commitdiff_plain;h=3715480b9dc5e6f82a298ea2df908f601f56000c;p=socialize.git add: freeall --- diff --git a/include/socialize/core.h b/include/socialize/core.h index b275947..41cb679 100644 --- a/include/socialize/core.h +++ b/include/socialize/core.h @@ -222,6 +222,7 @@ struct SOCK_CONTEXT_LOCK { struct SOCK_CTL { + int in_use; struct spinlock slock; int size; struct SOCK_CONTEXT** SOCK_CTX; diff --git a/include/socialize/sock/sock.h b/include/socialize/sock/sock.h index d136dc8..26e8c4f 100644 --- a/include/socialize/sock/sock.h +++ b/include/socialize/sock/sock.h @@ -7,6 +7,7 @@ int init_all(); +int free_all(); void sock_listen_and_serve(void* varg); diff --git a/src/ctl.c b/src/ctl.c index e61088a..800a043 100644 --- a/src/ctl.c +++ b/src/ctl.c @@ -256,6 +256,15 @@ struct SOCK_CONTEXT* get_sockctx_by_fd(int fd){ spinlock_lock(&SOCK_CTL.slock); + if(SOCK_CTL.in_use == 0){ + + printf("sockctl not in use\n"); + + spinlock_unlock(&SOCK_CTL.slock); + + return NULL; + } + int i = make_hash(fd, SOCK_CTL.size); ctx = SOCK_CTL.SOCK_CTX[i]; @@ -506,6 +515,15 @@ int calloc_sockctx(int fd){ spinlock_lock(&SOCK_CTL.slock); + if(SOCK_CTL.in_use == 0){ + + printf("sockctl not in use\n"); + + spinlock_unlock(&SOCK_CTL.slock); + + return -1; + } + int i = make_hash(fd, SOCK_CTL.size); ctx = SOCK_CTL.SOCK_CTX[i]; @@ -552,6 +570,16 @@ int free_sockctx(int fd, int memfree){ spinlock_lock(&SOCK_CTL.slock); + if(SOCK_CTL.in_use == 0){ + + printf("sockctl not in use\n"); + + spinlock_unlock(&SOCK_CTL.slock); + + return -1; + } + + int i = make_hash(fd, SOCK_CTL.size); ctx = SOCK_CTL.SOCK_CTX[i]; diff --git a/src/sock/sock.c b/src/sock/sock.c index 07ddd22..af087cf 100644 --- a/src/sock/sock.c +++ b/src/sock/sock.c @@ -23,6 +23,7 @@ struct SOCK_CTL SOCK_CTL; int init_all(){ + for(int i = 0 ; i < MAX_CONN;i ++){ CHAN_CTX[i].ssl = NULL; @@ -31,6 +32,7 @@ int init_all(){ } + SOCK_CTL.in_use = 1; spinlock_init(&SOCK_CTL.slock); @@ -55,6 +57,26 @@ int init_all(){ return 0; } +int free_all(){ + + spinlock_lock(&SOCK_CTL.slock); + + SOCK_CTL.in_use = 0; + + for(int i = 0; i < SOCK_CTL.size; i++){ + + free(SOCK_CTL.SOCK_CTX[i]); + + free(SOCK_CTL.SOCK_CTX_LOCK[i]); + + } + + free(SOCK_CTL.SOCK_CTX); + + free(SOCK_CTL.SOCK_CTX_LOCK); + + spinlock_unlock(&SOCK_CTL.slock); +} void sock_listen_and_serve(void* varg){