Memory Management Algorithms and Implementation in C/C++
Total Page:16
File Type:pdf, Size:1020Kb
Memory Management Algorithms and Implementation in C/C++ by Bill Blunden Wordware Publishing, Inc. Library of Congress Cataloging-in-Publication Data Blunden, Bill, 1969- Memory management: algorithms and implementation in C/C++ / by Bill Blunden. p. cm. Includes bibliographical references and index. ISBN 1-55622-347-1 1. Memory management (Computer science) 2. Computer algorithms. 3. C (Computer program language) 4. C++ (Computer program language) I. Title. QA76.9.M45 .B558 2002 005.4'35--dc21 2002012447 CIP © 2003, Wordware Publishing, Inc. All Rights Reserved 2320 Los Rios Boulevard Plano, Texas 75074 No part of this book may be reproduced in any form or by any means without permission in writing from Wordware Publishing, Inc. Printed in the United States of America ISBN 1-55622-347-1 10987654321 0208 Product names mentioned are used for identification purposes only and may be trademarks of their respective companies. All inquiries for volume purchases of this book should be addressed to Wordware Publishing, Inc., at the above address. Telephone inquiries may be made by calling: (972) 423-0090 This book is dedicated to Rob, Julie, and Theo. And also to David M. Lee “I came to learn physics, and I got Jimmy Stewart” iii Table of Contents Acknowledgments......................xi Introduction.........................xiii Chapter 1 Memory Management Mechanisms. 1 MechanismVersusPolicy..................1 MemoryHierarchy......................3 AddressLinesandBuses...................9 Intel Pentium Architecture . 11 RealModeOperation...................14 Protected Mode Operation. 18 Protected Mode Segmentation . 19 ProtectedModePaging................26 PagingasProtection..................31 Addresses: Logical, Linear, and Physical . 33 PageFramesandPages................34 Case Study: Switching to Protected Mode . 35 ClosingThoughts......................42 References..........................43 Chapter 2 Memory Management Policies. 45 CaseStudy:MS-DOS....................46 DOS Segmentation and Paging . 46 DOSMemoryMap....................47 MemoryUsage......................49 Example: A Simple Video Driver . 50 Example: Usurping DOS . 52 Jumping the 640KB Hurdle . 56 CaseStudy:MMURTL...................59 Background and Design Goals . 60 MMURTL and Segmentation . 61 PagingVariations.....................63 MMURTLandPaging..................64 v Table of Contents MemoryAllocation....................66 CaseStudy:Linux......................67 HistoryandMINIX....................67 Design Goals and Features. 68 Linux and Segmentation . 69 LinuxandPaging.....................72 Three-LevelPaging..................72 PageFaultHandling..................76 MemoryAllocation....................76 MemoryUsage......................81 Example:SiegeWarfare.................82 Example: Siege Warfare, More Treachery . 87 CaseStudy:Windows....................92 HistoricalForces.....................92 MemoryMapOverview.................96 Windows and Segmentation . 99 Special Weapons and Tactics . 99 Crashing Windows with a Keystroke . 102 Reverse Engineering the GDT . 102 WindowsandPaging..................105 Linear Address Space Taxonomy . 105 Musical Chairs for Pages. 106 MemoryProtection.................108 DemandPaging....................109 MemoryAllocation...................110 MemoryUsage.....................114 TurningOffPaging...................117 Example: Things That Go Thunk in the Night . 118 ClosingThoughts.....................122 References.........................123 BooksandArticles...................123 WebSites........................125 Chapter 3 High-Level Services. 127 Viewfrom10,000Feet...................127 Compiler-Based Allocation . 129 DataSection.......................132 CodeSection......................134 Stack...........................136 ActivationRecords..................138 Scope.........................144 vi Table of Contents StaticorDynamic?..................150 HeapAllocation......................151 SystemCallInterface..................151 TheHeap........................156 Manual Memory Management. 157 Example: C Standard Library Calls . 158 Automatic Memory Management . 160 Example: The BDW Conservative Garbage Collector ............................161 Manual Versus Automatic?. 164 The Evolution of Languages. 168 CaseStudy:COBOL..................171 CaseStudy:FORTRAN.................177 CaseStudy:Pascal...................181 CaseStudy:C......................184 CaseStudy:Java.....................192 LanguageFeatures..................192 Virtual Machine Architecture . 194 Java Memory Management . 196 Memory Management: The Three-layer Cake . 202 References.........................204 Chapter 4 Manual Memory Management . 207 Replacements for malloc() and free() .......207 System Call Interface and Porting Issues . 208 KeepItSimple...Stupid!.................211 MeasuringPerformance..................212 The Ultimate Measure: Time . 212 ANSI and Native Time Routines . 213 The Data Distribution: Creating Random Variates . 215 TestingMethodology..................219 Indexing: The General Approach . 224 malloc() Version 1: Bitmapped Allocation. 224 Theory..........................224 Implementation.....................226 tree.cpp........................227 bitmap.cpp......................232 memmgr.cpp.....................236 mallocV1.cpp.....................239 perform.cpp......................241 driver.cpp.......................241 vii Table of Contents Tests...........................242 Trade-Offs........................247 malloc() Version 2: Sequential Fit . 248 Theory..........................249 Implementation.....................251 memmgr.cpp.....................251 mallocV2.cpp.....................260 driver.cpp.......................261 Tests...........................262 Trade-Offs........................264 malloc() Version 3: Segregated Lists . 265 Theory..........................265 Implementation.....................266 memmgr.cpp.....................267 mallocV3.cpp.....................274 Tests...........................275 Trade-Offs........................279 Performance Comparison . 279 Chapter 5 Automatic Memory Management . 281 Garbage Collection Taxonomy . 281 malloc() Version 4: Reference Counting . 283 Theory..........................283 Implementation.....................284 driver.cpp.......................285 mallocV4.cpp.....................287 perform.cpp......................288 memmgr.cpp.....................289 Tests...........................299 Trade-Offs........................302 malloc() Version 5: Mark-Sweep . 304 Theory..........................304 Implementation.....................307 driver.cpp.......................307 mallocV5.cpp.....................309 perform.cpp......................311 memmgr.cpp.....................312 Tests...........................325 Trade-Offs........................330 Performance Comparison . 332 PotentialAdditions.....................332 viii Table of Contents Object Format Assumptions . 333 VariableHeapSize...................335 IndirectAddressing...................335 Real-TimeBehavior...................337 Life Span Characteristics . 338 Multithreaded Support . 339 Chapter 6 Miscellaneous Topics . 343 Suballocators........................343 Monolithic Versus Microkernel Architectures . 348 ClosingThoughts.....................351 Index............................355 ix.