The Memory Manager Project

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

DATA STRUCTURES USING C++ Chapter 5
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Data Structures Using C++ 2E
The memory allocation problem Define the memory allocation problem Memory organization and memory allocation schemes.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Pointers OVERVIEW.
1 Writing a Good Program 8. Elementary Data Structure.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
C++ Lesson 1.
CSCE 210 Data Structures and Algorithms
Pointers and Dynamic Arrays
Popping Items Off a Stack Using a Function Lesson xx
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Data Structure and Algorithms
UNIT-3 LINKED LIST.
Dynamic Memory Allocation
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
CSCE 210 Data Structures and Algorithms
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 20: Binary Trees.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Chapter 21: Binary Trees.
Optimizing Malloc and Free
Linked Lists.
Simulated Pointers.
Chapter 18: Linked Lists.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Popping Items Off a Stack Lesson xx
Memory Management Chapter 10 11/24/2018 Crowley OS Chap. 10.
Pointers, Dynamic Data, and Reference Types
Simulated Pointers.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Dynamic Memory A whole heap of fun….
Chapter 16 Linked Structures
CMSC 341 Lists 3.
CMSC 341 Lists 3.
Pointers & Dynamic Data Structures
Arrays an array of 5 ints is filled with 3,2,4,1,7
Lists.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Module 13 Dynamic Memory.
21 Data Structures.
Data Structures & Programming
Presentation transcript:

The Memory Manager Project 1

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

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

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

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)

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

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)

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

Merging Consecutive free Blocks It should be clear that we want to maximize the size of the free blocks This means there should never be consecutive free blocks Whenever consecutive free blocks occur, they should be merged When we free a block, we need to check the previous and next blocks to see if they are free If so, we must merge the blocks into one big block This may involve the deletion of one or two blocknodes from our list

Merging Consecutive free Blocks When checking a previous block to see if it is free, be sure to check whether there is such a block (not the first) Similarly, when checking a following block, be sure to check that there is such a block (not the last) The MemoryManager class has two methods that you should implement: mergeBackwards mergeForward

Doubly-Linked List Utilities To manage doubly-linked lists, we will use a collection of templated functions We will not need the apparatus of a class here, a struct suffices The definition of dlNode and associated functions will be supplied in the file dlUtils.h

Project Files The files used in this project are dlUtils.h blockdata.h blockdata.cpp MemoryManager.h MemoryManager.cpp testMemMgr.cpp Do not modify, do not submit Complete and submit Modify and use for testing; Do not submit

Source Code 13

dlUtils.h template <class T> struct dlNode { T info; dlNode<T> *prev; dlNode<T> *next; dlNode<T>(T val, dlNode<T> *p, dlNode<T> *n) :info(val),prev(p),next(n){}; };

dlUtils.h template <class T> void insertAfter(dlNode<T> *first, dlNode<T> *current, T newval) { assert(first != NULL && current != NULL); current->next = new dlNode<T>(newval,current,current->next); current = current->next; if (current->next != nullptr) current->next->prev = current; }

