rename unused session inner exdata

This commit is contained in:
luwenpeng
2024-08-21 10:02:46 +08:00
parent 28936ec2ad
commit 99edee1c4f
5 changed files with 0 additions and 249 deletions

View File

@@ -4,23 +4,6 @@
#include "session_utils.h"
#include "session_manager.h"
#define EX_KEY_MAX_LEN 64
struct session_exdata_schema
{
char key[EX_KEY_MAX_LEN];
session_ex_free_cb *free_cb;
void *args;
};
struct session_exdata_manager
{
struct session_exdata_schema schemas[EX_DATA_MAX_COUNT];
uint8_t count;
};
static struct session_exdata_manager g_ex_manager = {};
/******************************************************************************
* session set/get
******************************************************************************/
@@ -274,108 +257,6 @@ void session_free_tcp_segment(struct session *sess, struct tcp_segment *seg)
}
}
/******************************************************************************
* session ex data
******************************************************************************/
/*
* the exdata prodoced by user, and comsumed by same user.
* so, the exdata is not shared by different user.
* otherwise, the exdata need dup by refer count, and free by refer count.
*
* if key exist, not allow update, return original index.
*/
uint8_t session_get_ex_new_index(const char *key, session_ex_free_cb *free_cb, void *args)
{
if (g_ex_manager.count >= EX_DATA_MAX_COUNT)
{
abort();
return EX_DATA_MAX_COUNT;
}
for (uint8_t i = 0; i < g_ex_manager.count; i++)
{
if (strcmp(g_ex_manager.schemas[i].key, key) == 0)
{
return i;
}
}
uint8_t idx = g_ex_manager.count;
g_ex_manager.count++;
struct session_exdata_schema *schema = &g_ex_manager.schemas[idx];
strncpy(schema->key, key, EX_KEY_MAX_LEN);
schema->free_cb = free_cb;
schema->args = args;
return idx;
}
/*
* Support update ex_data.
*
* if key exist: run free_cb free old value, then set new value.
* if not run free_cb, old value will be memory leak.
* if not allow update, new value will be memory leak.
* if key not exist: set new value.
*/
void session_set_ex_data(struct session *sess, uint8_t idx, void *val)
{
if (idx >= g_ex_manager.count)
{
assert(0);
return;
}
session_free_ex_data(sess, idx);
sess->ex_data[idx] = val;
}
void *session_get0_ex_data(const struct session *sess, uint8_t idx)
{
if (idx >= g_ex_manager.count)
{
assert(0);
return NULL;
}
return sess->ex_data[idx];
}
/*
* after set ex_data, the owner of ex_data is session, so user should not free it directly.
* if user want to free ex_data, should use session_free_ex_data.
*/
void session_free_ex_data(struct session *sess, uint8_t idx)
{
if (idx >= g_ex_manager.count)
{
assert(0);
return;
}
struct session_exdata_schema *schema = &g_ex_manager.schemas[idx];
if (schema->free_cb != NULL && sess->ex_data[idx] != NULL)
{
printf("free ex_data, idx: %d, key: %s, val: %p\n", idx, schema->key, sess->ex_data[idx]);
schema->free_cb(sess, idx, sess->ex_data[idx], schema->args);
}
sess->ex_data[idx] = NULL;
}
void session_free_all_ex_data(struct session *sess)
{
if (sess)
{
for (uint8_t i = 0; i < g_ex_manager.count; i++)
{
session_free_ex_data(sess, i);
}
}
}
/******************************************************************************
* to string
******************************************************************************/