
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 solution
Chapter 9 Solutions
Computer Systems: A Programmer's Perspective Plus Mastering Engineering With Pearson Etext -- Access Card Package (3rd Edition)
- Although color television was not added to the industry standard until 1956, CBS had been broadcasting selected special events in color as early as 1950. Question 1 options: True False Two key factors in creating the Network Era of American television were the FCC licensing freeze and ______________. Question 4 options: The Quiz Show Scandals Habitual Viewing Operation Frontal Lobes Drop-In Viewing Least Objectionable Programming was designed to embrace the public service-oriented vision of using television to elevate mass culture and enrich viewers. Question 6 options: True False By the end of the 1950s, all three remaining networks (NBC, CBS, & ABC) were broadcasting their entire nightly programming schedule in full color. Question 9 options: True Falsearrow_forward7. See the code below and solve the following. public class Test { public static void main(String[] args) { int result = 0; } result = fn(2,3); System.out.println("The result is: + result); // fn(x, 1) = x // fn(x, y) = fn(x, y-1) + 2, when y>1 public static int fn(int x, int y) { if (x <= 1) return x; else return fn(x, y-1) + 2; } } 7-1. This program has a bug that leads to infinite recursion. Modify fn(int x, int y) method to fix the problem. (2 point) 7-2. Manually trace the recursive call, fn(2,3) and show the output (step by step). (2 point) 7-3. Can you identify the Base Case in recursive method fn(int x, int y)? (1 point)arrow_forward6. See the code below and solve the following. import java.io.*; public class DataStream { } public static void main(String[] args) } DataOutputStream output = new DataOutputStream(new FileOutputStream("temp.dat")); output.writeUTF("Book1"); output.writeInt(85); output.writeUTF("Book2"); output.writeInt(125); output.writeUTF("Book3"); output.writeInt(70); output.close(); // ToDo: Read all data from temp.dat and print the data to the standard output (monitor) 6-1. This program has a compile error, and the message is “Unhandled exception type FileNotFoundException". How do you fix this error? (1 point) 6-2. Is FileNotFoundException a checked exception or an unchecked exception? (1 point) 6-3. What is the difference between checked exception and unchecked exception? (1 point) 6-4. Please complete the above program by reading all data from temp.dat and print the data to the standard output (monitor) by using System.out.print, System.out.println or System.out.printf method. (2 points)arrow_forward
- Write a program that reads a list of integers from input and determines if the list is a palindrome (values are identical from first to last and last to first). The input begins with an integer indicating the length of the list that follows. Assume the list will contain a maximum of 20 integers. Output "yes" if the list is a palindrome and "no" otherwise. The output ends with a newline. Hints: - use a for loop to populate the array based on the specified size (the first number entered) - use a for loop to check first value with last value, second value with second from end, etc. - if the values do not match, set a Boolean variable to flag which statement to output (yes or no) Ex: If the input is (remember to include spaces between the numbers): 6 1 5 9 9 5 1 the output is: yes Ex: If the input is: 5 1 2 3 4 5 the output is: C++ codingarrow_forwardDesign and draw a high-level "as-is" process diagram that illustrates a current process related to a product or service offered through the SSDCI.gov database.arrow_forwardCompare last-mile connections for connecting homes and businesses to the Internetarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning
- New Perspectives on HTML5, CSS3, and JavaScriptComputer ScienceISBN:9781305503922Author:Patrick M. CareyPublisher:Cengage LearningProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage



