feature: session pool support forearch session by index

This commit is contained in:
luwenpeng
2024-07-25 18:32:14 +08:00
parent f43da9002b
commit 5fdf0e2aa4
3 changed files with 54 additions and 26 deletions

View File

@@ -5,25 +5,28 @@
struct session_pool
{
uint64_t count;
uint64_t capacity;
uint64_t available;
struct list_head free_queue;
};
struct session_pool *session_pool_new(uint64_t count)
struct session_pool *session_pool_new(uint64_t capacity)
{
struct session_pool *pool = (struct session_pool *)calloc(1, sizeof(struct session_pool) + count * sizeof(struct session));
struct session_pool *pool = (struct session_pool *)calloc(1, sizeof(struct session_pool) + capacity * sizeof(struct session));
if (pool == NULL)
{
return NULL;
}
pool->available = 0;
pool->capacity = capacity;
INIT_LIST_HEAD(&pool->free_queue);
struct session *array = (struct session *)(pool + 1);
for (uint64_t i = 0; i < count; i++)
for (uint64_t i = 0; i < capacity; i++)
{
struct session *sess = &array[i];
list_add_tail(&sess->free, &pool->free_queue);
pool->count++;
pool->available++;
}
return pool;
@@ -37,7 +40,7 @@ void session_pool_free(struct session_pool *pool)
{
struct session *sess = list_first_entry(&pool->free_queue, struct session, free);
list_del(&sess->free);
pool->count--;
pool->available--;
}
free(pool);
@@ -59,7 +62,7 @@ struct session *session_pool_pop(struct session_pool *pool)
struct session *sess = list_first_entry(&pool->free_queue, struct session, free);
list_del(&sess->free);
pool->count--;
pool->available--;
return sess;
}
@@ -72,15 +75,36 @@ void session_pool_push(struct session_pool *pool, struct session *sess)
}
list_add_tail(&sess->free, &pool->free_queue);
pool->count++;
pool->available++;
}
uint64_t session_pool_get_count(struct session_pool *pool)
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)
{
if (pool == NULL)
{
return 0;
}
return pool->count;
}
return pool->available;
}
uint64_t session_pool_capacity_size(const struct session_pool *pool)
{
if (pool == NULL)
{
return 0;
}
return pool->capacity;
}