dlUtils.h template <class T> void deleteNode(DNode<T>* &first, DNode<T>* current) { assert(first != NULL && current != NULL); DNode<T> *hold = current; if (current == first) { first = first->next; first->prev = NULL; current = first; } else { // we know that current->prev is not NULL current->prev->next = current->next; // But current->next might be NULL if(current->next != NULL) current->next->prev = current->prev; } delete hold; }

dlUtils.h template <class T> void printDlList(DNode<T>* first, const char *sep ) { DNode<T> *cursor = first; while(cursor != NULL && cursor->next!= NULL) { std::cout << cursor->info << sep; cursor = cursor->next; } if (cursor != NULL) std::cout << cursor->info << std::endl; }

dlUtils.h template <class T> void deletePrevious(dlNode<T>* &first, dlNode<T> *current) { assert(first != NULL && current != NULL); deleteNode(first, current->next); } template <class T> void deletePrevious(DNode<T> * &first, DNode<T> *current) { assert(first != NULL && current != NULL); deleteNode(first, current->prev); }

The blockdata Definition #include <iostream> class blockdata { friend ostream& operator<<(ostream& const blockdata &); public: blockdata(int s, bool f, unsigned char *p); int blocksize; bool free; // pointer to the first byte of the block: unsigned char* blockptr; };

The blockdata Implementation // blockdata.cpp blockdata::blockdata(int s, bool f, unsigned char *p) { blocksize = s; free = f; blockptr = p; }

The blockdata Implementation // blockdata.cpp std::ostream &operator<<(std::ostream &out, const blockdata &B) { std::out << "[" << B.blocksize << ","; if (B.free) std::out << "free"; else out << "allocated"; out << "]"; return out; }

The MemoryManager Definition class MemoryManager { public: MemoryManager(unsigned int memsize); unsigned char * malloc(unsigned int request); // On failure, returns nullptr or NULL void free(unsigned char * memptr); void showBlockList();

private: unsigned int memsize; // Heap size // pointer to the first byte of the heap: unsigned char *baseptr; dlNode<blockdata> *firstBlock; // Utility methods for free function: void mergeBackward(dlNode<blockdata> *p); void mergeForward(dlNode<blockdata> *p); // Utility method for malloc function: void splitBlock(dlNode<blockdata> *p, unsigned int chunksize); };

The MemoryManager Implementation #include <cassert> #include <iostream> #include "dlUtils.h“ #include "MemoryManager.h" MemoryManager::MemoryManager( unsigned int memtotal): memsize(memtotal) { baseptr = new unsigned char[memsize]; blockdata originalBlock(memsize,true,baseptr); firstBlock = new dlNode<blockdata>( originalBlock,nullptr,nullptr); }

The MemoryManager Implementation (partial) void MemoryManager::showBlockList() { printDlList(firstBlock,"->"); } void MemoryManager::mergeBackward(dlNode<blockdata> *p) { // Put your code here } void MemoryManager::mergeForward(dlNode<blockdata> *p) void MemoryManager::free(unsigned char *ptr2block) {// Put your code here }

The MemoryManager Implementation (partial) void MemoryManager::splitBlock(dlNode<blockdata> *p, unsigned int chunksize) { // Put your code here } unsigned char * MemoryManager::malloc(unsigned int request) { // Put your code here }

Visual Trace of Operations 27

The MemoryManager Constructor MemoryManager M(2048);

splitBlock Example splitBlock(q,20);

unsigned char *p1 = M.malloc(10);

unsigned char *p2 = M.malloc(20);

p1 = M.malloc(15);

When free is called on p1, we must merge the resulting consecutive free blocks to one Block allocated to p1 M.free(p1); 2018

Testing Code 34

#include <iostream> #include <cassert> #include "MemoryManager.h" const char * startlist = "\n---------BlockList start--------------\n" const char * endlist = "\n---------BlockList end-------------\n" int main() { MemoryManager heaper(2048); cout << "heap initialized\n"; cout << startlist; cout << heaper << endl; cout << endlist;

cout << "Doing first malloc:\n"; unsigned char * p1 = heaper.malloc(10); cout << "malloc done\n"; cout << startlist; cout << heaper << endl; cout << endlist; cout << "On to the second malloc\n"; unsigned char *p2 = heaper.malloc(20);

cout << "Next free the first pointer\n"; heaper.free(p1); cout << startlist; cout << heaper << endl; cout << endlist; cout << "Now do a malloc for a block too big for " << "the initial open block\n"; p1 = heaper.malloc(15); cout << "malloc done\n"; cout << heaper << endl; n\n";

cout << "Next free the most recently " << "allocated pointer\n"; heaper.free(p1); cout << startlist; cout << heaper << endl; cout << endlist; cout << "Next free the middle pointer\n"; heaper.free(p2); return 0; }

Test Output 39

heap initialized -------------BlockList start------------------ [2048,free] -------------BlockList end------------------ Executing p1 = malloc(10): malloc done [10,allocated] -> [2038,free] Executing p2 = malloc(20): [10,allocated] -> [20,allocated] -> [2018,free]

Executing free(p1): -------------BlockList start------------------ [10,free] -> [20,allocated] -> [2018,free] -------------BlockList end------------------ malloc for a block too big for the initial open block Executing p1 = malloc(15) malloc done [10,free] -> [20,allocated] -> [15,allocated] -> [2003,free]

Next free the most recently allocated pointer (p1) -------------BlockList start------------------ [10,free] -> [20,allocated] -> [2018,free] -------------BlockList end------------------ Next free p2 [2048,free]