For this assignment, you will be given the structure for a simple shared library that is used in place of the heap memory allocation functions malloc() and free(). You'll code two functions in this library, named Mem_Alloc() and Mem_Free().
Memory Allocation BACKGROUND
Memory allocators have two distinct tasks. First, the memory allocator uses the sbrk() system call to ask the operating system to expand the heap segment of the its virtual address space. However in the code we've provided, we use mmap() to simulate a process's heap in the memory-mapped segment. Second, the memory allocator manages this memory by maintaining the internal structure of the heap including tracking the size and status of each block. When the process makes a request for heap memory, the allocator searches its list of heap blocks for one that is large enough to satisfy the request. The chosen block may be split into two smaller ones before having its status set to allocated. Later when process frees memory, the allocator changes the block's status to freed and may coalesce it with free neighbors to make larger blocks.
This memory allocator is usually provided as part of a standard library rather than being part of the operating system. Thus, the memory allocator operates entirely within the virtual address space of a single process and knows nothing about which physical memory pages have been allocated to this process or the mapping from virtual addresses to physical addresses.
The C programming language defines its allocator with the functions malloc() and free() found in "stdlib.h" as follows:
void *malloc(size_t s): allocates s bytes and returns a pointer to the allocated memory. The memory is not cleared.
void free(void *ptr): frees the memory pointed to by ptr that was previously allocated by malloc() (or calloc() or realloc()). The behaviour is undefined if ptr is a stray pointer or if an attempt is made to free an allocation more than once. If ptr is NULL, the function does nothing and simply returns.
UNDERSTAND the Code
Copy the entire contents from following directory into your working directory for this assignment:
In this directory you'll find the files named "Makefile", "mem.c" and "mem.h" as well as a directory named "tests". In "mem.c" is the completed code for two functions: Mem_Init(int sizeOfRegion) and Mem_Dump(). Look at these functions to understand what they do and how they do it. Also note the global block header pointer first_block is the head of our list of memory blocks, where each block would be marked as either free or allocated. Very carefully read the comments for the provided block tag structure to understand the conventions used in this program. These functions we've completed are described below:
This function sets up and initializes the "heap" space that the allocator will manage. sizeOfRegion is the number of bytes desired for the heap.
This function should be called once at the start of any main program before calling any of the other allocator functions. In your main programs to test your code call this function first to initialize enough space so that subsequent calls to Mem_Alloc() function properly. The test main programs we've provided (discussed below) already do this.
8 freelancers are bidding on average ₹360/hour for this job
Hi There, Having good knowledge in C and operating systems. I can do the project of memory allocators in relatively short duration. I am wilking ti it. Please message me for further discussions. Regards, Sneha