Programming for Hardware/Software Systems Study Guide: Heap Management
Transcription
Programming for Hardware/Software Systems Study Guide: Heap Management
ECE 2035 Study Guide: Part 5 Version 1.0 Programming for Hardware/Software Systems Study Guide: Heap Management This study guide provides example problems on material from the class outline. It is valuable in helping you test your understanding of concepts from lecture. Many of these problems are from old exams; so they also provide good exam preparation. Solutions are provided to verify your answers. If any errors are detected, please send mail to [email protected] so future versions can be corrected. Good Luck! Version 1.0 November 2012 Copyright © 2012 1 ECE 2035 Study Guide: Part 5 Version 1.0 Problem HM-1 (2 parts) Hash Tables Consider an open hash table composed of a four-bucket table, with each bucket containing a variable length list. Each list entry has three slots <key, value, next> corresponding to the three word groupings in the entries section. The hash function is key mod four. Inserted entries are appended to the end of a bucket list. Deallocated entries are maintained on a LIFO free list. When the free list is empty, new entry objects are allocated from heap memory. Accesses are listed as <op, key, [value]>. Simulate the access list below and draw the ending state. Assume the hash table is initially empty, the heap pointer is initially 5016 and the free pointer is initially 0. Part A: 5016 Heap Pointer Free List 0000 Buckets 5000 5004 5008 5012 Entries 5016 5040 5064 5088 5020 5044 5068 5092 5024 5048 5072 5096 5028 5052 5076 5100 5032 5056 5080 5104 5036 5060 5084 5108 # 1 2 3 4 op insert insert insert insert key 1006 1000 1004 1006 Hash Table Access Trace value # op 111 5 insert 222 6 remove 333 7 remove 444 8 insert 2 key 1002 1000 1002 1003 value 555 n/a n/a 666 ECE 2035 Study Guide: Part 5 Version 1.0 Part B: 5016 Heap Pointer Free List 0000 Buckets 5000 5004 5008 5012 Entries 5016 5040 5064 5088 5020 5044 5068 5092 5024 5048 5072 5096 5028 5052 5076 5100 5032 5056 5080 5104 5036 5060 5084 5108 # 1 2 3 4 op insert insert insert insert key 1001 1005 1003 1007 Hash Table Access Trace value # op 111 5 remove 222 6 remove 333 7 insert 444 8 insert 3 key 1001 1007 1003 1006 value n/a n/a 555 666 ECE 2035 Study Guide: Part 5 Problem HM-2 (6 parts) Version 1.0 Heap Management Below is a snapshot of heap storage. Values that are pointers are denoted with a “$”. The heap pointer is $6172 and the free pointer is $6032. The heap has been allocated contiguously beginning at $6000, with no gaps between objects. addr value addr value addr value addr value addr value addr value 6000 12 6032 $6100 6064 $6148 6096 12 6128 12 6160 0 6004 48 6036 8 6068 4 6100 $6080 6132 4 6164 4 6008 4 6040 24 6072 12 6104 16 6136 4 6168 0 6012 16 6044 4 6076 8 6108 4 $6140 0 6172 0 6016 8 6048 8 6080 $6052 6112 8 6144 16 6176 0 6020 8 6052 $6140 6084 0 6116 8 6148 0 6180 0 6024 24 6056 8 6088 4 6120 16 6152 8 6184 0 6028 4 6060 12 6092 $6052 6124 8 6156 12 6188 0 Part A Assuming the heap has been allocated contiguously beginning at 6000, with no gaps between objects, circle all object size words in the map above. Part B List the address of the first data word in each object in the free list in order, starting with the head of the free list. Free List: Part C Based on the free list created in part B, if an object of size 7 bytes is allocated, what address will be returned using a first-fit allocation strategy? Address: Part D Based on the free list created in part B, if an object of size 7 bytes is allocated, what address will be returned using a best-fit allocation strategy? Address: Part E Based on the free list created in part B, if an object of size 15 bytes is allocated, what address will be returned using a best-fit allocation strategy? Address: Part F If the next instruction is free($6064), what memory changes will result? What will the free pointer become? memory changes free ptr 4 ECE 2035 Study Guide: Part 5 Problem HM-3 (2 parts) #define #define #define #define #define INITNUMBUCKETS LINKBLOCKSIZE RESIZERATIO NOMATCH DEBUG typedef struct int int struct Link } Link; Link { Key; Value; *Next; typedef struct Link int Link int } HashTable; { **Buckets; NumBuckets; *FreeLinks; Size; Version 1.0 Hash Table Implementation 5 16 1 -1 0 void Insert(HashTable *HT, int Key, int Value) { Link *FoundLink; } FoundLink = Find_Key(HT, Key); if (FoundLink != NULL) FoundLink->Value = Value; else { if (HT->FreeLinks == NULL) HT->FreeLinks = Make_New_Links(); FoundLink = HT->FreeLinks; HT->FreeLinks = HT->FreeLinks->Next; FoundLink->Key = Key; FoundLink->Value = Value; FoundLink->Next = HT->Buckets[Hash(HT, Key)]; HT->Buckets[Hash(HT, Key)] = FoundLink; HT->Size += 1; if (HT->Size > HT->NumBuckets * RESIZERATIO) Resize_Hash_Table(HT); } Part A: Complete the C function Find_Key that searches the hash table for an entry corresponding to a specified key. It should return a pointer to the matching Link entry if the key is found or return NULL if the key is not found in the hash table. Link *Find_Key(HashTable *HT, int Key) { Link *ThisLink; int Index; int Hash(HashTable *HT, int Key); } return ThisLink; Part B: The following questions are related to the hash table implementation listed above. When Insert is called more than once with the same key, what occurs? Where is a new entry link placed on the bucket list? What is the range of possible values for average bucket list length? Describe the type of Buckets in HashTable (in 10 words or less). What is the size (in bytes) of HashTable? What is the size (in bytes) of the initial Buckets array? 5 ECE 2035 Study Guide: Part 5 Version 1.0 Problem HM-4 (4 parts) Heap Manager Implementation Consider the following fragment from a HeapManager implementation. Note that $2 is the freed object pointer and $3 is the free list. Label1: beq add lw lw slt bne sw $3, $6, $4, $5, $5, $5, $3, $0, Label1 $3, $0 -4($2) -4($3) $5, $4 $0, Label3 0($2) # # # # # # # I01 I02 I03 I04 I05 I06 I07 Part A: Using abstract terms, define what is being compared in I05(slt). Don't simply transliterate the MIPS instructions (e.g., don't say “reg 2 is added to reg 3”). Part B: Describe the two cases where I07(sw) is executed. Be specific. 1. 2. Part C: Consider the three pointers below. Assume their initialized value is the address of an appropriate address in memory. For each statement below, list the resultant value. If the value is undecidable, list “error”. char void void Heap + 1 *Heap = 5000; *Object = 6000; **Free = 7000; Object + 1 mem[5000] = 5500 mem[6000] = 6500 mem[7000] = 7500 Free + 1 Part D: Concisely define the following terms? void pointer: null pointer: 6 ((int *) *Free) + 1 ECE 2035 Study Guide: Part 5 Version 1.0 Problem HM-5 (3 parts) Heap Management Below is a snapshot of heap storage. The heap has been allocated contiguously beginning at address 6000, with no gaps between objects using a heap manager implemented in C. addr value addr value addr value addr value addr value addr value 6000 16 6032 12 6064 0 6096 16 6128 12 6160 0 6004 33 6036 28 6068 4 6100 0 6132 6036 6164 0 6008 6072 6040 24 6072 6100 6104 16 6136 2525 6168 0 6012 16 6044 60 6076 8 6108 5 6140 0 6172 0 6016 6080 6048 16 6080 6080 6112 6004 6144 20 6176 0 6020 8 6052 6 6084 6004 6116 8 6148 6036 6180 0 6024 6120 6056 6100 6088 4 6120 6100 6152 8 6184 0 6028 52 6060 0 6092 6024 6124 24 6156 26 6188 0 Below is a portion of the heap manager: L-1 L-2 L-3 L-4 L-5 L-6 L-7 L-8 L-9 L-10 L-11 L-12 L-13 L-14 L-15 L-16 L-17 char char void Heap[HEAPSIZE]; *HeapPtr = Heap; **FreePtr = NULL; void Free(void *ObjectPtr) { int ObjectSize; void **ThisPtr = FreePtr; void **LastPtr = (void **) &FreePtr; ObjectSize = *(-1 + (int *) ObjectPtr); while (ThisPtr != NULL && ObjectSize > *((int *) ThisPtr - 1)) { LastPtr = ThisPtr; ThisPtr = (void **) *ThisPtr; } * (void **) ObjectPtr = ThisPtr; *LastPtr = ObjectPtr; } Assume HeapPtr = 6168 and FreePtr = 6092 and FreePtr is located at address 5064. Part A: Circle all object size words in the snapshot above. Part B: List the base addresses of the heap objects that are on the free list in the order they appear on the free list. Part C: Suppose the following function call is made: Free(p), where p=6132. Given the snapshot of heap above, what is the value of the following at each of the indicated locations in the code? ThisPtr LastPtr at L-9 at L-17 7 *ObjectPtr ECE 2035 Study Guide: Part 5 Version 1.0 Problem HM-6 (5 parts) Garbage Collection Below is a snapshot of heap storage. Values that are pointers are denoted with a “$”. The heap pointer is $6168. The heap has been allocated contiguously beginning at $6000, with no gaps between objects. addr 6000 6004 6008 6012 6016 6020 6024 6028 value 16 33 $6072 16 $6080 8 25 52 addr 6032 6036 6040 6044 6048 6052 6056 6060 value 12 28 24 60 16 6 $6100 0 addr 6064 6068 6072 6076 6080 6084 6088 6092 value 0 4 $6100 8 $6080 $6004 4 50 addr 6096 6100 6104 6108 6112 6116 6120 6124 value 16 $6036 16 5 $6004 8 32 24 addr 6128 6132 6136 6140 6144 6148 6152 6156 value 12 8000 2525 0 20 $6036 8 26 addr 6160 6164 6168 6172 6176 6180 6184 6188 value 0 0 0 0 0 0 0 0 Part A: Suppose the stack holds a local variable whose value is the memory address $6052 and register $3 holds the address $6148. No other registers or static variables currently hold heap memory addresses. List the addresses of all objects in the heap that are not garbage. Addresses of Non-Garbage Objects: Part B: Create a free list by scanning the memory for garbage, starting at address $6000 and pushing each reclaimed object on the front of the free list. List the addresses of the objects (in order) on the free list at the end of the scan. Free List: Part C: Based on the free list created in part B, if an object of size 13 bytes is allocated, what address will be returned using a best-fit allocation strategy? Address: Part D: Based on the free list created in part B, if an object of size 9 bytes is allocated, what address will be returned using a best-fit allocation strategy? Address: Part E: If the local variable whose value is the address $6052 is popped from the stack, which addresses will be reclaimed by each of the following strategies? If none, write “none.” (You do not have to list addresses already on the free list from part B.) Reference Counting: Mark and Sweep: Old-New Space (copying): 8 ECE 2035 Study Guide: Part 5 Version 1.0 Problem HM-7 (5 parts) Garbage Collection Below is a snapshot of heap storage. Values that are pointers are denoted with a “$”. The heap pointer is $6160. The heap has been allocated contiguously beginning at $6000, with no gaps between objects. addr value addr value addr value addr value addr value addr value 6000 8 6032 12 6064 8 6096 4 6128 6024 6160 0 6004 6132 6036 28 6068 $6004 6100 $6036 6132 12 6164 0 6008 $6092 6040 $6108 6072 20 6104 16 6136 4 6168 0 6012 16 6044 6024 6076 8 6108 6076 6140 16 6172 0 6016 6180 6048 12 6080 6004 6112 152 6144 4 6176 0 6020 8 6052 6 6084 $6016 6116 8 6148 8 6180 0 6024 25 6056 6080 6088 12 6120 $6004 6152 6100 6184 0 6028 $6128 6060 8 6092 6040 6124 4 6156 $6036 6188 0 Part A Suppose the stack holds a local variable whose value is the memory address $6068 and register $4 holds the address $6152. No other registers or static variables currently hold heap memory addresses. List the addresses of all objects in the heap that are not garbage. Addresses of Non-Garbage Objects: Part B Create a sorted (by size) free list by scanning the memory for garbage, starting at address $6000 and inserting each garbage object into the free list in increasing size order. List the base address of each object (not the address of the header) on the free list (in order) at the end of the scan. Free List: Part C Based on the free list created in part B, if an object of size 5 bytes is allocated, what address will be returned using a best-fit allocation strategy? How many bytes of slack (if any) will result? What is the value of the heap pointer after the object is allocated? Address: Slack: bytes Heap Pointer: Part D Based on the free list created in part B, if an object of size 19 bytes is allocated, what address will be returned using a best-fit allocation strategy? How many bytes of slack (if any) will result? What is the value of the heap pointer after the object is allocated? Address: Slack: bytes Heap Pointer: Part E If the local variable whose value is the address $6068 is popped from the stack, which addresses will be reclaimed by each of the following strategies? If none, write “none.” (You do not have to list addresses already on the free list from part B.) Reference Counting: Mark and Sweep: Old-New Space (copying): 9 ECE 2035 Study Guide: Part 5 Version 1.0 Problem HM-8 (3 parts) Associative Sets and Search Part A Consider a hash table that uses 21 buckets, each containing an unsorted LIFO list of items. Inserted entries are appended to the end of the bucket list. Suppose the hash table contains 210 entries total and the entries are evenly distributed across the hash table buckets. Assume that computing the hash function takes an average of two operations and comparing two strings takes an average of twenty operations. Ignore effects of spatial and temporal reference locality. Suppose that 80% of keys looked up are found in the hash table and 20% are not found. How many of these operations would be required for the average lookup in the hash table described above? (show work) operations Part B Suppose the hash table automatically resizes (with the same 210 entries and found key probabilities) so that the average access time becomes 82 operations. How many hash table buckets are being used? (show work) new number of buckets: Part C Implement a function findpos that returns the earliest position of the character x in a character string. If x does not appear in the character string, return 1. For example: char sample[] = “Happy Birthday”; findpos('p', sample); /* returns 2 */ findpos('m', sample); /* returns 1 */ findpos('H', sample); /* returns 0 */ int findpos(char x, char *str) { } 10 ECE 2035 Study Guide: Part 5 Version 1.0 Problem HM-9 (3 parts) Associative Sets Part A Consider a hash table that uses 15 buckets, each containing an unsorted LIFO list of items. Suppose the hash table contains 600 entries total and the entries are evenly distributed across the hash table buckets. Assume that computing the hash function takes an average of three operations and comparing two keys takes an average of ten operations. Ignore effects of spatial and temporal reference locality. Suppose that 80% of keys looked up are found in the hash table and 20% are not found. How many of these operations would be required for the average lookup in the hash table described above? (show work) operations Part B For the hash table in Part A, recompute the cost assuming that the bucket lists are sorted in order of ascending keys. How many of these operations would be required for the average lookup in the hash table described above? (show work) operations Part C Explain the difference between malloc() and calloc(). malloc calloc Part D Briefly describe the unfortunate outcome if a pointer to a stack-allocated object is passed to the function free(). 11