Skip to content

A Memory Allocator - Implementations of malloc, calloc, realloc and free

Notifications You must be signed in to change notification settings

JanBaig/Memory-Allocator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 

Repository files navigation

Memory-Allocator

A memory allocator that implements the core C functions - malloc, calloc, realloc and free

The implementations of malloc, calloc, realloc and free mainly rely on either the sbrk/brk or mmap system calls in a UNIX system (VirtualAlloc for Windows). The system call

void *sbrk(intptr_t increment);

moves the program break by an increment amount of bytes and returns either the previous or current address (depending on the internal implementation) of the breakpoint of the running process.

What is the program breakpoint?

Short Answer - The boundary separating the heap’s mapped and unmapped memory regions is the program breakpoint.

Processes have their own virtual address space - an address space the OS makes available for them to use. This virtual address space is divided into several regions to hold the process’s stack, code, constants, globals, local variables, and heap. The program’s heap is what we are concerned about as that is where dynamic memory allocation is supported.

The process's heap consists of both mapped and unmapped memory addresses. Mapped memory regions, unlike unmapped, contain addresses that have been mapped to physical memory. The translation of an unmapped memory address to a mapped one happens dynamically through the Memory Management Unit (MMU) of the system.

The boundary separating the heap’s mapped and unmapped memory regions is the system break point.

As malloc is used to request some memory, the system breakpoint is moved to enlarge the mapped region - effectively allocating more memory dynamically.

Allocator Implementation Diagrams (1)

Image created on FigJam


Note: This project was done following a simple tutorial by Marwan Burelle (site is currently down)
and is yet to be throughly tested. I may update the project in the future to include testing and
focus more on the optimization of the functions. Aside from that, I've found it quite eye-opening
how complicated concepts may seem before diving deep and disecting them. Projects like this help
make magic less magical and more understandable💡  

About

A Memory Allocator - Implementations of malloc, calloc, realloc and free

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages