2024-06-25 10:32:51 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2024-08-20 18:43:51 +08:00
|
|
|
#include "session_private.h"
|
2023-12-11 16:27:39 +08:00
|
|
|
#include "session_pool.h"
|
|
|
|
|
|
|
|
|
|
struct session_pool
|
|
|
|
|
{
|
2024-07-25 18:32:14 +08:00
|
|
|
uint64_t capacity;
|
|
|
|
|
uint64_t available;
|
2024-09-13 15:44:46 +08:00
|
|
|
struct session_list free_list;
|
2023-12-11 16:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
2024-07-25 18:32:14 +08:00
|
|
|
struct session_pool *session_pool_new(uint64_t capacity)
|
2023-12-11 16:27:39 +08:00
|
|
|
{
|
2024-07-25 18:32:14 +08:00
|
|
|
struct session_pool *pool = (struct session_pool *)calloc(1, sizeof(struct session_pool) + capacity * sizeof(struct session));
|
2023-12-11 16:27:39 +08:00
|
|
|
if (pool == NULL)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2024-07-25 18:32:14 +08:00
|
|
|
pool->available = 0;
|
|
|
|
|
pool->capacity = capacity;
|
2024-09-13 15:44:46 +08:00
|
|
|
TAILQ_INIT(&pool->free_list);
|
2023-12-11 16:27:39 +08:00
|
|
|
|
2024-03-29 17:45:41 +08:00
|
|
|
struct session *array = (struct session *)(pool + 1);
|
2024-07-25 18:32:14 +08:00
|
|
|
for (uint64_t i = 0; i < capacity; i++)
|
2023-12-11 16:27:39 +08:00
|
|
|
{
|
2024-03-29 17:45:41 +08:00
|
|
|
struct session *sess = &array[i];
|
2024-09-13 15:44:46 +08:00
|
|
|
TAILQ_INSERT_TAIL(&pool->free_list, sess, free_tqe);
|
2024-07-25 18:32:14 +08:00
|
|
|
pool->available++;
|
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)
|
|
|
|
|
{
|
2024-09-13 15:44:46 +08:00
|
|
|
struct session *sess;
|
|
|
|
|
while ((sess = TAILQ_FIRST(&pool->free_list)))
|
2024-03-29 17:45:41 +08:00
|
|
|
{
|
2024-09-13 15:44:46 +08:00
|
|
|
TAILQ_REMOVE(&pool->free_list, sess, free_tqe);
|
2024-07-25 18:32:14 +08:00
|
|
|
pool->available--;
|
2024-03-29 17:45:41 +08:00
|
|
|
}
|
|
|
|
|
|
2023-12-11 16:27:39 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-13 15:44:46 +08:00
|
|
|
struct session *sess = TAILQ_FIRST(&pool->free_list);
|
|
|
|
|
if (sess)
|
2023-12-11 16:27:39 +08:00
|
|
|
{
|
2024-09-13 15:44:46 +08:00
|
|
|
TAILQ_REMOVE(&pool->free_list, sess, free_tqe);
|
|
|
|
|
pool->available--;
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-13 15:44:46 +08:00
|
|
|
TAILQ_INSERT_TAIL(&pool->free_list, sess, free_tqe);
|
2024-07-25 18:32:14 +08:00
|
|
|
pool->available++;
|
2023-12-11 16:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-25 18:32:14 +08:00
|
|
|
const struct session *session_pool_get0(const struct session_pool *pool, uint64_t idx)
|
|
|
|
|
{
|
|
|
|
|
if (pool == NULL || idx >= pool->capacity)
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct session *array = (struct session *)(pool + 1);
|
|
|
|
|
return &array[idx];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t session_pool_available_num(const struct session_pool *pool)
|
2023-12-11 16:35:26 +08:00
|
|
|
{
|
|
|
|
|
if (pool == NULL)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-25 18:32:14 +08:00
|
|
|
return pool->available;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t session_pool_capacity_size(const struct session_pool *pool)
|
|
|
|
|
{
|
|
|
|
|
if (pool == NULL)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pool->capacity;
|
|
|
|
|
}
|