Add session pool

This commit is contained in:
luwenpeng
2023-12-11 16:27:39 +08:00
parent 37aeb10e59
commit 015b4e7695
3 changed files with 139 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
#include "session_pool.h"
struct session_pool
{
uint64_t count;
struct session *sess;
};
struct session_pool *session_pool_create(uint64_t count)
{
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++)
{
pool->sess[i].next = &pool->sess[i + 1];
}
pool->sess[count - 1].next = NULL;
return pool;
}
void session_pool_destroy(struct session_pool *pool)
{
if (pool)
{
free(pool);
pool = NULL;
}
}
struct session *session_pool_pop(struct session_pool *pool)
{
if (pool == NULL)
{
return NULL;
}
struct session *sess = pool->sess;
if (sess == NULL)
{
return NULL;
}
pool->sess = sess->next;
sess->next = NULL;
return sess;
}
void session_pool_push(struct session_pool *pool, struct session *sess)
{
if (pool == NULL || sess == NULL)
{
return;
}
sess->next = pool->sess;
pool->sess = sess;
}