The Type Of Allocator You Will Implement Is A Pool Allocator, Using Multiple Memory Pools. Each Memory Pool Consists Of Allocations Of A ﬁxed Size.
INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS
In This Assignment, You Will Implement A Dynamic Memory Allocator Suitable For Replacing Malloc() For Heap Memory In A Unix Process. You Will Learn About Dynamic Memory Management, Pointer Manipulation, And Pointer Casting.
The Type Of Allocator You Will Implement Is A Pool Allocator, Using Multiple Memory Pools. Each Memory Pool Consists Of Allocations Of A ﬁxed Size. Allocations Are Served Out Of The Smallest Pool That Supports The User’s Request, And Allocations Too Large For Any Memory Pool Are Requested From The OS Directly. The Memory Pools In This Project Will Contain Allocations Ranging From 32 Bytes To 4096 Bytes.
Your Allocator Is Designed In Such A Way That You Will Be Able To Use It To Run Standard Unix Binaries! Because You Will Not Be Implementing Support For Concurrency, Some Programs May Fail Through No Fault Of Your Own; However, Many Programs Will Operate Correctly. You May ﬁnd It Interesting And Enjoyable To Use Your Allocator Implementation To Learn About The Memory Accesses Of Standard Applications.
1 Getting Started
You Should Have Received A GitHub Classroom Invitation For This Assignment. Follow It, Then Check Out Your Repository.
Chapter 9, Section 9.9 Of Computer Systems: A Programmer’s Perspective Describes A Dynamic Allocator Im- Plementation In Some Detail. You May Want To Read This Section Before Starting Your Project, And Will Certainly Want To Read It Carefully While Implementing Your Project.
Chapter 8, Section 8.7 Of The CProgramming Language Also Describes A Dynamic Allocator Of Similar Structure To That Described In CS:APP. This Allocator Is Described In Somewhat Less Detail, But Uses Techniques (Such As Declaring Structures To Hold Metadata, Rather Than Using Macros And Pointer Math) That Are To Be Preferred Over The Approach In CS:APP, So You Should Read And Try To Understand It, As Well. Again, You May Want To Read This Section Before You Begin, And Will Certainly Want To Read It Carefully During Implementation.
You Will Absolutely Need To Read Man 3 Malloc. It Is Not Long. We Will Not Answer Questions That Are Clearly Answered In Man 3 Malloc.
Some Parts Of This Handout May Not Make Much Sense Until You Have Read The Above Readings.
2 Pool Allocators
Pool Allocators Are Designed To Allow Applications To Eﬃciently Allocate, Free, And Reallocate Chunks Of Memory In Sizes That They Use Frequently. This Is An Eﬀective Strategy For Many Applications Because It Is Common For An Application To Allocate Many Objects That Are The Same Size — For Example, Nodes In A Linked List Or Tree, Or Buﬀers For Communication. Placing Such Frequently-Used Objects In A Pool Where They Can Be Rapidly Reallocated Improves Application Performance.
Allocation Pools Are Maintained By Requesting Large-Ish Blocks Of Memory From The Operating System, Then Breaking Those Blocks Up Into The Allocation Size Stored In A Pool. These “Free” Blocks Are Then Used To Serve Allocations Requested By The User. When Blocks Are Freed By The User, They Are Simply Placed Back In The Pool, Preventing Future Allocations From Having To Request More Memory From The Operating System. When An Allo- Cation Is Requested From A Pool That Does Not Contain Any Free Blocks, The Allocator Will Request Another Large Chunk From The Operating System To Be Broken Up Again.
You Will Be Implementing A Multi-Pool Allocator, Which Will Maintain Pools Containing Allocation Blocks With Sizes Of Powers Of Two Between 25 (32) And 212 (4096). These Blocks Will Be Used To Serve Allocations Of 24 (32 8) To 4088 (4096 8) Bytes, Using The Remaining 8 Bytes Between These Sizes And The Power Of Two To Store Metadata. Because The Free() Function Does Not Accept A Size Argument, This Metadata Will Be Used To Store The Size Of The Block For Use By Free(). Each Allocation Request Will Be Served From The Smallest Block That Will Store The Allocation; For Example, A 1 Byte Allocation Would Come Out Of A 32 Byte Block, While A 1000 Byte Allocation Would Come Out Of A 1024 Byte Block.
You Must Implement The Following Functions In The ﬁle Src/Mm.C:
Void *Malloc(Size_t Size)
Void *Calloc(Size_t Nmemb, Size_t Size)
Void *Realloc(Void *Ptr, Size_t Size)
Void Free(Void *Ptr)
Your Implementation Must Satisfy The Standard Deﬁnition Of These Functions (See Man 3 Malloc), With The Ex- Ception That It Need Not Allow For Concurrent Access From Multiple Threads, As Well As The Additional Implementation Requirements Listed Here.
Your Implementation Must Be A Multi-Pool Allocator As Described In This Document. No Other Type Of Allocator Implementation Will Receive Credit. It Must Use Pool Allocation As Described Here To Implement All Allocations Between 1 And 4088 Bytes, And A Simple Bulk Allocator (Described Below) For Allocations Larger Than 4088 Bytes. (The Discrepancy Between 4088 Bytes And The Even-Power-Of-Two 4096 Bytes, Mentioned In Section 2, Will Be Further Explained Later.)
All Memory Returned By Your Allocation Functions (Malloc(), Calloc(), And Realloc()) Must Be 8 Byte Aligned. Failure To Maintain This Requirement Will Not Result In Functional Failures Of Your Allocator On The X86-64 Plat- Form (Programs Using Your Allocator Would Continue To Work Correctly), But May Result In Degraded Perfor- Mance For Programs Using Your Allocator. On Other Platforms, Failure To Maintain This Requirement Could Result In Program Crashes.
Your Implementation Must Be Able To Resize An Allocation Using Realloc. Under Certain Circumstances It Should Do This Without Performing Any New Allocation, And Under Other Circumstances It Should Do This By Allocating A New Block And Copying The User’s Data From The Old Block To The New. If It Is Possible To Perform The Reallocation Without Allocating A New Block And Moving Data, Your Implementation Must Do So. This Is Always Possible For Reallocations Which Reduce The Size Of An Allocation, As Realloc() Can Return The Existing Block, And The User Will Simply Use Less Of It. For Allocations Which Increase The Size Of The Allocation, It Is Possible Only If The Block Originally Selected To Serve The User’s Allocation Is Also Large Enough To Serve The New Allocation. For Example, If The User Calls Malloc(32) To Request A 32 Byte Allocation, Your Allocator Will Select A 64 Byte Block Containing 56 Usable Bytes Of Memory. If The User Later Wishes To Realloc() This Block To 48 Bytes, This Does Not Require Any Allocation Or Copying, As While 48 Bytes Is More Than The Original 32 Bytes Requested, It Is Less Than The 56 Bytes Available In This Block.
If Realloc() Cannot Resize A Block Without Performing Allocation, It Must Use Your Allocator To Acquire A New Block Of The Appropriate Size (Which Is Necessarily Larger Than The Original Block), Copy The User’s Data From The Old Block To The New Block, And Then Free The Old Block.
When Changing The Size Of An Allocation Using Realloc(), Your Implementation Must Attempt To Resize The Allocation In Place, If Possible. For Reallocations That Reduce The Size Of The Allocation, This Should Always Suc- Ceed. For Reallocations That Increase The Size Of The Allocation, It Should Succeed If There Is Suﬃcient Space In The Allocated Block After The Allocation Being Resized. Only If This Is Not The Case Should A New Allocation Be Created And The Memory Of The Original Allocation Be Moved To The New Location Before Freeing The Original Allocation.
Requests For An Allocation Of Size 0 To Any Of The Allocator Functions May Either Use The Pool Allocator To Create A Minimum-Size Allocation Or Return NULL, As You Prefer. You Will Probably ﬁnd That Simply Returning
NULL Is Easier, But If For Some Reason Your Algorithm Beneﬁts From The Possibility Of Returning A Minimum-Size Allocation, You May Do So.
Your Allocator Must Be Capable Of Returning Freed Memory To The Heap And Reusing It To Serve Future Allo- Cations. In Particular, If A Program Repeatedly Allocates And Frees Many Small Objects With A Constant Upper Bound On The Number Of Objects And Total Space Required, Your Allocator Should Eventually Settle On A Heap Size That Requires No Additional Requests To The Operating System For More Memory.
You Must Test Your Own Project. The Tests Provided In Autograder Will Be Incomplete, And You Should Not Rely Upon Them To Give More Than A Rough Estimate Of Your ﬁnal Score. There Is No Requirement To Submit Your Tests For This Project.
3.1 Metadata And Global Symbols
As Described In CS:APP In Section 9.9, A Heap Allocator Must Not Use Any Global State Of Non-Constant Size That Cannot Be Allocated From The Heap Itself. Therefore, You Should Declare Only Primitive Global (Or Static) Variables For Your Allocator, And You Must Use The Heap For Any Other Data Required. A Clever Implementation Will Add A Few Constant Bytes To Each Allocation And Store The Rest Of Its Variably-Sized Metadata In Free Blocks, Requiring Only A Constant Allocation At Initialization Time For Its Remaining Storage.
3.1.1 Static Variables
Any Global Variables Or Functions Declared By Your Implementation In Mm.C Should Include The Keyword Static. This Will Ensure That They Are Not Visible To Any Other Translation Units, And That Your Allocator’s Function Cannot Be Disrupted By Code That May Link To It. (See Section 4.6 Of K&R For More Discussion Of Static Variables, And The Compiler.Pdf Slide Deck For A Deﬁnition Of Translation Units.)
3.1.2 Block Headers
The Metadata Stored For Each Block Of Memory Managed By Your Allocator (Allocated Or Free) Should Be A 64 Bit Header Word Preceding The Allocation (Or Free Block), Containing The Size Of The Allocation And Some ﬂag Bits. Note That The Minimum Possible Block Size For The Pool-Allocated Blocks Is 25, Or 32 Bytes, And That All Pool- Allocated Blocks Have Sizes Which Are Powers Of Two. Recalling The Format Of Integers In Memory, This Means That The Lowest ﬁve Bits Of The Integer Size Of A Block, Whether It Is Free Or Allocated, Will Always Be Zero. Those Bits Can Therefore Be Used To Store ﬂags, And Then Masked Out When Retrieving The Size Of A Block. CS:APP Explains This In Some Detail In Section 9.9.6 (In Particular, See Figure 9.35).
You Should Use The Lowest-Order Bit (The “Ones Bit”) To Indicate Whether A Block Is Free Or Allocated; A One Bit Will Indicate An Allocated Block, And A Zero Bit Will Indicate A Free Block. While This Is Not Entirely Necessary To Complete This Project, It Makes The Implementation Of A Heap Validator (Suggested In Section 5) Much More Eﬀective. Remember To Account For This When Allocating And Freeing Bulk-Allocated Blocks, If Necessary. You Will Probably ﬁnd That Bitwise Operations Are The Best Way To Maintain The Allocated Bit And Other Bits In The Size ﬁeld, Rather Than Using + 1 And - 1 (Which Has For Some Reason Proven Both A Popular Technique And A Popular Source Of Bugs In The Past).
The Pointer Returned By The Allocation Functions, And The Pointer Given To Free() Or Realloc(), Is The ﬁrst Byte Of Memory After The Header Word. Your Implementation Will Have To Use Pointer Math To ﬁnd The Header Word For A Block That Is Passed To Free() Or Realloc(), And Perhaps To Calculate The Address To Be Returned From Malloc() Et Al.
This Metadata Is The Reason That Your Allocator’s Eﬀective Allocation Sizes Are 8 Bytes Smaller Than An Even Power Of Two. The Block From Which An Allocation Is Drawn Is A Power Of Two In Size, But There Are 8 Bytes Of Header Used From That Block For Storing Metadata.
3.2 The Sbrk System Call
The Operating System Provides Two System Calls To Manipulate The Program Break Between The Application’s Heap And The Unmapped Memory Region Above The Heap: Brk() And Sbrk(). The Brk() System Call Accepts An Absolute Address And Immediately Sets The Program Break To That Point, While Sbrk() Accpets A Relative Size And Adjusts The Current Program Break By That Amount. It Is Diﬃcult To Use Brk() Correctly And Safely, So We Will Use
Sbrk() For This Assignment.
Passing A Positive Value To Sbrk() Causes The System To Move The Program Break The Requested Number Of Bytes Upward In Memory, Toward Larger Addresses. It Will Then Return The Old Value Of The Program Break, Which Is A Memory Address. The Newly Allocated Memory Therefore Begins At The Address Returnd By Sbrk() And Continues For The Number Of Bytes Requested By The Application.
. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C
DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma
. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an
Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t
. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual
Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th
. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:
Introduction - the SeaPort Project series
For this set of projects for the course, we wish to simulate some of the aspects of a number of
. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:
Introduction - the SeaPort Project series
For this set of projects for the course, we wish to simulate some of the aspects of a number of