Question: Please write the entire C code for this. For this assignment, you will be writing your own memory allocator. Writing a custom memory allocator is

Please write the entire C code for this. For this assignment, you will be writing your own memory allocator. Writing a custom memory allocator is something you might do if you work on performance sensitive systems (games, graphics, quantitative finance, embedded devices or any application you want to run fast!). Malloc and free are general purpose functions written to manage memory in the average use case quite well, but they can always be optimized for a given workload. That said, a lot of smart people have worked on making malloc/free quite performant over a wide range of workloads. Optimization aside, you might write an allocator to add in debugging features, and swap it in as needed.
Release History
You are always responsible for the latest release. The release may be updated at any time to fix bugs or add clarification. Get used to it, programmers are expected to adapt to changing requirements.
V004 : Added the requirement to submit a commented testing application.
V003 : Adjusted header size maximum to 32 bytes
V002 : Added limitation for header size.
V001 : Initial release, expect a few bugs.
Introduction
For this assignment, you will implement a portion of a custom memory allocator for the C language. You will write your own versions of:
malloc
free
In addition you will write a utility to dump the state of your currently allocated memory and you will also need to write an initialization routine that establishes the allocation policy.
It's tempting to think of malloc() and free() as OS system calls, because we usually think of memory management as being a low-level activity, but they are actually part of the C standard library. When you call malloc(), you are actually calling a C routine that may make system calls to ask the OS to modify the heap portion of the process's virtual address space if necessary.
To be clear, the memory allocator operates entirely within the virtual address space of a single process and knows nothing about which physical pages have been allocated to this process or the mapping from logical addresses to physical addresses; that part is handled by the operating system.
Memory allocators have two distinct tasks. First, the memory allocator asks the operating system to expand the heap portion of the process's address space by calling either the sbrk or mmap system call. Second, the memory allocator doles out this memory to the calling process. To do this, the allocator maintains a free list of available memory. When the process calls malloc(), the allocator searches the free list to find a contiguous chunk of memory large enough to satisfy the user's request. Freeing memory adds the chunk back into the free list, making it available to be allocated again by a future call to malloc().
When implementing this basic functionality in your project, we have a few guidelines. First, when requesting memory from the OS, you must use mmap()(which is easier to use than sbrk()). Second, although a real memory allocator requests more memory from the OS whenever it can't satisfy a request from the user, your memory allocator must call mmap() only one time (when it is first initialized). This is a simplification to keep this project manageable.
The functions malloc() and free() are defined as given below.
void *malloc(size_t size): malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared.
void free(void *ptr): free() frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc()(or calloc() or realloc()). Otherwise, or if free(ptr) has already been called before, undefined behaviour occurs. If ptr is NULL, no operation is performed.
You will implement the following routines in your code
umalloc : has the same interface as malloc
ufree : has the same interface as free
Described in more detail below
umemdump : prints which regions are currently free and is primarily to aid your debugging.
umeminit : initializes your memory allocator.
Program Specifications
For this project, you will be implementing several different routines. Note that your main() routine must be in a separate file called main.c. Do not include a main function in your umem.c file; you should implement this only in your testing file main.c. You will submit both files for grading. Your umem.c file will be evaluated for functionality and will be linked to our testing code. Your main.c file will be evaluated for thoroughness and attention to detail in terms of testing.
We have provided the prototypes for these functions in the file umem.h. Download umem.h.you should include this header file in your code to ensure that you are adhering to the specification exactly. You should not change umem.h in any way!
int umeminit (size_t sizeOfRegion, int allocationAlgo): umeminit is called one time by a process using your routines. sizeOfRegion is the number of bytes that you should request from the OS using mmap(). Do not define your own constants for the allocation algorithm

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 Accounting Questions!