From 5715a9f0bd6669ac5153ec039c12e78d16e1ff71 Mon Sep 17 00:00:00 2001 From: yrutschle Date: Wed, 14 Apr 2021 23:26:33 +0200 Subject: [PATCH] stop managing connections as array; instead, allocate connections, and fd2cnx is the only pointer to it --- collection.h | 4 ---- gap.c | 19 +++++++++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/collection.h b/collection.h index d55c2e6..ac91268 100644 --- a/collection.h +++ b/collection.h @@ -13,10 +13,6 @@ int collection_add_fd(cnx_collection* collection, struct connection* cnx, int fd /* Remove a connection from the collection */ int collection_remove_cnx(cnx_collection* collection, struct connection *cnx); -struct connection* collection_get_cnx_from_index(cnx_collection* collection, int index); struct connection* collection_get_cnx_from_fd(struct cnx_collection* collection, int fd); -/* Returns the number of connections in the collection */ -int collection_get_length(cnx_collection* collection); - #endif diff --git a/gap.c b/gap.c index 3f8160d..26d80e3 100644 --- a/gap.c +++ b/gap.c @@ -25,6 +25,9 @@ #include #include #include +#include + +#include "sslh-conf.h" #include "gap.h" @@ -64,8 +67,7 @@ int gap_getlen(gap_array* gap) void* gap_get(gap_array* gap, int index) { - int elem_size = sizeof(gap->array[0]); - return gap->array[index * elem_size]; + return gap->array[index]; } static int gap_extend(gap_array* gap) @@ -75,20 +77,25 @@ static int gap_extend(gap_array* gap) void** new = realloc(gap->array, new_length * elem_size); if (!new) return -1; - for (int i = gap->len; i < new_length; i++) + gap->array = new; + + for (int i = gap->len; i < new_length; i++) { gap->array[i] = NULL; + } + + gap->len = new_length; + return 0; } int gap_set(gap_array* gap, int index, void* ptr) { - if (index > gap->len) { + if (index >= gap->len) { int res = gap_extend(gap); if (res == -1) return -1; } - int elem_size = sizeof(gap->array[0]); - gap->array[index * elem_size] = ptr; + gap->array[index] = ptr; return 0; }