Skip to content

servisbryce/malloc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The Memory Allocator

A simpler alternative to the glibc malloc.

Localization

There are multiple heaps. Every heap has its own unique linked list of segments. Segments that are inside of a heap only belong to that heap; everything is localized.

Allocation Methods

Splicer: a free segment is sliced into two segments. One segment is handed to the user and the other is marked as free. By far the most efficient and fastest. Bump: a dumb allocation method for when slicer isn't available. Avoid this.

Tunable Search Depth

Search depth is the count of how many loops are ran in the splicer function while searching. There is a threshold where malloc will give up that is tunable by the user.

Depending on personal preference, you may want to set this to a high number to have somewhat good performance and less fragmentation or a low number to have poor fragmentation but great performance.

Or, you can set it to 0 and always search for the best available segment.

Free Operations

Coalescing: a scan of the localized heap of the segment that was freed to merge adjacent free segments. Sweep: a scan of the localized heap of the segment that was freed to see if the entire heap is free. If the entire heap is free, then it may be deallocated depending on the lazy sweep threshold. Trim: a way of shortening the heap to a specific length whenever the tail segment of said heap is above the trim threshold. This is used to avoid overuse of memory by the process.

Lazy Operations

Lazy operations are operations that aren't ran every single time this program is invoked. Rather, they're set to have a threshold and when that threshold is crossed, then they execute.

Why? Linux kernel calls are extremely time consuming, so we must avoid or defer them as much as possible.

Memory Allocator Options

By using the mallopts option along with the enums inside of the bundled header file, you're able to configure the various thresholds mentioned above.

Design Choices

All segments are created equal. There isn't any binning, arenas, etc to reduce design complexity. This allocator is as simple as it gets while not jeopardizing performance or stability.

Why?

This program is a personal project designed to help me understand memory management better and learn to be a better C programmer. I've designed this project to the best of my ability and it performs similarly to malloc at the vast majority of tasks and even sometimes outperforms it in some specific tasks.

I've documented this code fairly well so if you'd like to understand the internal workings of a malloc() function, please take a peek at the source code.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published