2023-12-11 16:27:39 +08:00
|
|
|
#include "session_pool.h"
|
2023-12-11 16:35:26 +08:00
|
|
|
#include "session_private.h"
|
2023-12-11 16:27:39 +08:00
|
|
|
|
|
|
|
|
struct session_pool
|
|
|
|
|
{
|
|
|
|
|
uint64_t count;
|
|
|
|
|
struct session *sess;
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-08 14:20:36 +08:00
|
|
|
struct session_pool *session_pool_new(uint64_t count)
|
2023-12-11 16:27:39 +08:00
|
|
|
{
|
|
|
|
|
struct session_pool *pool = (struct session_pool *)calloc(1, sizeof(struct session_pool) + count * sizeof(struct session));
|
|
|
|
|
if (pool == NULL)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pool->count = count;
|
|
|
|
|
pool->sess = (struct session *)(pool + 1);
|
|
|
|
|
|
|
|
|
|
for (uint64_t i = 0; i < count; i++)
|
|
|
|
|
{
|
2023-12-13 17:56:27 +08:00
|
|
|
pool->sess[i].next_free_ptr = &pool->sess[i + 1];
|
2023-12-11 16:27:39 +08:00
|
|
|
}
|
2023-12-13 17:56:27 +08:00
|
|
|
pool->sess[count - 1].next_free_ptr = NULL;
|
2023-12-11 16:27:39 +08:00
|
|
|
|
|
|
|
|
return pool;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-08 14:20:36 +08:00
|
|
|
void session_pool_free(struct session_pool *pool)
|
2023-12-11 16:27:39 +08:00
|
|
|
{
|
|
|
|
|
if (pool)
|
|
|
|
|
{
|
|
|
|
|
free(pool);
|
|
|
|
|
pool = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
struct session *session_pool_pop(struct session_pool *pool)
|
2023-12-11 16:27:39 +08:00
|
|
|
{
|
|
|
|
|
if (pool == NULL)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct session *sess = pool->sess;
|
|
|
|
|
if (sess == NULL)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-13 17:56:27 +08:00
|
|
|
pool->sess = sess->next_free_ptr;
|
|
|
|
|
sess->next_free_ptr = NULL;
|
2023-12-11 16:35:26 +08:00
|
|
|
pool->count--;
|
2023-12-11 16:27:39 +08:00
|
|
|
|
|
|
|
|
return sess;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-09 19:28:14 +08:00
|
|
|
void session_pool_push(struct session_pool *pool, struct session *sess)
|
2023-12-11 16:27:39 +08:00
|
|
|
{
|
|
|
|
|
if (pool == NULL || sess == NULL)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-13 17:56:27 +08:00
|
|
|
sess->next_free_ptr = pool->sess;
|
2023-12-11 16:27:39 +08:00
|
|
|
pool->sess = sess;
|
2023-12-11 16:35:26 +08:00
|
|
|
pool->count++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t session_pool_get_count(struct session_pool *pool)
|
|
|
|
|
{
|
|
|
|
|
if (pool == NULL)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pool->count;
|
2023-12-11 16:27:39 +08:00
|
|
|
}
|