06_MemoryManagerProject.pptx - The Memory Manager Project...

This preview shows page 1 - 10 out of 42 pages.

The Memory Manager Project
Image of page 1

Subscribe to view the full document.

2 Objectives The goal of your next project is to simulate the runtime module used to allocate and de-allocate dynamic memory. This module is known as the "heap manager" The "heap" is a large "pool" of memory set aside by the runtime system The two main functions are malloc , used to satisfy a request for a specific number of consecutive blocks; free , used to make allocated blocks available f
Image of page 2
3 Objectives The two main heap manager functions are malloc , used to satisfy a request for a specific number of consecutive blocks malloc returns the address of the first byte of the allocated memory block free , used to make allocated blocks available for use in future calls to malloc
Image of page 3

Subscribe to view the full document.

4 Description Our simulation uses a large block of unsigned chars as our memory pool; and a doubly-linked list to keep track of allocated and available blocks of unsigned char. We will refer to the nodes of this list as blocknodes The info field of each node is of type blockdata An object of type blockdata has attributes blocksize number of bytes in the block free a Boolean flag indicating the status of a block blockptr a pointer to the first byte of the block
Image of page 4
5 malloc The malloc algorithm has an int parameter request request is the size of the block to be allocated request scans the list until it finds the first blocknode B such that B.free == true; and B.size request If no such block is found, malloc returns NULL (0)
Image of page 5

Subscribe to view the full document.

6 malloc If B.size is larger than request , the block is broken up into two blocks The first block's size: request The second's size: B.size-request This requires that we insert a new blocknode C after B to reference the second block (which is free) Then, whether we split the block or not, we set B.free to false set B.size to request return the address B.bptr
Image of page 6
7 malloc To create a new blocknode C after B , you must create a blockdata object x with the desired values of free , size and bptr call the doubly-linked list function insertAfter(B,x)
Image of page 7

Subscribe to view the full document.

8 free To implement free(unsigned char *p) we must find the blocknode whose bptr field equals p This is done by traversing the blocknode list If this fails, we return NULL (or nullptr ) Otherwise we change the blocknode's free field to true But we don't stop there
Image of page 8
9 Merging Consecutive free Blocks It should be clear that we want to maximize the size of
Image of page 9

Subscribe to view the full document.

Image of page 10
  • Fall '19
  • Pointer, Memory pool

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

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes