This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
stellar-stellar/src/session/session_pool.cpp

79 lines
1.4 KiB
C++
Raw Normal View History

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;
};
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++)
{
pool->sess[i].next_free_ptr = &pool->sess[i + 1];
2023-12-11 16:27:39 +08:00
}
pool->sess[count - 1].next_free_ptr = NULL;
2023-12-11 16:27:39 +08:00
return pool;
}
void session_pool_free(struct session_pool *pool)
2023-12-11 16:27:39 +08:00
{
if (pool)
{
free(pool);
pool = NULL;
}
}
2023-12-11 16:35:26 +08:00
struct session *session_pool_alloc(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;
}
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;
}
2023-12-11 16:35:26 +08:00
void session_pool_free(struct session_pool *pool, struct session *sess)
2023-12-11 16:27:39 +08:00
{
if (pool == NULL || sess == NULL)
{
return;
}
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
}