2024-10-23 10:01:20 +08:00
|
|
|
#include <assert.h>
|
2024-06-25 10:32:51 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
2024-09-19 16:18:54 +08:00
|
|
|
#include "session_internal.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;
|
2024-10-23 10:01:20 +08:00
|
|
|
uint64_t used;
|
|
|
|
|
uint64_t free;
|
|
|
|
|
struct session_queue 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-10-23 10:01:20 +08:00
|
|
|
pool->used = 0;
|
|
|
|
|
pool->free = 0;
|
2024-07-25 18:32:14 +08:00
|
|
|
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-10-23 10:01:20 +08:00
|
|
|
pool->free++;
|
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-10-23 10:01:20 +08:00
|
|
|
pool->free--;
|
2024-03-29 17:45:41 +08:00
|
|
|
}
|
2024-10-23 10:01:20 +08:00
|
|
|
assert(pool->free == 0);
|
|
|
|
|
assert(pool->used == 0);
|
2024-03-29 17:45:41 +08:00
|
|
|
|
2023-12-11 16:27:39 +08:00
|
|
|
free(pool);
|
|
|
|
|
pool = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 19:16:06 +08:00
|
|
|
struct session *session_pool_acquire_sessoin(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);
|
2024-10-23 10:01:20 +08:00
|
|
|
pool->free--;
|
|
|
|
|
pool->used++;
|
2023-12-11 16:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sess;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 19:16:06 +08:00
|
|
|
void session_pool_release_sessoin(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-10-23 10:01:20 +08:00
|
|
|
pool->free++;
|
|
|
|
|
pool->used--;
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
uint64_t session_pool_get_free_num(const struct session_pool *pool)
|
2023-12-11 16:35:26 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
return pool->free;
|
2024-07-25 18:32:14 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-23 10:01:20 +08:00
|
|
|
uint64_t session_pool_get_used_num(const struct session_pool *pool)
|
2024-07-25 18:32:14 +08:00
|
|
|
{
|
2024-10-23 10:01:20 +08:00
|
|
|
return pool->used;
|
2024-07-25 18:32:14 +08:00
|
|
|
}
|