66 lines
1.2 KiB
C++
66 lines
1.2 KiB
C++
|
|
#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;
|
||
|
|
}
|