Csapp Six important experimental LAB5

Source: Internet
Author: User

Csapp && LAB5



Experimental instruction book:

http://download.csdn.net/detail/u011368821/7951657

Experimental materials:

http://download.csdn.net/detail/u011368821/8019293


To get this done, take a look at the notes, and review how block is organized. just look at the 11th section in link , the Dynamic memory allocation section will be able to

http://blog.csdn.net/cinmyheart/article/details/38136375

And then look at the study of block

http://blog.csdn.net/cinmyheart/article/details/38174421



The only file you'll modify and turn in is MM.C.


Your Dynamic storage allocator would consist of the following three functions (and several helper functions), which is Dec Lared in mm.h and defined in MM.C:

int mm_init (void); void* mm_malloc (size_t size); void Mm_free (void* ptr);


Your job is to complete this implementation by filling out mm_malloc () and Mm_free ().




The task is to use the existing API below to complement the Mm_malloc () and Mm_free ()

Provided Code


We define a blockinfo struct designed to being used as a node in a doubly-linked explicit free list, and the following functi ONS for manipulating free lists:


blockinfo* searchfreelist (int reqsize): Returns a block of at least the requested size if one exists (and NULL otherwise). void Insertfreeblock (blockinfo* blockinfo): Inserts the given block in the free list in LIFO manner.void Removefreeblock (B lockinfo* blockinfo): Removes the given block from the free list.


In addition, we implement mm_init and provide-helper functions implementing important parts of the allocator:


void requestmorespace (int incr): Enlarges the Heap by incr bytes (if enough memory are available on the machine to does so). V OID Coalescefreeblock (blockinfo* oldblock): coalesces any and free blocks adjacent in memory to oldblock into Asingle NE W large block and updates the free list accordingly.


Here is the implementation of the free list: here to explain that the header is in the low address, footer high address. The reason for this is that there will be a pointer addition and subtraction at the time of implementation.:-)

The size information recorded in the Sizeandtags is the entire block. Contains the size of the struct blockinfo struct body


Argument: Observing the distribution of struct blockinfo structure in memory


It is based on the characteristics of memory layout, here Blockcursor just want the current block header when subtracting a word_size will get what?

The footer of the previous block is the boundary tag, thus knowing the size of the previous block of the current block



My Answer:

/* Allocate a block of size size and return a pointer to it.  */void* Mm_malloc (size_t size) {size_t reqsize;  Blockinfo * Ptrfreeblock = NULL;  Blockinfo * Ptrnextblock = NULL;  size_t BlockSize;  size_t Precedingblockusetag;   size_t* ptrnextblockfooter = NULL;  Zero-size requests Get NULL.  if (size = = 0) {return NULL;  }//ADD One word for the initial size header.  Note that we don ' t need to boundary tags when the block is used!  Size + = Word_size;  if (size <= min_block_size) {//Make sure we allocate enough space for a blockinfo in case we//free this BLOCK    (When we free this block, we'll need to use the//next pointer, the Prev pointer, and the boundary tag).  Reqsize = min_block_size;  } else {//Round up for correct alignment reqsize = Alignment * ((size + ALIGNMENT-1)/alignment);  }//Implement mm_malloc.  You can change or remove any of the above//code.  It is included as a suggestion of the where to start. You'll want to ReplaCe this return statement ... ptrfreeblock = searchfreelist (reqsize);  if (!ptrfreeblock) {requestmorespace (reqsize);p trfreeblock = Searchfreelist (reqsize);  } blockSize = SIZE (ptrfreeblock->sizeandtags);  if (BlockSize < Reqsize + min_block_size) {reqsize = blockSize;  } Precedingblockusetag = Ptrfreeblock->sizeandtags & tag_preceding_used; Ptrfreeblock->sizeandtags = Reqsize | Precedingblockusetag |  tag_used;  Ptrnextblock = (blockinfo*) unscaled_pointer_add (ptrfreeblock,reqsize); if (reqsize! = blockSize) {ptrnextblock->sizeandtags = (blocksize-reqsize) | Tag_preceding_used;ptrnextblockfooter = (size_t *) Unscaled_pointer_add (ptrfreeblock,blocksize-word_size);(*  Ptrnextblockfooter) = Ptrnextblock->sizeandtags;insertfreeblock (Ptrnextblock); } else {ptrnextblock->sizeandtags = Ptrnextblock->sizeandtags | Tag_preceding_used;ptrnextblockfooter = (size_t*) unscaled_pointer_add (Ptrnextblock,size (ptrNextBlock-> Sizeandtags)-Word_size;(*ptrnextblOckfooter) = ptrnextblock->sizeandtags;    } removefreeblock (Ptrfreeblock); Return (void*) Unscaled_pointer_add (ptrfreeblock,word_size); }/* FREE The block referenced by PTR.   */void mm_free (void *ptr) {size_t payloadsize;  size_t BlockSize;  Blockinfo * BLOCKINFO;  Blockinfo * Followingblock;  Implement Mm_free.  You can change or remove the declaraions//above.  They is included as minor hints.  blockinfo* prev_block = NULL;  blockinfo* next_block = NULL;  Blockinfo = (blockinfo*) unscaled_pointer_sub (ptr,word_size);  Followingblock = (Blockinfo *) Unscaled_pointer_add (blockinfo,blockinfo->sizeandtags);  BlockSize = SIZE (blockinfo->sizeandtags);  Payloadsize = blocksize-word_size;  Blockinfo->sizeandtags = blockinfo->sizeandtags & ~tag_used;  * (size_t *) unscaled_pointer_add (blockinfo,payloadsize) = blockinfo->sizeandtags;  Followingblock->sizeandtags = followingblock->sizeandtags & ~tag_preceding_used; if (Followingblock->sizeandtags & tag_used = = 0) {* (size_t *) Unscaled_pointer_add (followingblock,size (followingblock->sizeandtags)-WORD_SIZE) =  followingblock->sizeandtags;  } insertfreeblock (Blockinfo); Coalescefreeblock (blockinfo);}




Finally executed./mdriver

There will be a bit of output





Off-topic, to pursue this malloc implementation of the words .... In fact, it is based on the already existing stdlib.h has been implemented by malloc, introduces the implementation principle, the use of two-way linked list



The extra exercises in the back show no patience, for realloc ...


To this, Csapp's lab temporarily, will be in the future peak-peak repair to update:-)

If you are interested in these six labs, please contact us for a chat

[Email protected]





Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Csapp Six important experimental LAB5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.