講解:CSCE 351、Dynamic Storage Allocator、C/C++、C/C++R|Matla

CSCE 351Malloc Assignment : Writing a Dynamic Storage AllocatorDue: See Canvas for more information1 IntroductionIn this programming assignment you will be writing a dynamic storage allocator for C programs, i.e., yourown version of the malloc, free and realloc routines. You are encouraged to explore the design spacecreatively and implement an allocator that is most importantly correct. It should also be memory efficientand fast.2 LogisticsSince this assignment is quite large, I will allow you to work in team. However, if you choose to work alone,you can do so for a 10% bonus. (I will add 10% to the total points you’ve earned.) If you want to work in alarge team with more than two members, you can do so for a 10% penalty. (I will detect 10% from the totalpoints your team has earned.) You will have a choice of building two systems. One will have the limit of 3members per team and the other will have the limit of 4 members per team. Any clarifications and revisionsto the assignment will be posted on canvas.3 Hand Out InstructionsStart by downloading malloc-assignment.zip to a protected directory in CSCE.UNL.EDU (Note that thesystem is CSCE and not CSE.). Then issue the command: unzip malloc-assignment.zip. Thiswill cause a number of files to be unpacked into the directory called malloc-assignment. The only file youwill be modifying and handing in is mm.c. The mdriver.c program is a driver program that allowsyou to evaluate the correctness and performance of your solution. Use the command make to generate thedriver code and run it with the command ./mdriver -V. (The -V flag displays helpful summary information.)./mdriver -lV also reports the performance of the dynamic memory management routinesfrom standard C library (glibc).Looking at the file mm.c you’ll notice a C structure team into which you should insert the requestedidentifying information about your team. Do this right away so you don’t forget.1When you have completed the lab, you will hand in only one file (mm.c), which contains your solution.4 How to Work on the LabYour dynamic storage allocator will consist of the following four functions, which are declared in mm.hand defined in mm.c.int mm_init(void);void *mm_malloc(size_t size);void mm_free(void *ptr);void *mm_realloc(void *ptr, size_t size);The mm.c file we are providing, implements a simple implicit list with no boundary tags. As such, traversingthe list can only be done in one direction. We only implement mm init and mm malloc. The latter isworking but poorly utilizing memory and operating slowly. Thus, when we use it with the provided memoryallocation traces, it can only pass 6 of the 11 tests. It fails 3 tests due to out-of-memory and 2 tests due tolack of support for mm realloc.Using this as a starting place, implement mm free and mm realloc. The semantic of each function isdescribed below. mm init: Before calling mm malloc mm realloc or mm free, the application program (i.e.,the trace-driven driver program that you will use to evaluate your implementation) calls mm init toperform any necessary initializations, such as allocating the initial heap area. The return value shouldbe -1 if there was a problem in performing the initialization, the staring address of the heap otherwise. mm malloc: The mm malloc routine returns a pointer to an allocated block payload of at leastsize bytes. The entire allocated block should lie within the heap region and should not overlap withany other allocated chunk.We will compare your implementation to the version of malloc supplied in the standard C library(glibc). Since the glibc malloc always returns payload pointers that are aligned to 8 bytes, yourmalloc implementation should do likewise and always return 8-byte aligned pointers. Our mdriverprogram tests for 8-byte alignment and terminates if the alignment check fails. mm free: The mm free routine frees the block pointed to by ptr. It returns nothing. This routineis only guaranteed to work correctly when the passed pointer (ptr) was returned by an earliercall to mm malloc or mm realloc and has not yet been freed. mm realloc: The mm realloc routine returns a pointer to an allocated region of at least sizebytes with the following constraints.– if ptr is NULL, the call is equivalent to mm malloc(size);– if size is equal to zero, the call is equivalent to mm free(ptr);2– if ptr is not NULL, it must have been returned by an earlier call to mm malloc or mm realloc.The call to mm realloc changes the size of the memory block pointed to by ptr (the oldblock) to size bytes and returns the address of the new block. Notice that the address of thenew block might be the same as the old block, or it might be different, depending on your implementation,the amount of internal fragmentation in the old block, and the size of the reallocrequest.The contents of the new block are the same as those of the old ptr block, up to the minimum ofthe old and new sizes. Everything else is uninitialized. For example, if the old block is 8 bytesand the new block is 12 bytes, then the first 8 bytes of the new block are identical to the first 8bytes of the old block and the last 4 bytes are uninitialized. Similarly, if the old block is 8 bytesand the new block is 4 bytes, then the contents of the new block are identical to the first 4 bytesof the old block.These semantics match the the semantics of the corresponding libc malloc, realloc, and free routines.Issue man malloc command to a shell for complete documentation.5 Heap Consistency CheckerDynamic memory allocators are notoriously tricky beasts to program correctly and efficiently. They aredifficult to program correctly because they involve a lot of untyped pointer manipulation. You will find itvery helpful to write a heap checker that scans the heap and checks it for consistency.Some basic functions that a heap checker should support are : Is every block in the free list marked as free? Are there any contiguous free blocks that somehow escaped coalescing? Is every free block actually in the free list? Do the pointers in the free list point to valid free blocks? Do any allocated blocks overlap? Do the pointers in a heap block point to valid heap addresses?We provide you with a heap checker (mm checkheap) that works with the current implementation ofmm malloc. That is, it can check the header information to identify the size of the block and its allocationstatus. You can use this to help debug your implementation. When you submit mm.c, make sure to removeany calls to mm check as they will slow down your throughput.6 Support RoutinesThe memlib.c package simulates the memory system for your dynamic memory allocator. You can invokethe following functions in memlib.c:3 void *mem sbrk(int incr): Expands the heap by incr bytes, where incr is a positivenon-zero integer and returns a generic pointer to the first byte of the newly allocated heap area. Thesemantics are identical to the Unix sbrk function, except that mem sbrk accepts only a positivenon-zero integer argument. 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. size t mem pagesize(void): Returns the system’s page size in bytes (4K on Linux systems).7 The Trace-driven Driver ProgramThe driver program mdriver.c in the malloc-assignment.zip distribution tests your mm.c packagefor correctness, space utilization, and throughput. The driver program is controlled by a set of trace filesthat are included in the malloc-assignment.zip distribution. Each trace file contains a sequence ofallocate, reallocate, and free directions that instruct the driver to call your mm malloc, mm realloc, andmm free routines in some sequence. The driver and the trace files are the same ones we will use when wegrade your handin mm.c file.The driver mdriver.c accepts the following command line arguments: -t : Look for the default trace files in directory tracedir instead of the defaultdirectory defined in config.h. -f : Use one particular tracefile for testing instead of the default set of trace-files. -h: Print a summary of the command line arguments. -l: Run and measure libc malloc in addition to the student’s malloc package. -v: Verbose output. Print a performance breakdown for each tracefile in a compact table. -V: More verbose output. Prints additional diagnostic information as each trace file is processed.Useful during debugging for determining which trace file is causing your malloc package to fail.8 Programming Rules You should not change any of the interfaces in mm.c. You should not invoke any memory-management related library calls or system calls. This excludesthe use of malloc, calloc, free, realloc, sbrk, brk or any variants of these calls in yourcode.4 For consistency with the libc代寫CSCE 351作業(yè)、代做Dynamic Storage Allocator作業(yè)、代寫C/C++程序作業(yè)、代做C/C malloc package, which returns blocks aligned on 8-byte boundaries,your allocator must always return pointers that are aligned to 8-byte boundaries. The driver willenforce this requirement for you. Over the years, we have collected implementations of similar work that have been released on theInternet. While it has been argued that your implementation may be similar to those that are publiclyavailable, we have made several modifications to the provided implementation of mm.c so thatyour implementation would be different from those that are out there. As such, we will use MOSS(https://theory.stanford.edu/?aiken/moss/) to compare your work against our collectionthat includes those programs available on the Internet and from our prior semester. If your implementationis flagged as being similar to our collection, you will be reported to CSCE Academic IntegrityCommittee for Investigation without exceptions. For more information about CSCE AcademicIntegrity Policies, please visit: https://cse.unl.edu/academic-integrity-policy.9 EvaluationYou will receive zero points if you break any of the rules or your code is buggy and crashes the driver.Otherwise, there are two levels of implementation that you can choose from. Since the maximum numbersof team members differ, you will need to choose when you sign up the team (if you have 4 members, youMUST build an Explicit List System).9.1 Implicit List System (maximum marks: 75 out of 100 points)The easier path to earn a decent amount of points is to simply extend the implicit list implementationprovided to you to support the missing functions. If you choose to build this system, you would only earnat most 75 out of 100 points but the amount of time you spend to complete the project is also significantlyless than that to build the more advanced system. The requirements for this system are: The system must not use boundary tags. Traversing the list can only go in one direction. You cannot change mm init and mm malloc. Simply add code to complete mm free and mm realloc. mm free must support coalescing. Without it, your system will not pass all 11 tests. mm realloc must support all conditions stated as part of the reallocation semantics. There is a limit of three members per team if you choose to build this system. Working alone stillearns you a 10% bonus. Working in pair is allowed for no bonus. Working in three is allowed but fora 10% penalty.Since this system will perform slowly, you will only be graded on correctness. To earn all 75 points, yoursystem must pass all 11 tests. You will receive partial credit for operating correctly on each trace.59.2 Explicit List System (maximum marks: 100 points)You explicit list system must support boundary tags and can traverse the heap in both forward and backwarddirections. It must support coalescing so that you can pass all tests. Please refer to lecture5.pptx for moreinformation about how to implement an explicit list system. The requirements for this system are: You cannot significantly change mm init. Ideally, you should only need to add a boundary tag tothe initial block. You can use the current empty payload of the first block for this purpose. You can choose a criterion to insert a free block into the list (e.g., LIFO or address ordered). Thecriterion should not affect correctness but can yield better performance for your system. This can leadto more performance points. Your mm realloc implementation must follow the previously stated semantics. mm free must support coalescing. Without it, your system will not pass all 11 tests. There is a limit of 4 members per team if you choose to build this system. Working alone still earnsyou a 10% bonus. Working in a team of 2 or 3 members is allowed for no bonus. Working in a teamwith 4 members is allowed but for a 10% penalty.Next, we describe the scoring rules for this system. Correctness (75 points). You will receive full points if your solution passes the correctness testsperformed by the driver program. You will receive partial credit for each correct trace. Performance (25 points). Two performance metrics will be used to evaluate your solution:– Space utilization: The peak ratio between the aggregate amount of memory used by the driver(i.e., allocated via mm malloc or mm realloc but not yet freed via mm free) and the sizeof the heap used by your allocator. The optimal ratio equals to 1. You should find good policiesto minimize fragmentation in order to make this ratio as close as possible to the optimal.– Throughput: The average number of operations completed per second.The driver program summarizes the performance of your allocator by computing a performance index,P, which is a weighted sum of the space utilization and throughputP = wU + (1 ? w) min ?1,TTlibc?where U is your space utilization, T is your throughput, and Tlibc is the estimated throughput of libcmalloc on your system on the default traces.1 The performance index favors space utilization overthroughput, with a default of w = 0.6.Observing that both memory and CPU cycles are expensive system resources, we adopt this formula toencourage balanced optimization of both memory utilization and throughput. Ideally, the performance1The value for Tlibc is a constant in the driver (600 Kops/s) that your instructor established when they configured the program.6index will reach P = w + (1 ? w) = 1 or 100%. Since each metric will contribute at most w and1 w to the performance index, respectively, you should not go to extremes to optimize either thememory utilization or the throughput only. To receive a good score, you must achieve a balancebetween utilization and throughput.10 Programming StyleYou can lose points for poorly designed and written programs. As such, adhere to the following stylingrules. Your code should be decomposed into functions and use as few global variables as possible. Your code should begin with a header comment that describes the structure of your free and allocatedblocks, the organization of the free list, and how your allocator manipulates the free list. You can usethe current header comment section in mm.c as an example. Each function should be preceded by aheader comment that describes what the function does. Each subroutine should have a header comment that describes what it does and how it does it. Your heap consistency checker mm checkheap should be thorough and well-documented. Whilewe will not test it, it can help your productivity during the development of your system.11 Handin Instructions1. You will submit your mm.c file via a web interface in Canvas.2. When testing your system, make sure to use CSCE. This will insure that the performance report youget from mdriver is representative of the performance report you will receive when we grade yoursolution.12 Hints Use the mdriver -f option. During initial development, using tiny trace files will simplify debuggingand testing. We have included two such trace files (short1,2-bal.rep) that you can use forinitial debugging. Use the mdriver -v and -V options. The -v option will give you a detailed summary for eachtrace file. The -V will also indicate when each trace file is read, which will help you isolate errors. Compile with gcc -g and use a debugger. A debugger will help you isolate and identify out ofbounds memory references.7 Understand the general concept of malloc implementations in the lecture notes. The lecture note hasa detailed example of a simple allocator based on an implicit free list (lecture4.pptx). The providedmm.c also has extensive comments. Use this is a point of departure. Don’t start working on yourallocator until you understand everything about the provided simple implicit list allocator. Encapsulate your pointer arithmetic in C preprocessor macros. Pointer arithmetic in memory managersis confusing and error-prone because of all the casting that is necessary. You can reduce thecomplexity significantly by writing macros for your pointer operations. We provide you with severalworking macros in memhelper.h. Use them as the starting point to write your own macros. Do your implementation in stages. The first 9 traces contain requests to malloc and free. Thelast 2 traces contain requests for realloc, malloc, and free. We recommend that you start bygetting your malloc and free routines working correctly and efficiently on the first 9 traces. Onlythen should you turn your attention to the realloc implementation. For starters, build reallocon top of your existing malloc and free implementations. But to get really good performance,you will need to build a stand-alone realloc. Use a profiler. You may find the gprof tool helpful for optimizing performance. Start early! It is possible to write an efficient malloc package with a few pages of code. However, wecan guarantee that it will be some of the most difficult and sophisticated code you have written so farin your career. So start early, and good luck!轉(zhuǎn)自:http://ass.3daixie.com/2018101920716137.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • By clicking to agree to this Schedule 2, which is hereby ...
    qaz0622閱讀 1,639評(píng)論 0 2
  • 單鏈表:只能索引后繼節(jié)點(diǎn),不能索引前驅(qū)節(jié)點(diǎn).到了尾部標(biāo)識(shí)就停止了.問(wèn)題:不從頭結(jié)點(diǎn),就無(wú)法訪問(wèn)到全局節(jié)點(diǎn) 循環(huán)鏈表...
    豆瓣奶茶閱讀 426評(píng)論 0 0
  • 《丑奴兒·書博山道中壁》辛棄疾 少年不識(shí)愁滋味,愛上層樓;愛上層樓,為賦新詞強(qiáng)說(shuō)愁。 而今識(shí)盡愁滋味,欲說(shuō)還休;欲...
    山河遠(yuǎn)闊tang閱讀 493評(píng)論 1 2
  • 狂風(fēng)從北面浩蕩而來(lái),卻因?yàn)槟仙降淖钃醵鵁o(wú)法往南而去。于是反反復(fù)復(fù)刮個(gè)沒完沒了。從昨天晚上開始直到現(xiàn)在,風(fēng)聲...
    知不道君閱讀 152評(píng)論 0 0
  • 1.計(jì)劃和目標(biāo)完成情況 (1)按照計(jì)劃,完成的事有哪些? 早睡早起,冥想 (2)計(jì)劃外做的有價(jià)值的事有哪些? 安全...
    春風(fēng)化雨77閱讀 127評(píng)論 0 0

友情鏈接更多精彩內(nèi)容