diff --git a/src/session/gtest_session_pool.cpp b/src/session/gtest_session_pool.cpp new file mode 100644 index 0000000..9e56cfa --- /dev/null +++ b/src/session/gtest_session_pool.cpp @@ -0,0 +1,49 @@ +#include + +#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(); +} diff --git a/src/session/session_pool.cpp b/src/session/session_pool.cpp new file mode 100644 index 0000000..195a989 --- /dev/null +++ b/src/session/session_pool.cpp @@ -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; +} \ No newline at end of file diff --git a/src/session/session_pool.h b/src/session/session_pool.h new file mode 100644 index 0000000..a572e2d --- /dev/null +++ b/src/session/session_pool.h @@ -0,0 +1,24 @@ +#ifndef _SESSION_POOL_H +#define _SESSION_POOL_H + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include + +#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