memory management algorithms and implementation in cc++

391 345 0
memory management algorithms and implementation in cc++

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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 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 Table of Contents vi StaticorDynamic? 150 HeapAllocation 151 SystemCallInterface 151 TheHeap 156 Manual Memor y 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 Memor y 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 Por ting 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 Table of Contents vii 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 Table of Contents viii 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 Table of Contents ix [...]... for improvements and a look at certain hybrid approaches Chapter 5 – Automatic Memory Management In Chapter 5, a number of automatic memory management algorithms are examined The algorithms are presented in theory, implemented in C++, and then critiqued in terms of their strengths and weaknesses A significant amount of effort is invested in making this discussion easy to follow and keeping the reader... discovery and insight was playful experimentation Dick was the kind of guy who followed his own advice In his biography, Surely You’re Joking, Mr Feynman, Dick recounts how spinning plates in a dining hall at Cornell led to historic work in quantum mechanics By testing a variety of new ideas and comparing the results to your predictions, you force yourself to xxiv Introduction gain a better understanding... programming languages This chapter also serves as a launch pad for the next two chapters by presenting an overview of memory management at the application level Chapter 4 – Manual Memory Management In Chapter 4, a number of manual memory management algorithms are presented in explicit detail The algorithms are presented in theory, implemented in C++, and then critiqued in terms of their strengths and weaknesses... assembly language, including its special “privileged” instructions, you will also have a fairly solid understanding of how the machine functions and what its limitations are In addition, given that compilers generate assembly code, or at least spit it out in a listing file, you will also be privy to the inner workings of development tools In short, knowing assembly language is like learning Latin It may not... italics are used to define or emphasize a term The Courier font is used to denote code, memory addresses, input/output, and filenames For more information, see the section titled “Typographical Conventions” in the Introduction Mechanism Versus Policy Accessing and manipulating memory involves a lot of accounting work Measures have to be taken to ensure that memory being accessed is valid and that it corresponds... and dissected This will necessarily involve taking a good, hard look at the internals of production operating systems like Linux and Windows In general, hardware always provides features that are ahead of the software that uses it For example, Intel’s Pentium provides four distinct layers of memory protection Yet, I could not find a single xix Introduction operating system that took advantage of all... language for implementing memory management algorithms because it offers a mixture of tools With C++, you can manipulate memory at a very low, bit-wise level and invoke inline assembly code when needed You can also create high-level constructs using the object-oriented language features in C++ Encapsulation, in particular, is a compiler-enforced language feature that is crucial for maintaining large software... 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 various 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... performance and accuracy of his group’s computations were often more a function of his ability to motivate people He would sometimes assemble people into teams and have them compete against each other Not only was this a good idea from the standpoint of making things more interesting, but it was also an effective technique for catching discrepancies xv Introduction In 1958, the first integrated circuit was invented... When it comes to learning something complicated, like memory management, I believe that the most effective way is to examine a working subsystem On the other hand, it is easy to become lost in the details of a production memory manager Contemporary memory managers, like the one in Linux, are responsible for keeping track of literally hundreds of run-time quantities Merely tracking the subsystem’s execution . 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. the standpoint of making things more interesting, but it was also an effective technique for catching discrepancies. Introduction xv In 1958, the first integrated circuit was invented. The inventor. 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

Ngày đăng: 06/07/2014, 15:19

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan