Question: hi I have copy pasted the code in website too. appreciate your time and help Hi Vishal, #include #include // for setw() using namespace std;






















hi I have copy pasted the code in website too. appreciate your time and help
Hi Vishal,
#include #include // for setw() using namespace std; /******************************************* Author : Program : Memory Manager (MM3 - NO REPORTS) Due Date: Course : CSC 300 Data Structures - Fall 2018 - DSU Instr : Krebsbach ******************************************** */ // GLOBAL DATA STRUCTURES ======================= typedef struct FREE_NODE * FREEPTR; typedef struct ALLOCNODE * ALLOCPTR; struct FREE_NODE // FREE LIST NODES { int start_byte; int end_byte; int size; FREEPTR next; }; struct ALLOCNODE // ALLOCTADED LIST NODES { int start_byte; int end_byte; int size; int id; ALLOCPTR next; }; // ====== GLOBAL DATA ========================= FREEPTR freelist = NULL; // the FREE link list ALLOCPTR alloclist = NULL; // the ALLOCATED link list int total_memory_managed = 0; // the amount of memory managed //====== PROTOTYPES =========================== //--- test only --- void dump_freelist(void); void dump_alloclist(void); //--- utility --- void remove_empty_freenodes(void); void insert_freeblock(FREEPTR fptr); void merge_freenodes(void); //--- interface --- void init_memory_manager(const int amount); int allocate_memory(const int job, const int amount); void release_memory(const int job); int total_free(void); int total_allocated(void); int largest_free(void); int job_allocated(const int job); void report_memory(void); //STUB void report_jobs(void); // STUB //========== MAIN ============================= int main(void) { char ch ; // used to pause between tests int r; // results of allocate_memory // ================================================================================ cout >ch; cout >ch; cout >ch; cout >ch; cout >ch; cout start_byte = 0; freelist -> end_byte = amount - 1; freelist -> size = amount; freelist -> next = NULL; return ; } /* ======================================== int total_free(void); Description: compute the total number of bytes not allocated Parameters: NONE Return value: the amount of total free bytes Notes: ===========================================*/ int total_free(void) { int amount = 0; FREEPTR tmp = freelist; while (tmp != NULL) { amount = amount + tmp->size; tmp = tmp->next; } return amount; // return amount of free memory } /* ======================================== int total_allocated(void); Description: compute the total number of bytes allocated Parameters: NONE Return value: the amount of total allocated bytes Notes: ===========================================*/ int total_allocated(void) { int amount = 0; ALLOCPTR tmp = alloclist; while (tmp != NULL) { amount = amount + tmp->size; tmp = tmp->next; } return amount; // return amount of allocated memory }; /* ======================================== int largest_free(void); Description: find the largest available free block of memory Parameters: NONE Return value: the amount of free memory in the block Notes: ===========================================*/ int largest_free(void) { int amount = 0; FREEPTR tmp = freelist; while (tmp != NULL) { if (tmp->size > amount) amount = tmp->size; tmp = tmp->next; } return amount; } /* ======================================== int job_allocated(const int job); Description: compute the total amount of memory allocated to a particular job. Parameters: - job : the number of the job Return value: the total amount of memory allocated to the job Notes: ===========================================*/ int job_allocated(const int job) { int amount = 0; ALLOCPTR tmp = alloclist; while (tmp != NULL) { if (tmp->id == job) amount = amount + tmp->size; tmp = tmp->next; } return amount; // return amount of allocated memory } /* ======================================== int allocate_memory(const int job, const int amount); Description: reflect allocation of memory to a particular job Parameters: - job : the id of the user (can assume positive integer value) - amount : then number of bytes asked to allocate Return value: the number of bytes actually allocated Notes: Special cases: amount total_memory_managed : return 0 amount > largest_free( ) : return 0 ===========================================*/ int allocate_memory(const int job, const int amount) { FREEPTR tmpfree = NULL; // pointer to move through list ALLOCPTR tmpalloc = NULL; // use to point to new alloc node // check if should continue if (amount total_memory_managed) return 0; // more then total managed if (largest_free() next = NULL; // move to first node big enough and use it tmpfree = freelist; while (tmpfree != NULL) { if (tmpfree->size >= amount) //found node { // fill up the new alloc node tmpalloc->end_byte = tmpfree->end_byte; tmpalloc->start_byte = tmpalloc->end_byte - amount + 1; tmpalloc->size = amount; tmpalloc->id = job; //update freenode tmpfree->end_byte = tmpfree->end_byte - amount; tmpfree->size = tmpfree->size - amount; break; // exit the while loop } tmpfree = tmpfree->next; } // check for empty free node remove_empty_freenodes(); // now inset allocnode into list ------------------------ // check if alloclist is empty if (alloclist == NULL) { alloclist = tmpalloc; } else // check if insert as first node if (alloclist->start_byte > tmpalloc->start_byte) { tmpalloc->next = alloclist; alloclist = tmpalloc; } else // find where to insert in list (After first node) { ALLOCPTR t = alloclist; while (t->next != NULL) { if (t->next->start_byte > tmpalloc->start_byte) { tmpalloc->next = t->next; t->next = tmpalloc; break; } t = t->next; }//while if (t->next == NULL) t->next = tmpalloc; } return amount; // return amount allocated } /* ======================================== void release_memory(const int job); Description: reflect the deallocation of ALL memory held by a particular job Parameters: - job : the job whose memory to deallocate Return value: NONE Notes: ===========================================*/ void release_memory(const int job) { // Free all memory allocated to process ALLOCPTR a1 = alloclist; ALLOCPTR temp = NULL; FREEPTR f1 = NULL; if (a1 == NULL) return; // empty list // remove all first nodes THIS effects the alloclist pointer !!!!! while(a1 != NULL && a1->id == job) // { // set up freenode to be inserted f1 = new FREE_NODE; f1->next = NULL; f1->start_byte = a1->start_byte; f1->end_byte = a1->end_byte; f1->size = a1-> size; // INSERT f1 into FREELIST insert_freeblock(f1); f1 = NULL; // just to make sure; // clean up alloclist alloclist = a1->next; delete a1; a1 = alloclist; } // all first nodes have been deleted if (alloclist == NULL) return; // empty list while (a1->next != NULL) { while ((a1->next != NULL) && (a1->next->id == job)) { // set up freenode to be inserted f1 = new FREE_NODE; f1->next = NULL; f1->start_byte = a1->next->start_byte; f1->end_byte = a1->next->end_byte; f1->size = a1->next-> size; // INSERT f1 into free list insert_freeblock(f1); f1 =NULL; // just to make sure // clean up alloclist temp = a1->next ; a1->next = temp->next; delete temp; } if (a1->next != NULL) a1 = a1->next; } return; } /* ======================================== void report_memory(void); Description: Report the use of the continuous memory usage Parameters: NONE Return value: NONE Notes: See Report Formats 2018 document for format ===========================================*/ void report_memory(void) { // STUB ===== return; //=========== } /* ======================================== void report_jobs(void); Description: Report the memory blocks used by each job Parameters: NONE Return value: NONE Notes: See Report Formats 2018 document for format ===========================================*/ void report_jobs(void) { // STUB ===== return; //=========== } //============================================== //======= UTILITY FUNCTIONS ==================== //============================================== /* ======================================== void remove_empty_freenode(void); Description: remove all size == 0 nodes from freelist Parameters: NONE Return value: NONE Notes: supports: allocate_memory() ===========================================*/ void remove_empty_freenodes(void) { FREEPTR t1 = NULL; FREEPTR t2 = NULL; if (freelist == NULL) return; // empty list while ((freelist != NULL) && (freelist->size == 0)) // remove front nodes { t1 = freelist; freelist = freelist->next; delete t1; } if (freelist == NULL) return; // empty list t1 = freelist; // t points to first node and it is not an empty (SIZE =0) node!! while (t1!= NULL) // DOES REMOVE ALL ZERO NODES :) { if ((t1->next != NULL) && (t1->next->size == 0)) // remove the empty node { t2 = t1->next; t1->next = t2->next; //***************************** delete t2; } else // will move to next node or t1 becomes NULL t1 = t1->next; } return; } /* ======================================== void merge_freenodes(void); Description: merge any freenodes that need it Parameters: NONE Return value: NONE Notes: supports: insert_freeblock() ===========================================*/ void merge_freenodes(void) { if (freelist == NULL) //empty list return; // merge nodes FREEPTR t = freelist; FREEPTR d = NULL; while (t->next != NULL) { d = t->next; // points to next node if ((t->end_byte +1) == d->start_byte) // merge needed! { t->end_byte = d->end_byte; t->size = t->size + d->size; t->next = d->next; delete d; } else { t = t->next; } }//while return; } /* ======================================== void insert_freeblock(FREEPTR fptr); Description: place a freeblock into the freelist Parameters: - fptr : pointer to freenode to ne inserted Return value: NONE Notes: supports: release_memory() ===========================================*/ // insert freeblock (alt version) void insert_freeblock(FREEPTR fptr) { if (freelist == NULL) { freelist = fptr; return; } // handle if inserted at front of freelist; if (fptr ->start_byte start_byte) // need to insert { //insert at front fptr->next = freelist; freelist = fptr; } else { // NOT inserting into front of list. FREEPTR t = freelist; while (t->next != NULL) { if (t->next->start_byte > fptr->start_byte) // need to insert between nodes { fptr->next = t->next; t->next = fptr; break; } t = t->next; }//while // check if needs to be inserted at end of list if (t->next == NULL) t->next = fptr; // insert end of list } // else // have inserted node now merge if needed merge_freenodes(); return; } //============================================== //======= TESTING FUNCTIONS ==================== //============================================== void dump_freelist(void) { FREEPTR tmp = freelist; // temp pointer to list cout start_byte end_byte size next; //move pointer to next node (or NULL) } return; } //---------------------- void dump_alloclist(void) { ALLOCPTR tmp = alloclist; // temp pointer to list cout start_byte end_byte size id next; //move pointer to next node (or NULL) } return; } Thank You!! //Bro some partial answer would help too...
Here is the format to use for the following two services of the memory manager 1. Report Jobs For each job you should report each block of memory they use. You should report the blocks h order but you do NOT have to report the jobs in order of job number. The Colum Headers should also be printed (the green below) JB Memory Usage 10-50 64-75 200-240 1-9 101-134 180- 184 185-192 2. Report Memory This should be a sequential dump of all memory showing usage Mamory Block JOB 0-9 10-50 51-53 54-75 7-100 101 134 135-179 180-184 185- 192 193-199 200-240 241 255 FREE FREE FREE FREE FREE
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
