gvannest / ft_malloc Goto Github PK
View Code? Open in Web Editor NEW42 project which consists in coding the C dynamic memory allocation function malloc()
42 project which consists in coding the C dynamic memory allocation function malloc()
https://sourceware.org/glibc/wiki/MallocInternals#Overview_of_Malloc
https://sploitfun.wordpress.com/2015/02/10/understanding-glibc-malloc/
https://www.blackhat.com/presentations/bh-usa-07/Ferguson/Whitepaper/bh-usa-07-ferguson-WP.pdf
https://wiki-prog.infoprepa.epita.fr/images/0/04/Malloc_tutorial.pdf
One global variable : pointer to first free chunk in the free list.
Two header types: one for free and one for allocated chunks (cf doc above)
One call mmap = one heap (for ex 132Kb for tiny) corresponding to one allocaiton type (tiny or small). Once the heap is full -> call mmap again and link the two heaps with a pointer at the end of the first heap
Large allocations => direct call to mmap (no heap) and direct call to unmmap when free is called on them
when a chunk is free'd : check flag (P) to see if previous chunk is free, and combine them in the free list in case it is. However change header in heap block
WATCH OUT: the free list is only pointers to free chunks. indeed all the info (pointers to next and previous bloc + size) are in the header which has been changed when the program calls free on an allocated chunk. IT IS NOT A NEW DATA STRUCTURE! We only have the first pointer of the free list in a global variable, and then in the headers of the free chunks we have the necessary data to jump to the next free chunk + size
WATCH OUT: ALIGNMENT. chunks must be multiples of 8 bytes. So minimum allocated chunk is 16 bytes : 2 times 4 bytes for the 2 size_t variables in the header, + minimum 8 bytes for user data.
for defragmentation: when triggger (len of free list > threshold?) => defrag
Errors management: check what fbabin has done on ftp. Using an enum with error codes?
free
when the address does not exist?Note: free is in another issue, please refer to #1
analysis of malloc source code
https://code.woboq.org/userspace/glibc/malloc/malloc.c.html#malloc_chunk
making a global variable available in several .c
files: https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files
https://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration
https://www.tutorialspoint.com/extern-keyword-in-c
chunk headers:
https://reverseengineering.stackexchange.com/questions/19293/heap-chunk-structure-does-not-contain-previous-section-info
malloc minimum chunk size (from malloc source code):
If n is zero, malloc returns a minumum-sized chunk. (The minimum
--
size is 16 bytes on most 32bit systems, and 24 or 32 bytes on 64bit
systems.) On most systems, size_t is an unsigned type, so calls
with negative arguments are interpreted as requests for huge amounts
of space, which will often fail. The maximum supported value of n
differs across systems, but is in all cases less than the maximum
representable value of a size_t.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.