Refactor the session manager using session transformation 2D array & Update test case

This commit is contained in:
luwenpeng
2024-03-14 10:56:09 +08:00
parent 639614622b
commit ce00122557
47 changed files with 3403 additions and 3563 deletions

View File

@@ -12,8 +12,8 @@ struct session_table
void *arg;
uint64_t count;
struct session *least_recently_unused;
struct session *least_recently_used;
struct session *head; // Least recently used
struct session *tail; // Most recently used
};
/******************************************************************************
@@ -45,58 +45,54 @@ static int HASH_KEYCMP_OVERWRITE(const void *key_a, const void *key_b, size_t le
return -1;
}
static void session_table_add_session_to_linklist(struct session_table *table, struct session *sess)
// add session to the tail of the list
static void add_session_to_list(struct session_table *table, struct session *sess)
{
if (table == NULL || sess == NULL)
{
return;
}
if (table->least_recently_used == NULL)
sess->next_ptr = NULL;
sess->prev_ptr = NULL;
if (table->head == NULL && table->tail == NULL)
{
table->least_recently_unused = sess;
table->least_recently_used = sess;
sess->prev_ptr = NULL;
sess->next_ptr = NULL;
table->head = sess;
table->tail = sess;
}
else
{
sess->next_ptr = table->least_recently_used;
table->least_recently_used->prev_ptr = sess;
sess->prev_ptr = NULL;
table->least_recently_used = sess;
table->tail->next_ptr = sess;
sess->prev_ptr = table->tail;
table->tail = sess;
}
}
static void session_table_del_session_from_linklist(struct session_table *table, struct session *sess)
static void del_session_from_list(struct session_table *table, struct session *sess)
{
if (table == NULL || sess == NULL)
{
return;
}
if (sess->prev_ptr == NULL && sess->next_ptr == NULL)
if (sess->prev_ptr)
{
table->least_recently_unused = NULL;
table->least_recently_used = NULL;
}
else if (sess->prev_ptr == NULL && sess->next_ptr != NULL)
{
table->least_recently_used = sess->next_ptr;
sess->next_ptr->prev_ptr = NULL;
}
else if (sess->prev_ptr != NULL && sess->next_ptr == NULL)
{
table->least_recently_unused = sess->prev_ptr;
sess->prev_ptr->next_ptr = NULL;
sess->prev_ptr->next_ptr = sess->next_ptr;
}
else
{
sess->prev_ptr->next_ptr = sess->next_ptr;
table->head = sess->next_ptr;
}
if (sess->next_ptr)
{
sess->next_ptr->prev_ptr = sess->prev_ptr;
}
sess->prev_ptr = NULL;
sess->next_ptr = NULL;
else
{
table->tail = sess->prev_ptr;
}
}
/******************************************************************************
@@ -107,8 +103,8 @@ struct session_table *session_table_new()
{
struct session_table *table = (struct session_table *)calloc(1, sizeof(struct session_table));
table->count = 0;
table->least_recently_unused = NULL;
table->least_recently_used = NULL;
table->head = NULL;
table->tail = NULL;
return table;
}
@@ -153,33 +149,33 @@ void session_table_set_freecb(struct session_table *table, session_free_cb free_
}
}
int session_table_add_session(struct session_table *table, const struct tuple6 *tuple, struct session *sess)
int session_table_add(struct session_table *table, const struct tuple6 *tuple, struct session *sess)
{
if (table == NULL || sess == NULL)
{
return -1;
}
if (session_table_find_session(table, tuple))
if (session_table_find_tuple(table, tuple))
{
return -1;
}
HASH_ADD(hh, table->root, tuple, sizeof(sess->tuple), sess);
session_table_add_session_to_linklist(table, sess);
add_session_to_list(table, sess);
table->count++;
return 0;
}
void session_table_del_session(struct session_table *table, const struct tuple6 *tuple)
void session_table_del(struct session_table *table, const struct tuple6 *tuple)
{
if (table == NULL)
{
return;
}
struct session *sess = session_table_find_session(table, tuple);
struct session *sess = session_table_find_tuple(table, tuple);
if (sess == NULL)
{
return;
@@ -190,11 +186,11 @@ void session_table_del_session(struct session_table *table, const struct tuple6
{
table->free_cb(sess, table->arg);
}
session_table_del_session_from_linklist(table, sess);
del_session_from_list(table, sess);
table->count--;
}
struct session *session_table_find_session(struct session_table *table, const struct tuple6 *tuple)
struct session *session_table_find_tuple(struct session_table *table, const struct tuple6 *tuple)
{
if (table == NULL)
{
@@ -213,29 +209,29 @@ struct session *session_table_find_session(struct session_table *table, const st
if (sess)
{
session_table_del_session_from_linklist(table, sess);
session_table_add_session_to_linklist(table, sess);
del_session_from_list(table, sess);
add_session_to_list(table, sess);
}
return sess;
}
struct session *session_table_find_least_recently_unused_session(struct session_table *table)
struct session *session_table_find_lru(struct session_table *table)
{
if (table == NULL)
{
return NULL;
}
return table->least_recently_unused;
return table->head;
}
struct session *session_table_find_least_recently_used_session(struct session_table *table)
struct session *session_table_find_mru(struct session_table *table)
{
if (table == NULL)
{
return NULL;
}
return table->least_recently_used;
return table->tail;
}