Skip to content

WIP: New malloc#1

Draft
noryb009 wants to merge 1 commit intomasterfrom
new-malloc
Draft

WIP: New malloc#1
noryb009 wants to merge 1 commit intomasterfrom
new-malloc

Conversation

@noryb009
Copy link
Owner

For small objects, this implementation of malloc maintains arenas of objects of the same size. Since all objects in these arenas are the same size, they can share a single header, allowing small allocations to be efficient. In the below table, efficiency is defined as (# bytes used for storing objects) / (# bytes allocated), using pages of 4096 bytes. If the efficiency is 50%, then for each 1 byte stored, there is 1 byte of overhead.

Allocation Size Efficiency
1 0.883544921875
2 0.935546875
4 0.9638671875
8 0.978515625
16 0.984375
32 0.984375
64 0.984375
128 0.96875
256 0.9375
512 0.875
1024 0.75
2048 0.5

For allocations larger than the page size, it is assumed that the allocation will be aligned to the page.

Compared to a scheme with chunk headers of 8 bytes, this scheme is more efficient for allocations of size 1-128, but is a lot less efficient for larger allocations. However, it has the slight advantage that all large allocations are page-aligned.

It might be possible to combine this scheme with another scheme for allocations >= 256 (or 128) bytes. However, the current scheme reserves all allocations that are not aligned to the page size as a small allocation, so more might need to be added to differentiate between small allocations and large allocations, such as a hash table.

Passes all tests, but does not free anything yet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant