From f1790cf31436dd06663e1018a848ab72156c4a3c Mon Sep 17 00:00:00 2001 From: Karchnu Date: Sat, 4 Jul 2020 19:29:59 +0200 Subject: [PATCH] switching ok --- examples/simple-tcpd.c | 2 +- src/communication.c | 2 +- src/context.c | 2 ++ src/network.c | 21 +++++++++++++-------- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/examples/simple-tcpd.c b/examples/simple-tcpd.c index ec01fbb..2824a85 100644 --- a/examples/simple-tcpd.c +++ b/examples/simple-tcpd.c @@ -249,7 +249,7 @@ void exit_program (int signal) // Close then free remaining ctx. ipc_close_all (ctx); - // ipc_ctx_free (ctx); + ipc_ctx_free (ctx); // free, free everything! free (ctx); diff --git a/src/communication.c b/src/communication.c index 39d1877..9ab37f8 100644 --- a/src/communication.c +++ b/src/communication.c @@ -394,7 +394,7 @@ struct ipc_error handle_new_message (struct ipc_event *event, struct ipc_ctx *ct } #endif - IPC_EVENT_SET (event, IPC_EVENT_TYPE_DISCONNECTION, index, ctx->pollfd[index].fd, NULL); + IPC_EVENT_SET (event, IPC_EVENT_TYPE_DISCONNECTION, index, talkingfd, NULL); IPC_RETURN_ERROR (IPC_ERROR_CLOSED_RECIPIENT); } } diff --git a/src/context.c b/src/context.c index 5438cf7..8e7aa3e 100644 --- a/src/context.c +++ b/src/context.c @@ -42,5 +42,7 @@ void ipc_ctx_free (struct ipc_ctx *ctx) } ctx->size = 0; + ipc_switching_free(&ctx->switchdb); + ipc_messages_free (&ctx->tx); } diff --git a/src/network.c b/src/network.c index fc88a02..582bbb0 100644 --- a/src/network.c +++ b/src/network.c @@ -83,7 +83,13 @@ struct ipc_error ipc_provide_fd (int sock, int fd) void ipc_switching_add (struct ipc_switchings *is, int orig, int dest) { - is->collection = realloc (is->collection, sizeof (struct ipc_switching) * (is->size + 1)); + if (is->collection == NULL) { + is->collection = malloc (sizeof (struct ipc_switching) * (is->size + 1)); + } + else { + is->collection = realloc (is->collection, sizeof (struct ipc_switching) * (is->size + 1)); + } + if (is->collection == NULL) { fprintf (stderr, __FILE__ " error realloc line %d", __LINE__); exit (EXIT_FAILURE); @@ -110,13 +116,12 @@ int ipc_switching_del (struct ipc_switchings *is, int fd) is->collection[i].orig = is->collection[is->size - 1].orig; is->collection[i].dest = is->collection[is->size - 1].dest; - size_t s = (is->size - 1) > 0 ? (is->size - 1) : 1; - - is->collection = realloc (is->collection, sizeof (struct ipc_switching) * s); - if (is->collection == NULL) { - /** TODO: not sure we want this behavior */ - fprintf (stderr, __FILE__ " error realloc line %d", __LINE__); - exit (EXIT_FAILURE); + if (is->size == 1) { + free(is->collection); + is->collection = NULL; + } + else { + is->collection = realloc (is->collection, sizeof (struct ipc_switching) * (is->size-1)); } is->size--;