EssayGhost Assignment代写,Essay代写,网课代修,Quiz代考

EssayGhost-Essay代写,作业代写,网课代修代上,cs代写代考

一站式网课代修,论文代写

高质量的Assignment代写、Paper代写、Report代写服务

EG1hao
网课代修代上,cs代写代考
C语言代写
您的位置: 主页 > 编程案例 > C语言代写 >
代写C语言留学生C++英文题目:A Basic Memory Management System - C++代写
发布时间:2021-07-23 11:27:13浏览次数:
Part 1 A Basic Memory Management SystemThe aim of Part 1 is to implement a simple, first-fit dynamic memory management policy on an allocatable region of memory,M1. The idea is to write two routines calledMalloc()andFree()that operate onM1, which is established by the operating system. This is the basis for how heap-based memory management works for applications running on a real system, or a runtime environment such as a Java virtual machine.ApproachYou need to create the following functions, with *exactly* the type declaration as shown:void Init (size_t size) { addrs_t baseptr; /* Use the systemmalloc()routine (ornewin C++)onlyto allocatesizebytes for the initial memory area,M1.baseptris the starting address ofM1. */ baseptr = (addrs_t) malloc (size); Perform any other initialization here.}addrs_t Malloc (size_t size) { Implement your own memory allocation routine here. This should allocate the first contiguoussizebytes available inM1. Since some machine architectures are 64-bit, it should be safe to allocate space starting at the first address divisible by 8. Hence align all addresses on 8-byte boundaries! If enough space exists, allocate space and return the base address of the memory. If insufficient space exists, return NULL.}void Free (addrs_t addr) { This frees the previously allocatedsizebytes starting from addressaddrin the memory area,M1. You can assume thesizeargument is stored in a data structure after theMalloc()routine has been called, just as with the UNIX free() command.}addrs_t Put (any_t data, size_t size) { Allocatesizebytes fromM1usingMalloc(). Copysizebytes ofdatainto Malloc d memory. You can assumedatais a storage area outsideM1. Return starting address ofdatain Malloc d memory.}void Get (any_t return_data, addrs_t addr, size_t size) { Copysizebytes fromaddrin the memory area,M1, todataaddress. As withPut(), you can assumedatais a storage area outsideM1. De-allocate size bytes of memory starting fromaddrusingFree().}Additionally, you will need to keep afree-listof allocated and available slots in the memory area created byInit(). Moreover, when a block of memory is freed, it must be merged together with other contiguous free memory (either side of the block being freed). As memory is allocated, the memory area,M1, is fragmented and the free-list should keep track of these fragments and their sizes.WARNING: make sure all Malloc d addresses are 8 bytes aligned, to avoid possible address errors which the compiler cannot detect.TestingYou should test your basic memory management system by:·allocating space for a number of variable-length messages,·placing these messages into the allocatable memory area, beginning at the address returned from yourMalloc()routine,·removing each message from the memory area, copying it to local address space (outside the memory area), freeing up the space occupied by the message in the memory area (using your ownFree()routine), and finally displaying the message.The following type of test code should appear in your main() function, although you will realistically want to stress test your heap management system with a greater range of requests:typedef char *addrs_t;typedef void *any_t;void main (int argc, char **argv) { int i, n; char s[80]; addrs_t addr1, addr2; char data[80]; int mem_size = DEFAULT_MEM_SIZE; // Set DEFAULT_MEM_SIZE to 1 20 bytes for a heap region if (argc 2) { fprintf (stderr, Usage: %s [memory area size in bytes]\n , argv[0]); exit (1); } else if (argc == 2) mem_size = atoi (argv[1]); Init (mem_size); for (i = 0;; i++) { n = sprintf (s, String 1, the current count is %d\n , i); addr1 = Put (s, n+1); addr2 = Put (s, n+1); if (addr1) printf ( Data at %x is: %s , addr1, addr1); if (addr2) printf ( Data at %x is: %s , addr2, addr2); if (addr2) Get ((any_t)data, addr2, n+1); if (addr1) Get ((any_t)data, addr1, n+1); }}Part 2 A Virtualized Heap Allocation SchemeFor this part of the assignment, you are now tasked with the development of two new functions,VMalloc()andVFree()that operate on a new heap memory area,M2. Significantly, these two functions have the following prototypes:addrs_t *VMalloc (size_t size);void VFree (addrs_t *addr);These functions behave almost identically to Malloc() and Free(), except they work with POINTERS to addrs_t. Specifically, these pointers go to aredirection tablethat is an array ofRpointers to allocated blocks of memory within the heap. Each pointer returned by VMalloc(), and later passed to VFree(), points to a specific entry in the redirection table. The redirection table has one pointer entry for each allocated block of memory requested by a prior VMalloc(), but which has not yet been freed.For every call to VMalloc() or VFree(), the memory allocator must compact all current allocations to consume the least amount of space in the heap region. This means moving those allocations (and their contents) to different heap regions that are aligned on 8-byte boundaries. Once an allocation is moved, the address referring to it in the redirection table needs to be updated to keep track of where it is located. This means that, aside for unusable chunks of memory used to pad data on aligned address boundaries, there is ONE large chunk of contiguous memory that is currently unallocated. Having your memory allocator keep track of the size of this free heap region will quickly identify whether or not your heap can satisfy a subsequent VMalloc() request for a given size.In essence, VMalloc() and VFree() are similar to Malloc() and Free() except they perform compaction and move allocated data around the heap to make way for one large chunk of unallocated memory. This should mean they are slower than Malloc() and Free() but can potentially lead to more dynamic memory requests being satisfied.Example Redirection Table (for use with VMalloc() and VFree())Final TestingFor final testing, we will provide a sequence of allocation and deallocation requests on two regions of memory (M1andM2) of some specific size. You will then compare Malloc()/Free()[Case 1]against VMalloc()/VFree()[Case 2]for each of these requests.You should use the following code to measure the clock cycles for your requests. We will give you a test template at a later date with this timing code around a series of requests. You should attempt to come up with the most efficient ways of implementing Parts 1 and 2.#define rdtsc(x) __asm__ __volatile__( rdtsc \n\t : =A (*(x)))unsigned long long start, finish;rdtsc( start);// Insert your code here that you wish to timerdtsc( finish);Note also for Part 2, you will need to create two functions VPut() and VGet() to test your VMalloc() and VFree().The function prototypes for VPut() and VGet() are:addrs_t *VPut (any_t data, size_t size) { Allocatesizebytes fromM2usingVMalloc(). Copysizebytes ofdatainto Malloc d memory. You can assumedatais a storage area outsideM2. Returnpointer to redirection tablefor Malloc d memory.}void VGet (any_t return_data, addrs_t *addr, size_t size) { Copysizebytes fromthe memory area,M2, todataaddress. The addr argument specifies a pointer to a redirection table entry. As withVPut(), you can assumedatais a storage area outsideM2. Finally, de-allocate size bytes of memoryusing VFree()withaddrpointing to a redirection table entry.}Data Structures and Heap CheckerYou are free to choose whatever data structures you need to manage the size of each allocated chunk of data from your heap. You will somehow need to track the size of an allocation so that a subsequent free of that memory will de-allocate the right number of bytes.**You must allocate space within the heap for any data structures you use to track the allocation of space and which parts of the heap are free. However, the redirection table in the above diagram is assumed to be OUTSIDE the heap area.**You should also pay attention to how you combine free blocks into single larger blocks, when two or more contiguous blocks are formed. Issues such as internal fragmentation (due to padding and alignment) and external fragmentation (due to small-sized unallocated blocks) should be considered. Also, the data structure you use to track free and allocated blocks is up to you.You should develop a heap checker program that reports for each Malloc/Free/VMalloc/VFree call the state of the heap. The state of the heap should identify the following in *exactly* the format requested, where XXXX is replaced by a 64-bit long integer: Number of allocated blocks : XXXXNumber of free blocks : XXXX(discounting padding bytes)Raw total number of bytes allocated : XXXX(which is the actual total bytes requested)Padded total number of bytes allocated : XXXX(which is the total bytes requested plus internally fragmented blocks wasted due to padding/alignment)Raw total number of bytes free : XXXXAligned total number of bytes free : XXXX(which is sizeof(M1) minus the padded total number of bytes allocated. You should account for meta-datastructures insideM1also)Total number of Malloc requests : XXXXTotal number of Free requests: XXXXTotal number of request failures: XXXX(which were unable to satisfy the allocation or de-allocation requests)Average clock cycles for a Malloc request: XXXXAverage clock cycles for a Free request: XXXXTotal clock cycles for all requests: XXXX Number of allocated blocks : XXXXNumber of free blocks : XXXXRaw total number of bytes allocated : XXXXPadded total number of bytes allocated : XXXXRaw total number of bytes free : XXXXAligned total number of bytes free : XXXX(which is sizeof(M2) minus the padded total number of bytes allocated. You should account for meta-datastructures insideM2also)Total number of VMalloc requests : XXXXTotal number of VFree requests: XXXXTotal number of request failures: XXXXAverage clock cycles for a VMalloc request: XXXXAverage clock cycles for a VFree request: XXXXTotal clock cycles for all requests: XXXXNOTE: You do *not* need to include in your heap checker output anything shown above in parentheses. Additionally, the invariant with Part 2 (using the redirection table) is that for every memory request there should be *one* free block, discounting small unusable padding blocks. Depending on the tested requests, some of the above values may be 0.GradingGrading will check for:·Proper implementation of Malloc()/Free() and VMalloc()/VFree()·No memory leaks, first-fit policy, no address errors·No segmentation faults, correct address alignment and range checking·Correctly implemented free-list and/or other data structures as appropriate·Program style and comments·Performance/Efficiency·Correct heap checker output