Concept explainers
Explanation of Solution
Modify the allocator to perform constant-time coalescing requires both a header and a footer for each block:
In the “Section 9.9.12 (mm.c)”, remove the red color text and add the highlighted lines which is represented in the below code. The modified “mm.c” file is as follows:
#define MAX(x, y) ((x) > (y)? (x) : (y))
/* Pack a size and allocated bit into a word */
#define PACK(size, alloc) ((size) | (alloc))
// Define a pack with size and allocated bit
#define PACK(size, alloc, prev_alloc) ((size) | (alloc) | (prev_alloc << 1))
/* Read and write a word at address p */
#define GET(p) (*(unsigned int *)(p))
/* Read the size and allocated fields from address p */
#define GET_SIZE(p) (GET(p) & ~0x7)
#define GET_ALLOC(p) (GET(p) & 0x1)
// Define allocated fields
#define GET_PREV_ALLOC(p) ((GET(p) >> 1) & 0x1)
/* Given block ptr bp, compute address of its header and footer */
#define HDRP(bp) ((char *)(bp) - WSIZE)
if ((heap_listp = mem_sbrk(4 * WSIZE)) == (void *)-1)
return -1;
PUT(heap_listp, 0); /* Alignment padding */
PUT(heap_listp + (1 * WSIZE), PACK(DSIZE, 1)); /* Prologue header */
PUT(heap_listp + (2 * WSIZE), PACK(DSIZE, 1)); /* Prologue footer */
PUT(heap_listp + (3 * WSIZE), PACK(0, 1)); /* Epilogue header */
// Call PUT() function for Prologue header
PUT(heap_listp + (1 * WSIZE), PACK(DSIZE, 1, 1)); /* Prologue header */
// Call PUT() function for Prologue footer
PUT(heap_listp + (2 * WSIZE), PACK(DSIZE, 1, 1));
// Call PUT() function for Epilogue header
PUT(heap_listp + (3 * WSIZE), PACK(0, 1, 1));
heap_listp += (2 * WSIZE);
/* $end mminit */
return NULL;
/* Adjust block size to include overhead and alignment reqs. */
if (size <= DSIZE)
asize = 2 * DSIZE;
else
asize = DSIZE * ((size + (DSIZE)+(DSIZE - 1)) / DSIZE);
// Check size to adjust block
if (size <= WSIZE)
// Assign size value
asize = DSIZE;
// Otherwise
else
// Compute size to add overhead and alignment requirements
asize = DSIZE * ((size + (WSIZE)+(DSIZE - 1)) / DSIZE);
/* Search the free list for a fit */
if ((bp = find_fit(asize)) != NULL) {
}
/* $begin mmfree */
PUT(HDRP(bp), PACK(size, 0));
PUT(FTRP(bp), PACK(size, 0));
// Call PUT() function with size and allocated bit
PUT(HDRP(bp), PACK(size, 0, GET_PREV_ALLOC(HDRP(bp))));
PUT(FTRP(bp), PACK(size, 0, GET_PREV_ALLOC(HDRP(bp))));
// Check allocated bit
if (GET_ALLOC(HDRP(NEXT_BLKP(bp))))
// Call PUT() function
PUT(HDRP(NEXT_BLKP(bp)), PACK(GET_SIZE(HDRP(NEXT_BLKP(bp))), 1, 0));
// Otherwise
else {
// Call PUT() function for HDRP
PUT(HDRP(NEXT_BLKP(bp)), PACK(GET_SIZE(HDRP(NEXT_BLKP(bp))), 0, 0));
// Call PUT() function for FTRP
PUT(FTRP(NEXT_BLKP(bp)), PACK(GET_SIZE(HDRP(NEXT_BLKP(bp))), 0, 0));
}
// Call coalesce() function to fill values
coalesce(bp);
}
/* $begin mmfree */
static void *coalesce(void *bp)
{
size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp)));
// Call GET_PREV_ALLOC() function and the return value is assign to prev_alloc
size_t prev_alloc = GET_PREV_ALLOC(HDRP(bp));
size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
size_t size = GET_SIZE(HDRP(bp));
else if (prev_alloc && !next_alloc) { /* Case 2 */
size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
PUT(HDRP(bp), PACK(size, 0));
PUT(FTRP(bp), PACK(size, 0));
// Call PUT() function for HDRP with PACK size
PUT(HDRP(bp), PACK(size, 0, 1));
// Call PUT() function for FTRP with PACK size
PUT(FTRP(bp), PACK(size, 0, 1));
}
else if (!prev_alloc && next_alloc)
{
/* Case 3 */
size += GET_SIZE(HDRP(PREV_BLKP(bp)));
PUT(FTRP(bp), PACK(size, 0));
PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
// Call PUT() function for HDRP with PACK size
PUT(FTRP(bp), PACK(size, 0, 1));
// Call PUT() function for FTRP with PACK size
PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0, 1));
bp = PREV_BLKP(bp);
}
else {
/* Case 4 */
size += GET_SIZE(HDRP(PREV_BLKP(bp))) +
GET_SIZE(FTRP(NEXT_BLKP(bp)));
PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));
// Call PUT() function for HDRP with PACK size
PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0, 1));
// Call PUT() function for FTRP with PACK size
PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0...
Want to see the full answer?
Check out a sample textbook solutionChapter 9 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
- The goal of this second exercise is to develop a better understanding of the Linuxinterprocess communication mechanisms. This would require you to write twoprograms P1 and P2. The first program P1 needs to generate an array of 50random strings (of characters) of fixed length each. P1 then sends a group offive consecutive elements of the array of strings to P2 along with the ID’s ofthe strings, where the ID is the index of the array corresponding to the string.The second program P2 needs to accept the received strings, and send backthe highest ID received back to P1 to acknowledge the strings received. Theprogram P2 simply prints the ID’s and the strings on the console. On receivingthe acknowledged packet, P1 sends the next five strings, with the string elementsstarting from the successor of the acknowledged ID. The above mechanism needs to be implemented FIFO and UNIX DOMAIN SOCKETS. Please note that you may NOT make assumptions about the reliability of the interprocess communication…arrow_forwardPlease answer the questions in the picture correctly, and it is done by a team of bartleby experts. Please don't copy paste the answer from the website, because it was my previous question. Not purely answered by a bartlely team expert, but rather taking Chegg's answer. So, please don,t copy paste solution from website. Because many answers from websites are inaccurate, as well as from Cheggarrow_forwardThis one in c++.arrow_forward
- Using a Generic class that implements the Comparator interface, write a workingprogram that compares the performance of any two IPL teams in terms of the following:1) Points per match= PTS/PLD2) Form= (# of Ws)/33) Net Run Rare= Net RRYou can use the following data to make comparisons between any two teams of yourchoice: You can hard code the data associated with the two teams you have chosen. Hence, no needto take any inputs from the user. Upon running the program, the program should simplyoutput the three comparison results.Additionally, please mention the names of the two chosen teams in the beginning of theprogram in a comment.arrow_forwardI need help urgently to be able to understand how to do this program it is written in C++ but I am currently lost as to how I should start or do it or what is it even asking I would highly appreciate any good helparrow_forwardIn java please.arrow_forward
- Create a function that takes a grid of # and -, where each hash (#) represents amine and each dash (-) represents a mine-free spot.Return a grid, where each dash is replaced by a digit, indicating the number ofmines immediately adjacent to the spot i.e. (horizontally, vertically, anddiagonally).Example of an input:[ ["-", "-", "-", "#", "#"],["-", "#", "-", "-", "-"],["-", "-", "#", "-", "-"],["-", "#", "#", "-", "-"],["-", "-", "-", "-", "-"] ]Example of the expected output:[ ["1", "1", "2", "#", "#"],["1", "#", "3", "3", "2"],["2", "4", "#", "2", "0"],["1", "#", "#", "2", "0"],["1", "2", "2", "1", "0"] ]Here is a tip. When checking adjacent positions to a specific position in the grid,the following table might assist you in determining adjacent indexes:NW position =current_row - 1current_col - 1N position =current_row - 1current_colNE position =current_row - 1current_col + 1W position =current_rowcurrent_col - 1Current position =current_rowcurrent_colE position…arrow_forwardThe following question requires to be done in Python. Thanks in advancearrow_forwardDuring the sockets lab we cast the second parameter from a struct sockaddr_in to a struct sockaddr. struct sockaddr_in address; /* ... */ bind(server_fd, (struct sockaddr*)&address, sizeof(address)) This provides a limited form of polymorphism: bind only needs the first few fields of its struct, and both struct sockaddr_in and struct sockaddr have the same fields at those addresses. Consider the following example: typedef struct { float x; float y; float z; } point3D; typedef struct { float x; float y; } point2D; float distance(point2D *a1, point2D *a2) { /* ... */ } int main() { point2D a = { .x = 2.1, .y = 3.0 }; point3D b = { .x = 4.1, .y = 4.3, .z = 4.0 }; // Call distance function with a and b } How would you pass a and b to the distance function? If several work, pick one that will not create warnings. A. distance(a, b); B. distance(a, (point2D)b); C. distance(&a, &b); D. distance(&a, &(point2D)b); E. distance(&a, (point2D*)&b); F. none of…arrow_forward
- Write the definition for a class named Vector2D that stores information about a two-dimensional vector. The class should have functions to get and set the x and y components, where x and y are integers.Next, overload the * operator so that it returns the dot product of two vectors. The dot product of two-dimensional vectors A and B is equal to(Ax * Bx) + (Ay * By).Finally, write a main subroutine that tests the * operation.arrow_forwardPython3arrow_forward1. The operation Perm(w), applied to a string w, is all strings that can be constructed by permuting the symbols of w in any order. For example, if w = 101, then Perm(w) is all strings with two 1's and one 0, i.e., Perm(w) = {101, 110,011}. If L is a regular language, then Perm(L) is the union of Perm(w) taken over all w in L. For example, if L is the language L(0*1*), then Perm(L) is all strings of 0's and 1's, i.e., L((0+1)*). If L is regular, Perm(L) is sometimes regular, sometimes context-free but not regular, and sometimes not even context-free. Consider each of the following regular expressions R below, and decide whether Perm(L(R)) is regular, context-free, or neither: 1. (01)* 2.0*+1* 3. (012)* 4. (01+2)* a) Perm(L((01+2)*)) is context-free but not regular. b) Perm(L(0*+1*)) is not context-free. c) Perm(L((012)*)) is context-free but not regular. d) Perm(L(0*+1*)) is context-free but not regular. 2. The language of regular expression (0+10)* is the set of all strings of O's and…arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning