Windows Memory Management, Memory- Mapped Files, and DLLs

  • View
    217

  • Download
    0

Embed Size (px)

Text of Windows Memory Management, Memory- Mapped Files, and DLLs

  • Slide 1
  • Windows Memory Management, Memory- Mapped Files, and DLLs
  • Slide 2
  • 2 OBJECTIVES (1 of 2) Upon completion of this Chapter you will be able to: Describe the Windows memory management architecture and the role of heaps and memory-mapped files Use multiple independent heaps in applications requiring dynamic memory management Use Structured Exception Handling to respond to memory allocation errors Use memory-mapped files
  • Slide 3
  • 3 OBJECTIVES (2 of 2) Determine when to use the independent heaps and when to use memory-mapped files and to describe the advantages and disadvantages of each Describe Windows dynamic link libraries (DLLs) Describe the difference between static, implicit, and explicit linking Describe the advantages and disadvantages of each Use DLLs to load different implementations of the same function
  • Slide 4
  • 4 OVERVIEW (1 of 2) 32-bit operating system, so pointers are 4-byte objects Win64 provides 64-bit pointers Processes have a private 4GB virtual address space Half (2GB) is available to a process Remainder allocated to shared data and code Win64 enlarges VA space; required for many applications Programs can create independent memory heaps Processes can map files to memory Processes can share memory through a mapped file Fast and convenient for some file processing
  • Slide 5
  • 5 OVERVIEW (2 of 2) Dynamic Link Libraries with Monolithic Programs Gather all the source code, including commonly used Chapters such as utility functions Put all the source code in a single project Build, test, debug, and use the program Inefficiency Recompile same code in all projects All executables include the same object code Waste of disc space and physical memory at run time Maintenance complexity as shared code changes
  • Slide 6
  • Part I Memory Management and Heaps
  • Slide 7
  • 7 Windows Program Heap API: HeapCreate, HeapDestroy, HeapAlloc, HeapFree MMF API: CreateFileMapping, CreateViewOfFile Virtual Memory API Windows Kernel with Virtual Memory Manager Physical Memory Disc & File System C library: malloc, free Memory Management Architecture
  • Slide 8
  • 8 HEAPS (1 of 2) Pools of memory within the process virtual address space Every process has a default process heap A process may have more than one heap. Benefits of separate heaps include: Fairness (between threads and between uses) Allocation efficiency (fixed size blocks in each heap) Deallocation efficiency (you can deallocate a complete data structure with one call) Locality of reference efficiency
  • Slide 9
  • 9 HEAPS (2 of 2) Every process has a process heap Every heap has a handle The programmer can use the process heap or create new ones HANDLE GetProcessHeap (VOID) Return: The handle for the process heap; NULL on failure
  • Slide 10
  • 10 Node Record Not allocated Process Heap RecHeap NodeHeap Virtual Address Space Program ProcHeap = GetProcessHeap ( ); pRoot = HeapAlloc (ProcHeap); RecHeap = HeapCreate ( ); NodeHeap = HeapCreate ( ); while ( ) { pRec = HeapAlloc (RecHeap); pNode = HeapAlloc (NodeHeap); } HeapFree (RecHeap, 0, pRec); HeapFree (NodeHeap, 0, pNode); HeapDestroy (RecHeap); HeapDestroy (NodeHeap); MEMORY MGT. IN MULTIPLE HEAPS
  • Slide 11
  • 11 HEAP MANAGEMENT (1 of 2) HANDLE HeapCreate (DWORD flOptions, DWORD dwInitialSize, DWORD dwMaximumSize) Return: A heap handle or NULL on failure dwMaximumSize How large the heap can become 0 growable heap; no fixed limit non-zero non-growable heap The entire block is allocated from the virtual address space But only the initial size is committed in the paging file
  • Slide 12
  • 12 HEAP MANAGEMENT (2 of 2) flOptions is a combination of two flags: HEAP_GENERATE_EXCEPTIONS HEAP_NO_SERIALIZE By generating exceptions, you can avoid explicit tests after each heap management call
  • Slide 13
  • 13 HEAPS BOOL HeapDestroy (HANDLE hHeap) hHeap a heap generated using HeapCreate Do not destroy the process heap (obtained using GetProcessHeap ) Benefits of HeapDestroy : No data structure traversal code No need to deallocate each individual data structure element, which can be time-consuming
  • Slide 14
  • 14 MANAGING HEAP MEMORY (1 of 4) LPVOID HeapAlloc (HANDLE hHeap, DWORD dwFlags, DWORD dwBytes) Return: A pointer to the allocated memory block (of size dwBytes ) or NULL on failure (unless exception generation is specified) hHeap Handle from GetProcessHeap or HeapCreate dwFlags A combination of: HEAP_GENERATE_EXCEPTIONS HEAP_NO_SERIALIZE HEAP_ZERO_MEMORY Allocated memory initialized to zero
  • Slide 15
  • 15 MANAGING HEAP MEMORY (2 of 4) BOOL HeapFree (HANDLE hHeap, DWORD dwFlags, LPVOID lpMem) dwFlags Should be zero (or HEAP_NO_SERIALIZE ) lpMem Should have a value returned by HeapAlloc or HeapReAlloc hHeap Should be the heap that lpMem was allocated from
  • Slide 16
  • 16 MANAGING HEAP MEMORY (3 of 4) LPVOID HeapReAlloc (HANDLE hHeap, DWORD dwFlags, LPVOID lpMem, DWORD dwBytes) Return: Pointer to the reallocated block. Failure returns NULL or causes exception. dwFlags Some essential control options: HEAP_GENERATE_EXCEPTIONS and HEAP_NO_SERIALIZE HEAP_ZERO_MEMORY Only newly allocated memory is initialized HEAP_REALLOC_IN_PLACE_ONLY Do not move the block lpMem Existing block in hHeap to be reallocated dwByte New block size
  • Slide 17
  • 17 MANAGING HEAP MEMORY (4 of 4) DWORD HeapSize (HANDLE hHeap, DWORD dwFlags, LPVOID lpMem) Return: The size of the block or zero on failure.
  • Slide 18
  • 18 HEAP FLAGS (1 of 2) HEAP_NO_SERIALIZE Specified in HeapCreate, HeapAlloc, and other functions Performance gain (about 15% in tests) as functions do not provide mutual exclusion to threads accessing the heap Can safely be used if (BUT, BE CAREFUL): Your process uses only a single thread Each thread has its own heap(s) that no other thread can access You provide your own mutual exclusion mechanism to prevent concurrent access to a heap by several threads You use HeapLock and HeapUnlock
  • Slide 19
  • 19 HEAP FLAGS (2 of 2) HEAP_GENERATE_EXCEPTIONS Allows you to avoid error tests after each allocation
  • Slide 20
  • 20 OTHER HEAP FUNCTIONS HeapValidate Determine whether a heap has been corrupted HeapCompact Combine adjacent free blocks; decommit large free blocks HeapWalk Determine all blocks allocated within a heap
  • Slide 21
  • 21 EXAMPLE-A Write a program, sortHP, which reads fixed-size records from a file into a memory-allocated buffer in a heap, where the first 8 characters are a birth date (CCYYMMDD format). The rest of the record is a line of text. Enter each date in an array, along with a file position. Each array element will contain the date and the file position of the record (which is not fixed length). Sort the array using the C library qsort function. Print out the complete file sorted by birth date. Repeat the process for each file on the command line. Before each new file, destroy the heaps from the preceding file.
  • Slide 22
  • 22 EXAMPLE-A The TestData directory contains two text files with 64- byte records that can be used to test your program. Or, use the RandFile program to generate sortable files of any size.
  • Slide 23
  • 23 EXAMPLE-A Modify the sort program to create sortBT, which enters the records in to a binary search tree and then scans the tree to display the records in order. Allocate the tree nodes and the data in separate heaps. Destroy the heaps before sorting the next file, rather than freeing individual tree nodes and data elements. Test the program with and without heap serialization and determine whether there is a detectable performance difference.
  • Slide 24
  • Part II Memory-Mapped Files
  • Slide 25
  • 25 MEMORY-MAPPED FILES Advantages to mapping your virtual memory space directly to normal files rather than the paging file: You never need to perform direct file I/O Data structures you create are saved in the file You can use in-memory algorithms (string processing, sorts, search trees) to process data even though the file may be much larger than available physical memory There is no need to manage buffers and the file data they contain Multiple processes can share memory (this is the only way), and the file views will be coherent There is no need to consume space in the paging file
  • Slide 26
  • 26 PROCESS ADDRESS SPACE MAPPED TO A FILE Program fH = CreateFile ( ); mH = CreateFileMapping (fH); while ( ) { pRecA = MapViewOfFile (mH); pRecB = MapViewOfFile (mH); pRecB -> Data = pRecA -> Data; UnmapViewOfFile (pRecA); UnmapViewOfFile (pRecB); } CloseHandle (mH); CloseHandle (fH); Process Address Space File
  • Slide 27
  • 27 FILE-MAPPING OBJECTS (1 of 4) HANDLE CreateFileMapping (HANDLE hFile, LPSECURITY_ATTRIBUTES lpsa, DWORD dwProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCTSTR lpMapName) Return: A file mapping handle or NULL
  • Slide 28
  • 28 FILE-MAPPING OBJECTS (2 of 4) Parameters hFile Open file handle; protection flags compatible with dwProtect LPSECURITY_ATTRIBUTES NULL for now dwProtect How you can access the mapped file: PAGE_READONLY Pages in the mapped region are read only PAGE_READWRI