” ICS 53课程 写作、 辅导C++程序ICS 53, Winter 2021Assignment 4: A Memory AllocatorYou will write a program which maintains a heap. Your program will allow a user to allocatememory, free memory, And see the current state of the heap. Your program will accept usercommands and execute them.I. Assumptions about the heapThe heap is 127 bytes long and memory is byte-addressable. The first address of the heap isaddress 0, so the last address of the heap is address 126. When we refer to a pointer in thisassignment we mean an address in memory. All pointers should therefore be values between 0and 126.The heap will be organized as an implicit free list. The heap is initially completelyunallocated, so it Should contain a single free block which is as big as the entire heap. Memoryis initialized so that all addresses (other than the header and the footer of the initial free block)contain 0. Each block should have a header and a footer which is a single byte, and the headerand footer byte should be contained in memory, just before the payload of the block(header)and after payload of a block (footer). The most-significant 7 bits of the header and footershould indicate the size of the block, including the header and footer itself. The leastsignificant bit of the Header and footer should indicate the allocation of the block: 0 for free,1 for allocated. The header for the first block (the initial single free block) must be placed ataddress 0 in memory and the footer of the first block must be placed at address 126.You cannot assume that the blocks are aligned. This means that the start address of a block canbe any address in the heap.II. OperationsYour program should provide a prompt to the user () and accept the followingcommands. Your program should repeatedly accept commands until the user enters quit. Youonly need to support These commands and can safely assume they will be entered andformatted in the manner shown below.1. malloc int size – This operation allows the user to allocate a block of memory fromyour heap. This operation should take one argument, the number of bytes which theuser wants in the payload of the allocated block. This operation should print out apointer which is the first address of the payload of the allocated block.Example:malloc 10 // Comment: header at 0, payload from 1-10,footer at 111malloc 5 // Comment: header at 12, payload from 13-17,footer at 1813malloc 2 // Comment: header at 19, payload from 20-21,footer at 22202. free int index- This operation allows the user to free a block of memory. Thisoperation takes one argument, the pointer to the start of the payload of a previouslyallocated block of memory. You Can assume that the argument is a correct pointer tothe payload of an allocated block.Example:malloc 101malloc 513free 13free 13. blocklist – This operation prints out information about all the blocks in your heap. Theinformation about blocks should be printed in the order that the blocks are contained inthe heap. The following information should be printed about each block: pointer to thestart of the payload, payload size, and the allocation status (allocated of free). Allthree items of information about a single block should be printed on a single line andshould be separated by commas.Example:malloc 101malloc 513blocklist1, 10, allocated.13, 5, allocated.20, 106, free.4. writemem int index, char * str This operation writes alpha-numeric charactersinto memory. The operation takes two arguments. The first argument is a pointer to thelocation in memory and the second argument is a sequence of alpha-numeric characterswhich will be written into memory, starting at the address indicated by the pointer. Thefirst character will be Written into the address indicated by the pointer, and eachcharacter thereafter will be written into the neighboring addresses sequentially. Forexample, the operation writemem 3 abc will write an a into address 3, a b intoaddress 4, and a c into address 5. Additionally, if a block is freed, you must ensurethat whatever was written to the block is reset back to 0. The index can be any locationin the heap. Since C does not do explicit bounds-checking, the writmem command foryour simulator can very well overwrite parts of another blocks even headers andfooters and payloads of other allocated/free blocks. You are not required to dobounds checking for your heap as well. Operation like these will destroy the heapstructure and are permitted operations. This can then corrupt subsequent operationsas well.5. printmem int index, int number_of_characters_to_print This operation printsout a segment of memory in hexadecimal. The operation takes two arguments. The firstargument is a pointer to the first location in memory to print, and the second argumentis an integer indicating how many addresses to print. The contents of all addresses willbe printed on a single line and separated by a single space. The index along withnumber of characters can very well exceed block sizes. Like writemem, you are notrequired to do bounds Checking.Example:writemem 5 ABCprintmem 5 341 42 43Notice that the values 41, 42, and 43 are the hexadecimal representations of the ASCIIvalues of the characters A, B, and C.6. quit This quits your program.III. Requirements about allocation and freeing of memory.When a block is requested which is smaller than any existing block in the heap, then yourcode must perform splitting to create a new block of the appropriate size.When a block is freed, it must be coalesced with the next block if the next block is freeand the previous block if the previous block is free. (Forward and Backward Coalescing needsto be supported by your code)When searching for a block to allocate, be sure to use the first-fit allocation strategy.IV. Developing TestcasesTesting is an important part of the programming process, so you are required to developyour own test cases. It is up to you to consider all the possibilities that can occur, within thelimits of the specification, and make your test cases based on that. We will grade your testcases based on coverage, i.e., you should come up with the test cases such that the wholesuite of test cases is capable of 100% code coverage, if you have a question about how theprogram is expected to Perform, come to office hours and ask the professor, or go to lab andask a TA. We have created a Python script called autocov.py which you should download withthe assignment and use before you submit your assignment in order to ensure that you haveachieved 100%-line coverage. The autocov.py script will compile and execute your code with aset of test cases Which you define, and it will report the line coverage achieved by the test case.Do not modify the Autocov.py script which we provide.Test RunsTesting is performed by executing your program several times with different test data.Each test execution of your code is referred to as a test run (run for short) and the testdata associated with each run must be stored in a .run file. A .run file will contain all thetest data which will be supplied to your program during a single test execution. A .runfile is a text file describing all the inputs supplied to your program for a single execution.The name of each .run file must have the suffix .run, but the prefix does not matter.Each line of a .run file can be no longer than 80 characters.Input to autocov.py.The autocov.py script requires your access to the source code of your program and theset of .run files to be used during testing. Your source code file must be named hw.c.This is important; it is the only file which will be compiled by the script. Any number of.run files may be provided. All these files, hw.c and all of your .run files, must bezipped together into a single zip file named hw.zip. You can create the zip file usingthe zip command on linux. For example, if you have two .run files called t1.run andt2.run then you could create the zip file with the following command: zip hw.zip hw.ct1.run t2.run. The hw.zip file is the only input to the autocov.py script.Running autocov.pyIn order to execute the script, the autocov.py file must be placed in the same directoryas the hw.zip file and another directory called hw. The hw directory will be used bythe script to compile and run your program, so it should be empty before running thescript. The script needs to be run using a Python interpreter version 3.6.8 or later. Onthe openlab systems the command python3 invokes the Python 3.x interpreter, so youcan run the autocov.py script by entering the command python3 autocov.py. Thescript will print several lines To the screen, including program outputs, but the secondto-lastline should look like this, Lines executed: X% of Y, where X is the line coverageand Y is the number of lines in your program.V. Submission Instructions:Your source code must be a single c file named hw.c containing your solution and any numberof run files containing your suite of test cases zipped into 1 file named hw.zip. Be sure thatyour program must Compile on openlab.ics.uci.edu using gcc version 4.8.5 with no compilerswitches other than gcov switches.Submissions will be done through Gradescope. If you are working with a partner, you mustindicate that through Gradescope. The first line of your submitted file should be a commentwhich includes the name and ID number of you and your partner (if you are working with apartner).VI. Sample Executionmalloc 101malloc 513blocklist1, 10, allocated.13, 5, Allocated.20, 106, free.free 1blocklist1, 10, free.13, 5, allocated.20, 106, free.malloc 51blocklist1, 5, allocated.8, 3, free.13, 5, allocated.20, 106, free.writemem 1 HELLOprintmem 1 548 45 4C 4C 4Ffree 13blocklist // Comment: You can see here that there was ablock allocated between 2 free blocks, on freeing that allocatedblock, Both the free blocks were coalesced into one free block.1, 5, allocated.8, 118, free.quit$ – back to bash Prompt.如有需要,请加QQ:99515681 或WX:codehelp
“
添加老师微信回复‘’官网 辅导‘’获取专业老师帮助,或点击联系老师1对1在线指导。