下面为大家整理一篇优秀的assignment代写范文- malloc lab,供大家参考学习。在本实验中,您将为 C++编写一个通用动态存储分配器;也就是你自己的malloc,free,realloc和calloc函数版本。
Introduction
In this lab you will be writing a general purpose dynamic storage allocator for C programs; that is, your own version of the malloc, free, realloc, and calloc functions. You are encouraged to explore the design space creatively and implement an allocator that is correct, efficient, and fast.
Required Functions and Support Routines
Your dynamic storage allocator will implement the following functions, declared in mm.h and defined in mm.c:
bool mm_init(void);
void *malloc(size_t size);
void free(void *ptr);
void *realloc(void *ptr, size_t size);
void *calloc(size_t nmemb, size_t size);
bool mm_checkheap(int);
We provide you three versions of memory allocators:
mm.c: A placeholder that compiles correctly, but does not run.
mm-naive.c: A functional implementation that runs fast but gets very poor utilization, because it never reuses any blocks of memory.
mm-baseline.c: A fully-functional implicit-list allocator. We recommend that you use this code as your starting point.
Your allocator must run correctly on a 64-bit machine. It must support a full 64-bit address space, even though current implementations of x86-64 machines support only a 48-bit address space. The driver mdriver-emulate will evaluate your program’s correctness using benchmark traces that require the use of a full 64-bit address space.
Your submitted mm.c must implement the following functions:
mm_init: Performs any necessary initializations, such as allocating the initial heap area. The return value should be falseif there was a problem in performing the initialization, true otherwise. You must reinitialize all of your data structures in this function, because the drivers call your mm_init function every time they begin a new trace to reset to an empty heap.
malloc: The malloc routine returns a pointer to an allocated block payload of at least size bytes. The entire allocated block should lie within the heap region and should not overlap with any other allocated block.
Your malloc implementation must always return 16-byte aligned pointers.
free: The free routine frees the block pointed to by ptr. It returns nothing. This routine is only guaranteed to work when the passed pointer was returned by an earlier call to malloc, calloc, or realloc and has not yet been freed. free(NULL) has no effect.
realloc: The realloc routine returns a pointer to an allocated region of at least size bytes with the following constraints:
if ptr is NULL, the call is equivalent to malloc(size);
if size is equal to zero, the call is equivalent to free(ptr) and should return NULL;
if ptr is not NULL, it must have been returned by an earlier call to malloc or realloc and not yet have been freed. The call to realloc takes an existing block of memory, pointed to by ptr — the old block. Upon return, the contents of the new block should be the same as those of the old block, up to the minimum of the old and new sizes. Everything else is uninitialized. Achieving this involves either copying the old bytes to a newly allocated region or reusing the existing region.
For example, if the old block is 16 bytes and the new block is 24 bytes, then the first 16 bytes of the new block are identical to the first 16 bytes of the old block and the last 8 bytes are uninitialized. Similarly, if the old block is 16 bytes and the new block is 8 bytes, then the contents of the new block are identical to the first 8 bytes of the old block.
The function returns a pointer to the resulting region. The return value might be the same as the old block—perhaps there is free space after the old block, or size is smaller than the old block size—or it might be different. If the call to realloc does not fail and the returned address is different than the address passed in, the old block has been freed and should not be used, freed, or passed to realloc again.
Hint: Your realloc implementation will have only minimal impact on measured throughput or utilization. A correct, simple implementation will suffice.
calloc: Allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero before returning.
Hint: Your calloc will not be graded on throughput or performance. A correct, simple implementation will suffice.
mm_checkheap: The mm_checkheap function (the heap consistency checker, or simply heap checker) scans the heap and checks it for possible errors (e.g., by making sure the headers and footers of each block are identical). Your heap checker should run silently until it detects some error in the heap. Then, and only then, should it print a message and return false. If it finds no errors, it should return true. It is very important that your heap checker run silently; otherwise, it will produce too much output to be useful on the large traces.
A quality heap checker is essential for debugging your malloc implementation. Many malloc bugs are too subtle to debug using conventional gdb techniques. The only effective technique for some of these bugs is to use a heap consistency checker. When you encounter a bug, you can isolate it with repeated calls to the consistency checker until you find the operation that corrupted your heap. Because of the importance of the consistency checker, it will be graded. If you ask members of the course staff for help, the first thing we will do is ask to see your checkheap function, so please write this function before coming to see us!
The mm_checkheap function takes a single integer argument that you can use any way you want. One very useful technique is to use this argument to pass in the line number of the call site:
mm_checkheap(__LINE__);
If mm_checkheap detects a problem with the heap, it can print the line number where mm_checkheap was called, which allows you to call mm_checkheap at numerous places in your code while you are debugging.
The semantics for malloc, realloc, calloc, and free match those of the corresponding libc routines. Type man mallocto the shell for complete documentation.
Support Routines
The memlib.c package simulates the memory system for your dynamic memory allocator. You can invoke the following functions in memlib.c:
void *mem_sbrk(intptr_t incr): Expands the heap by incr bytes, where incr is a non-negative integer, and returns a generic pointer to the first byte of the newly allocated heap area. The semantics are identical to the Unix sbrk function, except that mem_sbrk will fail for negative arguments. (Data type intptr_t is defined to be a signed integer large enough to hold a pointer. On our machines it is 64-bits long.)
void *mem_heap_lo(void): Returns a generic pointer to the first byte in the heap.
void *mem_heap_hi(void): Returns a generic pointer to the last byte in the heap.
size_t mem_heapsize(void): Returns the current size of the heap in bytes.
You are also allowed to use the following libc library functions: memcpy, memset, printf, fprintf, and sprintf. Other than these functions and the support routines, your mm.c code may not call any externally-defined function.
51due留学教育原创版权郑重声明:原创assignment代写范文源自编辑创作,未经官方许可,网站谢绝转载。对于侵权行为,未经同意的情况下,51Due有权追究法律责任。主要业务有assignment代写、essay代写、paper代写、cs代写服务。
51due为留学生提供最好的assignment代写服务,亲们可以进入主页了解和获取更多assignment代写范文 提供作业代写服务,详情可以咨询我们的客服QQ:800020041。