hi everyone I'm asked to implement malloc and free as my project in operating system course , but the big problem is that i have searched a lot about it and still have no idea about it , if anyone can tell me what are the main functionalities of the malloc and free or provide me with any usefull links about how to use brk & sbrk system calls I would really appreciate it .
Thanks in advance

There are plenty of online resources for understand free/malloc implementations. Some of them go as far as step-by-step tutorials. Google for simple malloc implementation
I would suggest, however, that you start with something easier than brk and sbrk. Why not get things working with a fixed size array (say char pool[4096]) and once your algorithms seem to be working there transition them to an environment that uses the system calls.

Beginning with L7Sqr's comment, there are a lot of resources out there, including the source code for GCC/glibc and other compilers/libraries out there that implement malloc/free which you can study to help understand what is going on. I did a considerable amount of research back in the 1990's into memory management functions and generic garbage collectors. This is not simple, but it is doable.

  1. You need to get memory from the operating system heap. Traditionally the brk()/sbrk() system calls do this.
  2. You need to determine a method of tracking where memory that malloc provides is located.
  3. You need to be able to merge freed memory segments as necessary.
  4. You need to have a method to provide a suitably sized chunk of memory to the user program when asked with malloc(), from the free store you have available.
  5. If a big enough chunk of memory is not available, then you need to know how to ask the operating system to give you more, if available, and tack that onto your free store.
  6. etc. etc... :-)

Remember, Google is your friend! :-) And good luck! FWIW, this is a GREAT assignment! It will teach you a lot by the time you are done!

Edited 4 Years Ago by rubberman

Thanks a lot for your notes , I really did googled it out but I didn't find what want ,

start with something easier than brk and sbrk

L7sqr do you mean do a simulation and then implement the real one ?? I only have 2 weeks would this be enough to do so ??

This article has been dead for over six months. Start a new discussion instead.