Win32 Programming Lesson 16: Virtual Memory. Where are we?  We’ve covered the theory of Windows memory, and poked around some  Now let’s use how to.

Slides:



Advertisements
Similar presentations
Process A process is usually defined as an instance of a running program and consists of two components: A kernel object that the operating system uses.
Advertisements

Memory Protection: Kernel and User Address Spaces  Background  Address binding  How memory protection is achieved.
The Linux Kernel: Memory Management
Win32 Programming Lesson 5: Error Codes. Before We Begin  Much of the time in this class we’ll be calling Win32 Functions  However, sometimes they’re.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Run time vs. Compile time
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
Chapter 3.7 Memory and I/O Systems. 2 Memory Management Only applies to languages with explicit memory management (C or C++) Memory problems are one of.
File System Variations and Software Caching May 19, 2000 Instructor: Gary Kimura.
Memory Management April 28, 2000 Instructor: Gary Kimura.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Pointers Applications
Cute Tricks with Virtual Memory CS 614 9/7/06 by Ari Rabkin CS 614 9/7/06 by Ari Rabkin (and why they don’t work)
Tutorial 6 Memory Management
Win32 Programming Lesson 9: Jobs & Thread Basics.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Tutorial 7 Memory Management presented by: Antonio Maiorano Paul Di Marco.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Win32 Programming Lesson 20: Advanced DLL Techniques.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
Stack and Heap Memory Stack resident variables include:
Win32 Programming Lesson 22: DLL Magic Part Deux All your base are belong to us…
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming  To allocate scarce memory resources.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
Win32 Programming Lesson 7: Kernel Objects. Abstract  Many of the concepts we’ll look at today won’t make complete sense until you use them  However,
Win32 Programming Lesson 18: More Memory Mapped Files and the HEAP (Finally, cool stuff!)
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Win32 Programming Lesson 25: Unhandled Exceptions Bet you’ve never encountered one of those, eh?
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Windows Memory Architecture 井民全製作. A Process ’ s Virtual Address Space Every Process has its own private virtual address 32-bits processes  4 GB address.
Chapter 4 Memory Management Virtual Memory.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec.
Memory Management II CS Spring Overview Logical Addressing and Virtual Memory –Logical to Linear Address Mapping –Linear to Physical Address.
Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
CNIT 127: Exploit Development Ch 8: Windows Overflows Part 2.
Win32 Programming Lesson 17: Memory Mapped Files (Finally, cool stuff again, all this work is getting tedious!)
Operating Systems Lesson 5. Plan Memory Management ◦ Memory segments types ◦ Processes & Memory ◦ Virtual Memory ◦ Virtual Memory Management ◦ Swap File.
Lecture 7 Page 1 CS 111 Summer 2013 Another Option Fixed partition allocations result in internal fragmentation – Processes don’t use all of the fixed.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 Pintos Virtual Memory Management Project (CS3204 Spring 2006 VT) Yi Ma.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Win32 Programming Lesson 15: Practical Windows Memory (If you can read this you have good vision)
Security Attacks Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
Sections Basic Data Structures. 1.5 Data Structures The way you view and structure the data that your programs manipulate greatly influences your.
Memory Mapped I/O Gregory Mortensen CSIS 4330, Advanced Windows Programming – UVSC.
Lesson Objectives Aims Key Words Paging, Segmentation, Virtual Memory
Programs – Loading and Running an Executable
Storage Management.
CS 153: Concepts of Compiler Design November 28 Class Meeting
Advanced Programming Behnam Hatami Fall 2017.
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Windows CE Memory Management
PROCESSES & THREADS ADINA-CLAUDIA STOICA.
Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/22.
COMP755 Advanced Operating Systems
Memory allocation.
Presentation transcript:

Win32 Programming Lesson 16: Virtual Memory

Where are we?  We’ve covered the theory of Windows memory, and poked around some  Now let’s use how to use memory

Three ways to Use Memory  Virtual Memory – Best for very large arrays and structures  Memory-mapped files – Good for managing data streams and sharing memory between applications  Heaps – Best for managing large numbers of small objects

Reserving Memory  Fairly straightforward: PVOID VirtualAlloc( PVOID pvAddress, SIZE_T dwSize, DWORD fdwAllocationType, DWORD fdwProtect );

Parameters  pvAddress: Usually NULL, but can be where you would like the memory  dwSize: How much memory you would like  dwAllocationType: Should we RESERVE memory or COMMIT it?  dwProtection: The type of memory protection on this area of memory

Before Use…  Once you have reserved memory, you need to commit it before the pages can be accessed  Same call, but you use the parameters slightly differently  You don’t have to COMMIT all the region – you just need to COMMIT pages  Why is this a Good Thing?

Can do it all in one go…  Can bitwise-OR the flags…  Just like this: PVOID pvMem = VirtualAlloc( NULL, 99 * 1024, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE ); Can also specify MEM_TOP_DOWN

Knowing When to Commit  Let’s say you’re writing a spreadsheet application (gee, there’s a good idea…)  Why doesn’t Excel crash the system when it loads a spreadsheet (well, usually…)

MEM_LARGE_PAGE  New addition to VirtualAlloc Higher performance, as we can allocate a large page of memory These pages are not pageable however SIZE_T GetLargePageMinimum();

Determining the State of an Address  Four ways… Always call COMMIT – but this is slow because it’s often (usually?) unnecessary Use VirtualQuery to map the state and commit if necessary Keep a record of which pages you’ve committed and which you have – can be complicated Use SEH to catch memory exceptions and COMMIT on error (best way)

Must Decommit  If you don’t want to kill the machine, it’s good to decommit memory BOOL VirtualFree( LPVOID pvAddress, SIZE_T dwSize, DWORD fdwFreeType ); Pass 0 for dwSize as the system already knows it Must pass MEM_RELEASE

Knowing when is the trick…  Consider our Spreadsheet example Make each cell a page (not very practical, but very simple) Keep track of which cells are on which page Implement a garbage collection function which checks the state of cells

Bug Hunting…  Sometimes protecting memory can help tremendously when hunting bugs and coding defensively  Can use VirtualProtect to change access when we’re not using memory  If a rogue pointer blats into your memory, you’re protected

Physical Storage  Knowledge of the underlying system can help tremendously when trying to improve performance  For example, imagine you have some memory which you use for short periods of time… How does the design of the system potentially slow things down?

RESETting Memory  When the system looks to free physical memory it has to write RAM to the paging file  But what if you say that the memory to swap isn’t important? That is, the changes don’t need to be kept?  If you have write-access but the changes aren’t needed you can RESET the memory

Example  PINT pnData = (PINT) VirtualAlloc( NULL, 1024, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); pn[0] = 100; pn[1] = 200; VirtualAlloc( (PVOID) pnData, sizeof(int), MEM_RESET, PAGE_READWRITE);

MEM_RESET  One thing to remember is that the MEM_RESET flag doesn’t work if you OR it with anything else  It only makes sense used on its own

Cool Stuff: AWE  Address Windowing Extensions  Allows the program to access more memory than fits in its process space  Introduced in Windows 2000  Uses AllocateUserPhysicalPages  Then MAP to a Window: BOOL MapUserPhysicalPages( PVOID pvAddressWindow, ULONG_PTR ulRAMPages, PULONG_PTR aRAMPages);

Stack Space  Each thread stack exists in the process’ address space  Very clever – assigns a “guard page” so that the system knows when to commit more memory to the stack  When the stack grows too far, generates a EXCEPTION_STACK_OVERFLOW  Can handle via a SEH – we’ll talk about that later in the term