https://github.com/kylesmith19091/malloc-implementation
A from scratch implementation of the malloc c function using a custom heap structure
https://github.com/kylesmith19091/malloc-implementation
malloc
Last synced: 10 months ago
JSON representation
A from scratch implementation of the malloc c function using a custom heap structure
- Host: GitHub
- URL: https://github.com/kylesmith19091/malloc-implementation
- Owner: KyleSmith19091
- Created: 2021-12-30T13:07:05.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-12-30T13:10:13.000Z (about 4 years ago)
- Last Synced: 2025-02-02T03:41:28.833Z (about 1 year ago)
- Topics: malloc
- Language: C++
- Homepage:
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
Malloc Implementation
Implementation Discussion
Heap is represented as an array of uintptr_t, where each index in the heap will have space for an 8 byte value(this depends on the user's system), which is used to represent a form of alignment that allocations need to follow.
📐 Allocation
Allocations will then start with a process where we will traverse a list of 'headers' which hold the matadata for previous or currently in use memory allocations, by inspecting whether a block of suffiecent size is available.
If a block is available it will then mark that block as in use and return a pointer to the user. This is done by returning the memory location that is one unit after the header(current header address + 16 bytes/size of header struct).
This list is implemented as a singly linked list and will provide O(n) traversals where n is the number of previously allocated / in use blocks of memory on heap. In the case where a block is not found the linked list, we then exapnd our
heap memory at the tail end to make space for the allocation request.
🔥 Deallocation
Deallocation is done by first checking if the block to be deallocated is at the tail end of the heap, if this is the case we proceed to 'give back' the memory (simulating a similar behaviour of the malloc function). Otherwise
we simply mark the block as free for reuse. Thus dealocation is done O(1) time except when giving back the memory as we manually clear the data in the heap (just a personal design decision).
⚡️ Serialising
The main purpouse of the project is to get a basic understanding of how malloc works with a basic heap memory structure. So for this purpose I created two member functions of the memory manager class
- printHeap()
- dumpHeap()
Printing the heap, will show the actaul values that are stored in the heap. Dumping the heap will show some more of the metadata that is associated with the heap, such as used, freed and unused blocks.
Usage
Requirements
- clang++ compiler => Must be able to compile c++11 code
- make
To run and compile enter:
```bash
make
```