remzi-arpacidusseau / ostep-homework

1.99k stars 959 forks source link

Questions about homework #55

Open BILY5354 opened 1 year ago

BILY5354 commented 1 year ago
tessapower commented 1 year ago

@BILY5354 in this case, you need to consider the operations which have been run to get to this point and the constraints you have put in place through the command line args.

The allocator is configured to give out chunks of memory of size 4 (using the -a 4 flag), so the first step hands out a pointer to a chunk of memory of size 4, which satisfies the request with 1 unit of memory wasted.

The call to Free(ptr[0]) puts the memory chunk of size 4 allocated in the first step back into the free list, so you have two entries of size 4 and 96.

The third step allocates more memory, and hands out a pointer to a chunk of size 8 to satisfy the request for 5. Because you have specified to use the Best Fit Policy with -p BEST, the allocator will choose to split the chunk of memory which will best satisfy the request. In this case, it's the second entry in the free list with size 96, and the pointer returned will point to a chunk of memory of size 2 x 4 = 8, i.e., 96 - 8 = 88. The total amount of free memory in the free list is now 4 + 88 = 92.

More concretely:

ptr[0] = Alloc(3)
# Allocated a chunk of size 4, free list now has 1 entry of size 96.

free(ptr[0])
# Put back the chunk, free list now has two entries of size 4 + 96 = 100.

ptr[1] = Alloc(5)
# Allocated a chunk of size 8 by splitting the entry in the free list with size 96, free list now has two entries of size 4 + 88 = 92.

Hope that helps :)