Add session pool
This commit is contained in:
49
src/session/gtest_session_pool.cpp
Normal file
49
src/session/gtest_session_pool.cpp
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include "session_pool.h"
|
||||||
|
|
||||||
|
TEST(SESSION_POOL, POP_PUSH)
|
||||||
|
{
|
||||||
|
struct session *sess1 = NULL;
|
||||||
|
struct session *sess2 = NULL;
|
||||||
|
struct session *sess3 = NULL;
|
||||||
|
struct session *sess4 = NULL;
|
||||||
|
struct session_pool *sess_pool = NULL;
|
||||||
|
|
||||||
|
sess_pool = session_pool_create(3);
|
||||||
|
EXPECT_TRUE(sess_pool != NULL);
|
||||||
|
|
||||||
|
sess1 = session_pool_pop(sess_pool);
|
||||||
|
EXPECT_TRUE(sess1 != NULL);
|
||||||
|
sess2 = session_pool_pop(sess_pool);
|
||||||
|
EXPECT_TRUE(sess2 != NULL);
|
||||||
|
sess3 = session_pool_pop(sess_pool);
|
||||||
|
EXPECT_TRUE(sess3 != NULL);
|
||||||
|
sess4 = session_pool_pop(sess_pool);
|
||||||
|
EXPECT_TRUE(sess4 == NULL);
|
||||||
|
|
||||||
|
session_pool_push(sess_pool, sess1);
|
||||||
|
session_pool_push(sess_pool, sess2);
|
||||||
|
session_pool_push(sess_pool, sess3);
|
||||||
|
|
||||||
|
sess1 = session_pool_pop(sess_pool);
|
||||||
|
EXPECT_TRUE(sess1 != NULL);
|
||||||
|
sess2 = session_pool_pop(sess_pool);
|
||||||
|
EXPECT_TRUE(sess2 != NULL);
|
||||||
|
sess3 = session_pool_pop(sess_pool);
|
||||||
|
EXPECT_TRUE(sess3 != NULL);
|
||||||
|
sess4 = session_pool_pop(sess_pool);
|
||||||
|
EXPECT_TRUE(sess4 == NULL);
|
||||||
|
|
||||||
|
session_pool_push(sess_pool, sess1);
|
||||||
|
session_pool_push(sess_pool, sess2);
|
||||||
|
session_pool_push(sess_pool, sess3);
|
||||||
|
|
||||||
|
session_pool_destroy(sess_pool);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
|
return RUN_ALL_TESTS();
|
||||||
|
}
|
||||||
66
src/session/session_pool.cpp
Normal file
66
src/session/session_pool.cpp
Normal 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;
|
||||||
|
}
|
||||||
24
src/session/session_pool.h
Normal file
24
src/session/session_pool.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#ifndef _SESSION_POOL_H
|
||||||
|
#define _SESSION_POOL_H
|
||||||
|
|
||||||
|
#ifdef __cpluscplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "session.h"
|
||||||
|
|
||||||
|
struct session_pool;
|
||||||
|
struct session_pool *session_pool_create(uint64_t count);
|
||||||
|
void session_pool_destroy(struct session_pool *pool);
|
||||||
|
|
||||||
|
struct session *session_pool_pop(struct session_pool *pool);
|
||||||
|
void session_pool_push(struct session_pool *pool, struct session *sess);
|
||||||
|
|
||||||
|
#ifdef __cpluscplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user