Skip to content

Commit 0a165ea

Browse files
committed
Refactored by using TbxMemPoolAllocateAuto() in operator new.
1 parent 1e81e33 commit 0a165ea

File tree

1 file changed

+5
-21
lines changed

1 file changed

+5
-21
lines changed

source/extra/cplusplus/tbxcxx.cpp

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,11 @@ void * operator new(size_t size)
7979
{
8080
void * result;
8181

82-
/* Attempt to allocate a block from the best fitting memory pool. */
83-
result = TbxMemPoolAllocate(size);
84-
/* Was the allocation not successful? */
85-
if (result == nullptr)
86-
{
87-
/* The allocation failed. This can have two reasons:
88-
* 1. A memory pool for the requested size hasn't yet been created.
89-
* 2. The memory pool for the requested size has no more free blocks.
90-
* Both situations can be solved by calling TbxMemPoolCreate(), as this
91-
* function automatically extends a memory pool, if it was created before.
92-
* Note that there is no need to check the return value, because we will
93-
* attempt to allocate again right afterwards. We can catch the error
94-
* there in case the allocation fails.
95-
*/
96-
(void)TbxMemPoolCreate(1U, size);
97-
98-
/* Assuming sufficient heap was available, the memory pool was extended.
99-
* Attempt to allocate the block again.
100-
*/
101-
result = TbxMemPoolAllocate(size);
102-
}
82+
/* Attempt to allocate a block from a memory pool with the same size. If non-existant,
83+
* automatically create the memory pool. If no more free blocks available in the
84+
* memory pool, automatically expand the memory pool by adding one more block.
85+
*/
86+
result = TbxMemPoolAllocateAuto(size);
10387
/* Verify the allocation result. */
10488
if (result == nullptr)
10589
{

0 commit comments

Comments
 (0)