2
3
4
5
8#include <gmock/gmock.h>
9#include <Core/Memory/MemoryPool.h>
42
43
44
50
51
52
56
57
58
62
63
67
68
73
74
84 uint64_t* d0 =
reinterpret_cast<uint64_t*>(a);
89 uint64_t* d1 =
reinterpret_cast<uint64_t*>(a);
94 uint64_t* d2 =
reinterpret_cast<uint64_t*>(a);
95 EXPECT_EQ(d2,
nullptr);
99
100
105 EXPECT_EQ(Spices::MemoryPool::AlignUp(1), 8);
106 EXPECT_EQ(Spices::MemoryPool::AlignUp(127), 128);
108 EXPECT_EQ(Spices::MemoryPool::AlignUp(129), 144);
109 EXPECT_EQ(Spices::MemoryPool::AlignUp(1023), 1024);
111 EXPECT_EQ(Spices::MemoryPool::AlignUp(1025), 1152);
112 EXPECT_EQ(Spices::MemoryPool::AlignUp(8 * 1024 - 1), 8 * 1024);
114 EXPECT_EQ(Spices::MemoryPool::AlignUp(8 * 1024 + 1), 9 * 1024);
115 EXPECT_EQ(Spices::MemoryPool::AlignUp(64 * 1024 - 1), 64 * 1024);
117 EXPECT_EQ(Spices::MemoryPool::AlignUp(64 * 1024 + 1), 72 * 1024);
118 EXPECT_EQ(Spices::MemoryPool::AlignUp(256 * 1024 - 1), 256 * 1024);
120 EXPECT_EQ(Spices::MemoryPool::AlignUp(257 * 1024 + 1), 264 * 1024);
122 EXPECT_EQ(Spices::MemoryPool::AlignUp(1024 * 1024 + 1), 1032 * 1024);
126
127
132 EXPECT_EQ(Spices::MemoryPool::Index(1), 0);
133 EXPECT_EQ(Spices::MemoryPool::Index(16), 1);
134 EXPECT_EQ(Spices::MemoryPool::Index(128), 15);
135 EXPECT_EQ(Spices::MemoryPool::Index(256 * 1024), 207);
139
140
145 std::array<MemoryPoolTest*, n> objects;
146 for (size_t i = 0; i < n; i++)
148 MemoryPoolTest* a =
new(Spices::MemoryPool::Alloc(
sizeof(MemoryPoolTest)))MemoryPoolTest;
150 EXPECT_EQ(std::get<0>(a->m_Tuple), 1.0f);
151 EXPECT_EQ(std::get<1>(a->m_Tuple), 2);
152 EXPECT_EQ(std::get<2>(a->m_Tuple),
nullptr);
157 std::array<MemoryPoolTest2*, n2> object2s;
158 for (size_t i = 0; i < n2; i++)
160 MemoryPoolTest2* b =
new(Spices::MemoryPool::Alloc(
sizeof(MemoryPoolTest2)))MemoryPoolTest2;
165 for (size_t i = 0; i < n; i++)
167 Spices::MemoryPool::Free(objects[i]);
170 for (size_t i = 0; i < n2; i++)
172 Spices::MemoryPool::Free(object2s[i]);
177
178
183 static constexpr int nThread = 1;
184 static constexpr int nCount = 500000;
189 std::vector<std::thread> threads;
190 for (
int i = 0; i < nThread; i++)
192 std::thread t1([&]() {
193 std::vector<MemoryPoolTest*> objects;
194 objects.resize(nCount);
196 for (
int j = 0; j < nCount; j++)
200 objects[j] = std::move(p);
203 for (
int j = 0; j < nCount; j++)
210 threads.push_back(std::move(t1));
213 for (
int i = 0; i < nThread; i++)
222 std::vector<std::thread> threads;
223 for (
int i = 0; i < nThread; i++)
225 std::thread t1([&]() {
226 std::vector<MemoryPoolTest*> objects;
227 objects.resize(nCount);
229 for (
int j = 0; j < nCount; j++)
231 MemoryPoolTest* p =
static_cast<MemoryPoolTest*>(malloc(4 *
sizeof(MemoryPoolTest)));
234 objects[j] = std::move(p);
237 for (
int j = 0; j < nCount; j++)
246 threads.push_back(std::move(t1));
249 for (
int i = 0; i < nThread; i++)
258 std::vector<std::thread> threads;
259 for (
int i = 0; i < nThread; i++)
261 std::thread t1([&]() {
262 std::vector<MemoryPoolTest*> objects;
263 objects.resize(nCount);
265 for (
int j = 0; j < nCount; j++)
267 MemoryPoolTest* p =
static_cast<MemoryPoolTest*>(Spices::MemoryPool::Alloc(4 *
sizeof(MemoryPoolTest)));
270 objects[j] = std::move(p);
273 for (
int j = 0; j < nCount; j++)
282 threads.push_back(std::move(t1));
285 for (
int i = 0; i < nThread; i++)
293
294
299 static constexpr int nThread = 10;
300 static constexpr int nCount = 50000;
305 std::vector<std::thread> threads;
306 for (
int i = 0; i < nThread; i++)
308 std::thread t1([&]() {
309 std::vector<MemoryPoolTest*> objects;
310 objects.resize(nCount);
312 for (
int j = 0; j < nCount; j++)
316 objects[j] = std::move(p);
319 for (
int j = 0; j < nCount; j++)
326 threads.push_back(std::move(t1));
329 for (
int i = 0; i < nThread; i++)
338 std::vector<std::thread> threads;
339 for (
int i = 0; i < nThread; i++)
341 std::thread t1([&]() {
342 std::vector<MemoryPoolTest*> objects;
343 objects.resize(nCount);
345 for (
int j = 0; j < nCount; j++)
347 MemoryPoolTest* p =
static_cast<MemoryPoolTest*>(malloc(4 *
sizeof(MemoryPoolTest)));
350 objects[j] = std::move(p);
353 for (
int j = 0; j < nCount; j++)
362 threads.push_back(std::move(t1));
365 for (
int i = 0; i < nThread; i++)
374 std::vector<std::thread> threads;
375 for (
int i = 0; i < nThread; i++)
377 std::thread t1([&]() {
378 std::vector<MemoryPoolTest*> objects;
379 objects.resize(nCount);
381 for (
int j = 0; j < nCount; j++)
383 MemoryPoolTest* p =
static_cast<MemoryPoolTest*>(Spices::MemoryPool::Alloc(4 *
sizeof(MemoryPoolTest)));
386 objects[j] = std::move(p);
389 for (
int j = 0; j < nCount; j++)
398 threads.push_back(std::move(t1));
401 for (
int i = 0; i < nThread; i++)
409
410
415 static constexpr int nThread = 1;
416 static constexpr int nCount = 500000;
417 static constexpr int maxBytes = 1024;
422 std::vector<std::thread> threads;
423 for (
int i = 0; i < nThread; i++)
425 std::thread t1([&]() {
426 std::vector<
void*> objects;
427 objects.resize(nCount);
429 for (
int j = 0; j < nCount; j++)
431 const size_t bytes = maxBytes * std::rand() /
float(RAND_MAX);
432 void* p = malloc(bytes);
434 objects[j] = std::move(p);
437 for (
int j = 0; j < nCount; j++)
439 void* p = objects[j];
445 threads.push_back(std::move(t1));
448 for (
int i = 0; i < nThread; i++)
457 std::vector<std::thread> threads;
458 for (
int i = 0; i < nThread; i++)
460 std::thread t1([&]() {
461 std::vector<
void*> objects;
462 objects.resize(nCount);
464 for (
int j = 0; j < nCount; j++)
466 size_t bytes = maxBytes * std::rand() /
float(RAND_MAX);
467 bytes = std::max((size_t)8, bytes);
468 void* p = Spices::MemoryPool::Alloc(bytes);
470 objects[j] = std::move(p);
473 for (
int j = 0; j < nCount; j++)
475 void* p = objects[j];
481 threads.push_back(std::move(t1));
484 for (
int i = 0; i < nThread; i++)
492
493
498 static constexpr int nThread = 10;
499 static constexpr int nCount = 50000;
500 static constexpr int maxBytes = 1024;
505 std::vector<std::thread> threads;
506 for (
int i = 0; i < nThread; i++)
508 std::thread t1([&]() {
509 std::vector<
void*> objects;
510 objects.resize(nCount);
512 for (
int j = 0; j < nCount; j++)
514 const size_t bytes = maxBytes * std::rand() /
float(RAND_MAX);
515 void* p = malloc(bytes);
517 objects[j] = std::move(p);
520 for (
int j = 0; j < nCount; j++)
522 void* p = objects[j];
528 threads.push_back(std::move(t1));
531 for (
int i = 0; i < nThread; i++)
540 std::vector<std::thread> threads;
541 for (
int i = 0; i < nThread; i++)
543 std::thread t1([&]() {
544 std::vector<
void*> objects;
545 objects.resize(nCount);
547 for (
int j = 0; j < nCount; j++)
549 size_t bytes = maxBytes * std::rand() /
float(RAND_MAX);
550 bytes = std::max((size_t)8, bytes);
551 void* p = Spices::MemoryPool::Alloc(bytes);
553 objects[j] = std::move(p);
556 for (
int j = 0; j < nCount; j++)
558 void* p = objects[j];
564 threads.push_back(std::move(t1));
567 for (
int i = 0; i < nThread; i++)
577
578
584 constexpr int nCounts_0 = 1 * 1000000 * 5;
630 size_t bytes =
rand * (8 * 1024 * 1024 - 1024 * 1024) + 1024 * 1024 + 1;
#define DELEGATE_ONE_PARAM(name, p0)
Use this macro to instance a Delegate Class. One Parameter Specific.
#define DELEGATE_THREE_PARAM(name, p0, p1, p2)
Use this macro to instance a Delegate Class. Three Parameter Specific.
#define DELEGATE_SIX_PARAM(name, p0, p1, p2, p3, p4, p5)
Use this macro to instance a Delegate Class. Six Parameter Specific.
#define DELEGATE_FIVE_PARAM(name, p0, p1, p2, p3, p4)
Use this macro to instance a Delegate Class. Five Parameter Specific.
#define DELEGATE_NONE_PARAM(name)
Use this macro to instance a Delegate Class. None Parameter Specific.
#define DELEGATE_SEVEN_PARAM(name, p0, p1, p2, p3, p4, p5, p6)
Use this macro to instance a Delegate Class. Seven Parameter Specific.
#define DELEGATE_EIGHT_PARAM(name, p0, p1, p2, p3, p4, p5, p6, p7)
Use this macro to instance a Delegate Class. Eight Parameter Specific.
#define DELEGATE_TWO_PARAM(name, p0, p1)
Use this macro to instance a Delegate Class. Two Parameter Specific.
#define DELEGATE_FOUR_PARAM(name, p0, p1, p2, p3)
Use this macro to instance a Delegate Class. Four Parameter Specific.
#define DELEGATE_NINE_PARAM(name, p0, p1, p2, p3, p4, p5, p6, p7, p8)
Use this macro to instance a Delegate Class. Nine Parameter Specific.
#define SPICESTEST_PROFILE_SCOPE_LINE2(name, line)
#define SPICESTEST_PROFILE_SCOPE(name)
#define SPICESTEST_PROFILE_END_SESSION()
#define SPICESTEST_PROFILE_BEGIN_SESSION(name, filepath)
#define SPICESTEST_PROFILE_SCOPE_LINE(name, line)
#define SPICESTEST_PROFILE_FUNCTION()
void Test0(int a, int b)
Basic Class Function.
void Test(float f)
Basic Override Class Function.
void Test()
Basic Override Class Function.
static void Test1()
Static Class Function.
void SetUp() override
The interface is inherited from testing::Test. Registry on Initialize.
void TearDown() override
Testing class TearDown function.
InstrumentationTimer(const char *name)
std::chrono::time_point< std::chrono::steady_clock > m_StartTimepoint
Instrumentor(Instrumentor &&)=delete
static Instrumentor & Get()
void BeginSession(const std::string &name, const std::string &filepath="results.json")
void WriteProfile(const ProfileResult &result)
InstrumentationSession * m_CurrentSession
std::ofstream m_OutputStream
Instrumentor(const Instrumentor &)=delete
void InternalEndSession()
MemoryPoolTest2(const MemoryPoolTest2 &)=delete
MemoryPoolTest2 & operator=(const MemoryPoolTest2 &)=delete
MemoryPoolTest2()=default
static constexpr size_t y
std::array< std::array< int, x >, y > datas
static constexpr size_t x
MemoryPoolTest(const MemoryPoolTest &)=delete
MemoryPoolTest & operator=(const MemoryPoolTest &)=delete
std::tuple< int, float, void * > m_Tuple
void SetUp() override
The interface is inherited from testing::Test. Registry on Initialize.
static constexpr size_t n2
Iter counts 2.
void TearDown() override
The interface is inherited from testing::Test. Call before Destructor.
static constexpr size_t n
Iter counts.
The interface is inherited from testing::Test. Registry on Initialize.
void SetUp() override
The interface is inherited from testing::Test. Registry on Initialize.
std::unique_ptr< Spices::ObjectPool< Object > > m_ObjectPool
ObjectPool.
void TearDown() override
The interface is inherited from testing::Test. Call before Destructor.
static constexpr size_t n
ObjectPool objects number.
The interface is inherited from testing::Test. Registry on Initialize.
virtual ~Object()=default
void SetUp() override
The interface is inherited from testing::Test. Registry on Initialize.
Spices::PageCache pc
PageCache.
static constexpr size_t n
Test iter number.
void TearDown() override
The interface is inherited from testing::Test. Call before Destructor.
The interface is inherited from testing::Test. Registry on Initialize.
ThreadCacheTest & operator=(const ThreadCacheTest &)=delete
std::tuple< int, float, void * > m_Tuple
ThreadCacheTest(const ThreadCacheTest &)=delete
static constexpr size_t n
Iter counts.
void TearDown() override
The interface is inherited from testing::Test. Call before Destructor.
void SetUp() override
The interface is inherited from testing::Test. Registry on Initialize.
Spices::ThreadCache tc
ThreadCache.
The interface is inherited from testing::Test. Registry on Initialize.
void Test(float f)
Basic Override Class Function.
static void Test1()
Static Class Function.
void Test()
Basic Override Class Function.
void TearDown() override
Testing class TearDown function.
std::vector< scl::directed_acyclic_node > m_Nodes
void SetUp() override
Testing class initialize function.
scl::directed_acyclic_graph m_DAG
The interface is inherited from testing::Test. Registy on Initialize.
void SetUp() override
The interface is inherited from testing::Test. Registry on Initialize.
scl::free_list m_FreeList
void TearDown() override
The interface is inherited from testing::Test. Call before Destructor.
The interface is inherited from testing::Test. Registry on Initialize.
scl::kd_tree< 2 > m_KDTree
Create a KDTree with 2 dimensions.
void TearDown() override
Testing class TearDown function.
void SetUp() override
Testing class initialize function.
The interface is inherited from testing::Test. Registry on Initialize.
scl::linked_unordered_map< int, std::string > c1
scl::linked_unordered_map< float, std::string > c2
scl::linked_unordered_map< std::string, std::string > c0
void SetUp() override
Testing class initialize function.
void TearDown() override
Testing class TearDown function.
The interface is inherited from testing::Test. Registry on Initialize.
void SetUp() override
The interface is inherited from testing::Test. Registry on Initialize.
void TearDown() override
Testing class TearDown function.
scl::runtime_memory_block m0
Unit Test for RuntimeMemoryBlock.
void TearDown() override
The interface is inherited from testing::Test. Call before Destructor.
void SetUp() override
The interface is inherited from testing::Test. Registry on Initialize.
scl::span_list m_SpanList
The interface is inherited from testing::Test. Registry on Initialize.
static std::string GetClassString(ClassType t)
Get Class Name as string.
Class Static Function Library.
static bool FileLibrary_Size(const FileHandle *handle, uint64_t *out_size)
Calculate The file size.
static bool FileLibrary_Read(const FileHandle *handle, uint64_t data_size, void *out_data, uint64_t *out_bytes_read)
Read Specific size of data form the current file handle pointer, and move pointer the same size.
static bool FileLibrary_Write_Line(const FileHandle *handle, const char *text)
Write one line data to the file handle pointer.
static bool FileLibrary_Write(const FileHandle *handle, uint64_t data_size, const void *data, uint64_t *out_bytes_written)
Write given data to the file handle pointer.
static bool FileLibrary_Open(const char *path, FileModes mode, bool binary, FileHandle *out_handle)
Open the file using given string.
static void FileLibrary_Close(FileHandle *handle)
Close the file by the file handle.
static bool FileLibrary_Delete(const char *filePath)
Delete a file from disk.
static bool FileLibrary_CopyFile(std::string srcFilePath, std::string dstFilePath)
Copy a file to dst path.
static bool FileLibrary_Read_Line(const FileHandle *handle, uint64_t max_length, char *line_buf, uint64_t *out_line_length)
Read one line from the current file handle pointer, and move pointer the same size.
static bool FileLibrary_Exists(const char *path)
Determine whether the given string is existing a file.
static bool FileLibrary_Read_all_bytes(const FileHandle *handle, char *out_bytes, uint64_t *out_bytes_read)
Read all data form the current file handle pointer.
File Static Function Library.
static void Init()
Init Log.
static void ShutDown()
Shutdown Log.
static void Free(void *ptr)
Free memory entry point.
static void *& PointerSpace(void *obj)
Get object first 4/8 bytes as a pointer.
Page memory cache. Third level of memory allocator.
static bool CloseProcess(const char *processName)
Close a Process with command.
static bool OpenProcess(const char *processPath, const char *commandLine="")
Open a Process with command.
static float ProcessMemoryInUsed()
Get this Process Memory used( GB ).
Process Static Function Library.
static bool StringsEqual(const char *str0, const char *str1)
Determine if the strings given are equal. Platform Specific.
String Static Function Library.
Thread memory cache. First level of memory allocator.
static bool SetThreadName(const std::string &name)
Set Thread name.
Thread Static Function Library.
behave_state_list. wrapper of combing all state behaves.
bool Empty() const
Determine if this list is empty.
void Push(void *obj)
Recycle a object memory to this free list.
void *& Begin()
Get current pointer (no const & reference version).
void *& End()
Get end pointer (no const & reference version).
Free list for memory pool.
The kd_tree with K dimensions container Class. K the number of dimensions. Every node in the tree is ...
The container combines hashmap and list together. Used in the case that we want iter a hashmap in ord...
void build()
Malloc a memory to begin_.
void add_element(const std::string &name, const std::string &type)
Add a element to object_, means a memory block will be occupied with given param type.
runtime_memory_block()=default
Constructor Function.
void * get_addr() const
Get the begin_.
The container is wrapper of a continue memory block. Used in Material::BuildMaterial(),...
Bidirectional cyclic linked list for span.
span * m_Prev
previous span.
Used for manage multiple page memory.
This Class is similar to std::vector, the difference between that is this one allocates memory by mal...
int main(int argc, char **argv)
The Entry of SpicesTest.
constexpr auto CleanupOutputString(const char(&expr)[N], const char(&remove)[K])
TEST_F(free_list_test, PushPop)
Testing scl::free_list::Push/Pop.
TEST_F(runtime_memory_block_test, Initialize)
Testing if initialize successfully.
TEST_F(directed_acyclic_graph_test, Addnode)
Testing if add node successfully.
TEST_F(span_list_test, Initialize)
Testing scl::span_list::Initialize.
TEST_F(linked_unordered_map_test, Initialize)
Testing if initialize successfully.
TEST_F(kd_tree_test, Insert)
Testing if Insert successfully.
void DelegateTestT(Args ...args)
Template Function.
TEST_F(MemoryPool_test, PointerSpace)
Testing Spices::MemoryPool::PointerSpace.
TEST(tuple_test, IterTuple)
Testing std::tuple Helper Function.
TEST_F(ObjectPool_test, Initialize)
Testing Spices::ObjectPool::Initialize.
TEST_F(Delegate_test, Bind)
Testing if bind successfully.
TEST_F(ThreadCache_test, AllocateDeallocate)
Testing Spices::ThreadCache::Allocate/Deallocate.
TEST_F(PageCache_test, NewSpan)
Testing Spices::PageCache::NewSpan.
@ FILE_MODE_READ
model : read
@ FILE_MODE_WRITE
model : write
void IterTuple(Tuple &tuple, Function &&f)
Iter a tuple.
std::chrono::microseconds ElapsedTime
FloatingPointMicroseconds Start
void * handle
FILE* handle. Need cast while use.
bool is_valid
Is this handle Valid.
This Struct is FILE* handle pointer Wrapper.
bool operator==(const String2 &other) const
equal operation.
bool operator==(const UInt2 &other) const
equal operation.
UInt2(const uint32_t &x, const uint32_t &y)
Constructor Function.