Everything about Internet Technology! Chose your section ● Auctions ● Network Marketing ● Business ● Online Promotion ● Computers ● Search Engines ● Domain Names ● Site Promotion ● Downloads ● Software ● eBay ● Technology ● Ebooks ● Video Conferencing ● Ecommerce ● VOIP ● Email Marketing ● Web Design ● Internet Marketing ● Web Development ● HTML ● Web Hosting ● JavaScript ● WIFI ● MP3 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 Acknowledgments Publishing a book is an extended process that involves a number of people. Writing the final manuscript is just a small part of the big picture. This section is dedicated to all the people who directly, and indirectly, lent me their help. First and foremost, I would like to thank Jim Hill of Wordware Publishing for giving me the opportunity to write a book and believ- ing in me. I would also like to extend thanks to Wes Beckwith and Beth Kohler. Wes, in addition to offering constant encouragement, does a great job of putting up with my e-mails and handling the vari- ous packages that I send. Beth Kohler, who performed the incredible task of reading my first book for Wordware in a matter of days, has also been invaluable. I first spoke with Barry Brey back in the mid-1990s when I became interested in protected mode programming. He has always taken the time to answer my questions and offer his insight. Barry wrote the first book on the Intel chip set back in 1984. Since then, he has written well over 20 books. His current textbook on Intel’s IA32 processors is in its sixth edition. This is why I knew I had to ask Barry to be the technical editor for this book. Thanks, Barry. “Look, our middleware even runs on that little Windows NT piece of crap.” — George Matkovitz “Hey, who was the %&^$ son of a &*$# who wrote this optimized load of . oh, it was me.” — Mike Adler Mike Adler and George Matkovitz are two old fogeys who worked at Control Data back when Seymour Cray kicked the tar out of IBM. George helped to implement the world’s first message-passing operating system at Control Data. Mike also worked on a number of groundbreaking system software projects. I met these two codgers while performing R&D for an ERP vendor in the Midwest. I hadn’t noticed how much these engineers
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages392 Page
-
File Size-