Malloc_tutorial - A Malloc Tutorial Marwan Burelle...

This preview shows page 1 - 2 out of 20 pages.

A Malloc Tutorial * Marwan Burelle Laboratoire Syst`eme et S´ecurit´e de l’EPITA (LSE) February 16, 2009 Contents 1 Introduction 2 2 The Heap and the brk and sbrk syscalls 2 2.1 The Process’s Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.2 brk(2) and sbrk(2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2.3 Unmapped Region and No-Man’s Land . . . . . . . . . . . . . . . . . . . . . 4 2.4 mmap(2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3 Dummy malloc 4 3.1 Principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3.2 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 4 Organizing the Heap 5 4.1 What do we need ? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 4.2 How to represent block information . . . . . . . . . . . . . . . . . . . . . . . 6 5 A First Fit Malloc 7 5.1 Aligned Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 5.2 Finding a chunk: the First Fit Algorithm . . . . . . . . . . . . . . . . . . . . . 8 5.3 Extending the heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 5.4 Spliting blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 5.5 The malloc function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 6 calloc , free and realloc 11 6.1 calloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 6.2 free . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 6.2.1 Fragmentation: the malloc illness . . . . . . . . . . . . . . . . . . . . 12 6.2.2 Finding the right chunk . . . . . . . . . . . . . . . . . . . . . . . . . . 13 6.2.3 The free function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 * http: // wiki-prog.kh405.net [email protected] 1
Image of page 1

Subscribe to view the full document.

6.3 Resizing chunks with realloc . . . . . . . . . . . . . . . . . . . . . . . . . . 14 6.3.1 FreeBSD’s reallocf . . . . . . . . . . . . . . . . . . . . . . . . . . 18 6.4 Putting things together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1 Introduction What is malloc ? If you don’t even know the name, you might begin to learn C in the Unix environment prior to read this tutorial. For a programmer, malloc is the function to allocate memory blocks in a C program, most people don’t know what is really behind, some even thinks its a syscall or language keyword. In fact malloc is nothing more than a simple function and can be understood with a little C skills and almost no system knowledge. The purpose of this tutorial is to code a simple malloc function in order to understand the underlying concepts. We will not code an e ffi cient malloc , just a basic one, but the concept behind can be useful to understand how memory is managed in every day processes and how-to deal with blocks allocation, reallocation and freeing. From a pedagogical standpoint, this is a good C practice. It is also a good document to understand where your pointers come from and how things are organized in the heap. What is malloc malloc(3) is a Standard C Library function that allocates ( i.e. reserves) memory chunks. It complies with the following rules: malloc allocates at least the number of bytes requested; The pointer returned by malloc points to an allocated space ( i.e. a space where the program can read or write successfully;) No other call to malloc will allocate this space or any portion of it, unless the pointer has been freed before. malloc should be tractable: malloc must terminate in as soon as possible (it should not be NP-hard !;) malloc should also provide resizing and freeing. The function obey to the following signature: void * malloc(size_t size); Where size is the requested size. The returned pointer should be NULL in case of faillure (no space left.) 2 The Heap and the brk and sbrk syscalls Prior to write a first malloc , we need to understand how memory is managed in most multitask systems. We will keep an abstract point of view for that part, since many details are system and hardware dependant.
Image of page 2
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern