Question: Write a collection of C functions that provide the functionality of a heap manager. These are the functions that you should provide: void heap_init(int num_pages_for_heap)

Write a collection of C functions that provide the functionality of a heap manager.

These are the functions that you should provide:

void heap_init(int num_pages_for_heap)

void *heap_alloc(int num_bytes_to_allocate)

void heap_free(void *pointer_to_area_to_free)

I will write a set of test programs which invoke your functions.

The program must manage the space in a set of pages which you obtain via the mmap system call. It should use the first-fit algorithm to allocate new space.

The program must align requests on 16-byte addresses.

The program will need to maintain data structures that minimally keep track of blocks of free space. These data structures should *NOT* be in the allocated pages themselves.

If the user frees two blocks of memory that are adjacent, your program should coalesce them.

If the user attempts to allocate more space than is available in an open free block, then heap_alloc should return NULL. # If the user attempts to heap_free space that was not obtained via heap_alloc # then heap_free should return silently without freeing the space.

Your program will NOT include a main procedure; that will be supplied to you in a source program named p2test.c It will provide the ability to test with the -hw command-line arg.

Your makefile should compile both your code and the source program named p2test.c linking them into into a single executable named p2

p2test.c:

#include #include #include

void heap_init(int num_pages_for_heap); void *heap_alloc(int num_bytes_to_allocate); void heap_free(void *pointer_to_area_to_free); // not used in this test

int main(int argc, char *argv[]) { char *p1, *p2, *p3, *p4, *p5, *p6;

if (argc > 1 && strcmp(argv[1],"-hw") == 0) { printf("hello world "); exit(0); }

heap_init(2);

p1 = (char *) heap_alloc(2000); if ((long int)p1 % 16 != 0) { printf("p1 bad %p pmod16 %d ",p1,((long int)p1)%16); exit(-1); } memset(p1,'X',2000);

p2 = (char *) heap_alloc(2000); if ((long int)p2 % 16 != 0) { printf("p2 bad %p pmod16 %d ",p2,((long int)p2)%16); exit(-1); } memset(p2,'X',2000);

p3 = (char *) heap_alloc(2000); if ((long int)p3 % 16 != 0) { printf("p3 bad %p pmod16 %d ",p3,((long int)p3)%16); exit(-1); } memset(p3,'X',2000);

p4 = (char *) heap_alloc(1000); if ((long int)p4 % 16 != 0) { printf("p4 bad %p pmod16 %d ",p4,((long int)p4)%16); exit(-1); } memset(p4,'X',1000);

p5 = (char *) heap_alloc(1000); if ((long int)p5 % 16 != 0) { printf("p5 bad %p pmod16 %d ",p5,((long int)p5)%16); exit(-1); } memset(p5,'X',1000);

p6 = (char *) heap_alloc(1500); // try 1500 first if (p6 != NULL) { printf("p6 should have been NULL, but is %p ",p6); exit(-1); }

p6 = (char *) heap_alloc(50); // then just get 50 if ((long int)p6 % 16 != 0) { printf("p6 bad %p pmod16 %d ",p6,((long int)p6)%16); exit(-1); } memset(p6,'X',50);

printf("DONE ");

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!