]> git.feebdaed.xyz Git - socialize.git/commitdiff
add: freeall
authorseantywork <seantywork@gmail.com>
Wed, 12 Mar 2025 23:29:57 +0000 (08:29 +0900)
committerseantywork <seantywork@gmail.com>
Wed, 12 Mar 2025 23:29:57 +0000 (08:29 +0900)
include/socialize/core.h
include/socialize/sock/sock.h
src/ctl.c
src/sock/sock.c

index b275947f222973a563566905313669b4fd7317fc..41cb6794ab8d15c22405a71969d921506dfb9ee9 100644 (file)
@@ -222,6 +222,7 @@ struct SOCK_CONTEXT_LOCK {
 
 
 struct SOCK_CTL {
+    int in_use;
     struct spinlock slock;
     int size;
     struct SOCK_CONTEXT** SOCK_CTX;
index d136dc853587f7aa70351450e956659b59354d7f..26e8c4ffe33ec2a8291d235a21de8e48f441da4c 100644 (file)
@@ -7,6 +7,7 @@
 
 int init_all();
 
+int free_all();
 
 void sock_listen_and_serve(void* varg);
 
index e61088af8d41c0bb335b799f6812b1aea1e0625c..800a04367e48ab3ccbb8138dd8ddd02bb97f1c6d 100644 (file)
--- 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];
index 07ddd22ebbd27b46b62c68bbed54c168034e39f3..af087cf6536c3993481139e24e5023e90c399884 100644 (file)
@@ -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){