Skip to content

Commit

Permalink
use blocksize
Browse files Browse the repository at this point in the history
  • Loading branch information
bertmelis authored Mar 1, 2024
1 parent 621460f commit b7851f1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/Fixed.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ class Fixed {

std::size_t freeMemory() {
const std::lock_guard<std::mutex> lockGuard(_mutex);
std::size_t adjustedBlocksize = sizeof(sizeof(unsigned char*)) > sizeof(blocksize) ? sizeof(sizeof(unsigned char*)) : sizeof(blocksize);
unsigned char* i = _head;
std::size_t retVal = 0;
while (i) {
retVal += adjustedBlocksize;
retVal += blocksize;
i = reinterpret_cast<unsigned char**>(i)[0];
}
return retVal;
Expand Down
12 changes: 5 additions & 7 deletions test/test_FixInt/test_FixInt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ void mallocFull() {
void freePartial() {
const size_t nrBlocks = 4;
const size_t blocksize = sizeof(int);
size_t adjustedBlocksize = std::max(blocksize, blockHeadersize);
MemoryPool::Fixed<nrBlocks, blocksize> pool;

int* int1 = reinterpret_cast<int*>(pool.malloc());
Expand All @@ -67,22 +66,21 @@ void freePartial() {
(void) int1;
pool.free(int2);
pool.print();
TEST_ASSERT_EQUAL_UINT(1 * adjustedBlocksize, pool.freeMemory());
TEST_ASSERT_EQUAL_UINT(1 * blocksize, pool.freeMemory());
pool.free(int4);
pool.print();
TEST_ASSERT_EQUAL_UINT(2 * adjustedBlocksize, pool.freeMemory());
TEST_ASSERT_EQUAL_UINT(2 * blocksize, pool.freeMemory());
int* int5 = reinterpret_cast<int*>(pool.malloc(blocksize));
TEST_ASSERT_NOT_NULL(int5);
pool.print();
(void) int5;

TEST_ASSERT_EQUAL_UINT(1 * adjustedBlocksize, pool.freeMemory());
TEST_ASSERT_EQUAL_UINT(1 * blocksize, pool.freeMemory());
}

void freeEmpty() {
const size_t nrBlocks = 4;
const size_t blocksize = sizeof(int);
size_t adjustedBlocksize = std::max(blocksize, blockHeadersize);
const size_t blocksize = sizeof(int););
MemoryPool::Fixed<nrBlocks, blocksize> pool;

int* int1 = reinterpret_cast<int*>(pool.malloc());
Expand All @@ -100,7 +98,7 @@ void freeEmpty() {
pool.free(int4);
pool.print();

TEST_ASSERT_EQUAL_UINT(nrBlocks * adjustedBlocksize, pool.freeMemory());
TEST_ASSERT_EQUAL_UINT(nrBlocks * blocksize, pool.freeMemory());
}

int main() {
Expand Down

0 comments on commit b7851f1

Please sign in to comment.