stop managing connections as array; instead, allocate connections, and fd2cnx is the only pointer to it

This commit is contained in:
yrutschle 2021-04-14 23:26:33 +02:00
parent de474e1d07
commit 5715a9f0bd
2 changed files with 13 additions and 10 deletions

View File

@ -13,10 +13,6 @@ int collection_add_fd(cnx_collection* collection, struct connection* cnx, int fd
/* Remove a connection from the collection */ /* Remove a connection from the collection */
int collection_remove_cnx(cnx_collection* collection, struct connection *cnx); 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); 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 #endif

19
gap.c
View File

@ -25,6 +25,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h>
#include "sslh-conf.h"
#include "gap.h" #include "gap.h"
@ -64,8 +67,7 @@ int gap_getlen(gap_array* gap)
void* gap_get(gap_array* gap, int index) void* gap_get(gap_array* gap, int index)
{ {
int elem_size = sizeof(gap->array[0]); return gap->array[index];
return gap->array[index * elem_size];
} }
static int gap_extend(gap_array* gap) 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); void** new = realloc(gap->array, new_length * elem_size);
if (!new) return -1; 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->array[i] = NULL;
}
gap->len = new_length;
return 0; return 0;
} }
int gap_set(gap_array* gap, int index, void* ptr) int gap_set(gap_array* gap, int index, void* ptr)
{ {
if (index > gap->len) { if (index >= gap->len) {
int res = gap_extend(gap); int res = gap_extend(gap);
if (res == -1) return -1; if (res == -1) return -1;
} }
int elem_size = sizeof(gap->array[0]); gap->array[index] = ptr;
gap->array[index * elem_size] = ptr;
return 0; return 0;
} }