Search for question
Question

Please complete the following: I will provide some test code as a .c file and a .h file. This should be in the Onedrive project directory. Please copy the contents of these files into your project, but keep them in their own .h and .c files. Your main() should call these. You are writing a simple memory manager. When initialized, it will allocate a single large block of memory (via mmap()(preferably, if on Unix) or otherwise malloc()) and then manage that memory, responding to requests to allocate and free blocks of memory as outlined below. Your code provides the following functions which are prototyped in the .h file I provide: • ⚫ void* memoryInitialize(memory Structure*, fscAllocation Method, size_t totalSize) Initializes the system by allocating size bytes of memory. Returns the base of the allocated memory (the address that the malloc returned) unless the malloc fails, in which case it returns 0. The allocation method describes what allocation heuristic to use. In our assignment, you can assume that this will ask to do first fit, where you split the memory and return the first part (adding the second part to the free list). The free list is kept in order. • void memoryCleanup(memoryStructure*) : indicates that I'm done for now, so you can free the block of memory allocated in memoryInitialize() and clear your internal state. • ⚫ void* fscMalloc(memoryStructure*, size_t blockSize): Cuts off an unused block of memory from the block allocated by memoryInitialize() and returns a starting address to the caller. If the block cannot be allocated, returns 0. Uses the first-fit algorithm • void fscFree(memoryStructure*, void* address) : Returns the memory pointed to by address to the internal free memory list kept by your system. Some important points: • You may only allocate memory once, and the allocated memory mustbe of size totalSize or of size totalSize + sizeof(<one of the header types>). No other allocations (mmap or malloc) are allowed and no other size allocations are allowed. • The memoryStructure is a piece of memory where you can store stuff (for example head, the head of the free list). • You must maintain your free list as a linked list, using the method in Chapter 17. • Each allocated (returned by fscMalloc()) block will have a header before the returned address that contains the size and a magic number (see figure 17.2 of your book). Magic numbers should be generated/used similar to the method we covered in our C language Linked List. You should shave off exactly the size of the header and return the beginning of the [formerly free] block the size of the bloc. I have also defined the header for you, similar to that on page 5 of chapter 17 in your book. • Note that the base (returned by memoryInitialize()) is not the first address returned by fscMalloc() since there's a header - the first address is base + sizeof(fsc_alloc_header_t)./n