Skip to content

Commit eff3cca

Browse files
authored
Implement basic malloc and add unit test for it (#21)
## Description Implement basic malloc and add unit test for it. ## Issues <!-- use if this PR fully resolves the issue --> Closes #5 <!-- use if this PR is linked but should not close the issue --> Related: #<issue-number> ## Testing malloc unit test
1 parent d1ad382 commit eff3cca

File tree

3 files changed

+104
-8
lines changed

3 files changed

+104
-8
lines changed

include/malloc_from_scratch/memory_internal.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@ namespace internal
1111

1212
struct MemoryBlock
1313
{
14-
size_t size;
15-
bool allocated;
16-
MemoryBlock* next;
14+
size_t size_;
15+
bool allocated_;
16+
MemoryBlock* next_;
1717
};
1818

19-
extern MemoryBlock* block_list_head;
20-
extern void* heap_start;
21-
extern pthread_mutex_t allocator_mutex;
19+
inline MemoryBlock* block_list_head = nullptr;
20+
inline void* heap_start = nullptr;
21+
inline size_t total_memory_allocated = 0;
22+
inline pthread_mutex_t allocator_mutex = PTHREAD_MUTEX_INITIALIZER;
2223

2324
constexpr size_t BLOCK_METADATA_SIZE = sizeof(MemoryBlock);
2425
constexpr size_t MIN_BLOCK_SIZE = 16;
2526

27+
void insertMemoryBlockAtEnd(MemoryBlock** block_list_head, MemoryBlock* new_block);
28+
void* getPayloadAddress(MemoryBlock* block);
29+
MemoryBlock* getMetadata(void* payload_address);
30+
void* increaseHeap(size_t size);
31+
void* decreaseHeap(MemoryBlock* block_heap_end, size_t size);
32+
2633
} // namespace internal
2734
} // namespace mem
2835

src/malloc.cpp

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,91 @@
11
#include "malloc_from_scratch/memory_allocator.h"
2+
#include "malloc_from_scratch/memory_internal.h"
3+
4+
#include <unistd.h>
25

36
namespace mem
47
{
58

69
void* malloc(size_t size)
710
{
8-
(void)size;
9-
return nullptr;
11+
if (internal::heap_start == nullptr)
12+
{
13+
internal::heap_start = sbrk(0);
14+
}
15+
16+
internal::total_memory_allocated += size;
17+
18+
size_t total_size = sizeof(internal::MemoryBlock) + size;
19+
20+
void* new_memory_allocation = internal::increaseHeap(total_size);
21+
if (new_memory_allocation == NULL)
22+
{
23+
return NULL;
24+
}
25+
26+
internal::MemoryBlock* new_block =
27+
reinterpret_cast<internal::MemoryBlock*>(new_memory_allocation);
28+
new_block->size_ = size;
29+
new_block->allocated_ = 1;
30+
new_block->next_ = nullptr;
31+
32+
internal::insertMemoryBlockAtEnd(&internal::block_list_head, new_block);
33+
34+
return internal::getPayloadAddress(new_block);
35+
}
36+
37+
namespace internal
38+
{
39+
40+
void insertMemoryBlockAtEnd(MemoryBlock** block_list_head, MemoryBlock* new_block)
41+
{
42+
if (*block_list_head == nullptr)
43+
{
44+
*block_list_head = new_block;
45+
}
46+
else
47+
{
48+
MemoryBlock* current = *block_list_head;
49+
while (current->next_ != nullptr)
50+
{
51+
current = current->next_;
52+
}
53+
current->next_ = new_block;
54+
}
55+
}
56+
57+
void* getPayloadAddress(MemoryBlock* block)
58+
{
59+
if (!block)
60+
{
61+
return nullptr;
62+
}
63+
64+
return reinterpret_cast<void*>(reinterpret_cast<char*>(block) + BLOCK_METADATA_SIZE);
65+
}
66+
67+
MemoryBlock* getMetadata(void* payload_address)
68+
{
69+
if (!payload_address)
70+
{
71+
return nullptr;
72+
}
73+
74+
return reinterpret_cast<MemoryBlock*>(reinterpret_cast<char*>(payload_address) -
75+
BLOCK_METADATA_SIZE);
76+
}
77+
78+
void* increaseHeap(size_t size)
79+
{
80+
void* result = sbrk(static_cast<intptr_t>(size));
81+
if (result == reinterpret_cast<void*>(-1))
82+
{
83+
return nullptr;
84+
}
85+
86+
return result;
1087
}
1188

89+
void* decreaseHeap(MemoryBlock* block_heap_end, size_t size) { return nullptr; }
90+
} // namespace internal
1291
} // namespace mem

tests/unit/test_malloc.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "malloc_from_scratch/memory_allocator.h"
2+
#include "test_utils.h"
3+
4+
int main()
5+
{
6+
void* ptr = mem::malloc(64);
7+
ASSERT_NOT_NULL(ptr);
8+
9+
TEST_PASS();
10+
}

0 commit comments

Comments
 (0)