Presentation is loading. Please wait.

Presentation is loading. Please wait.

Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

Similar presentations


Presentation on theme: "Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)"— Presentation transcript:

1 Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)

2 Where are we?  We’ve covered threads in glorious detail  But to really take advantage of threads, we need to know a lot more about Windows Memory Architecture Why?

3 Why Study Memory  “How do I share data between applications?”  “Where is this information stored?”  “Why is my program so sloooooow?”  Understanding how the system uses memory helps us answer these questions and more…

4 Process Memory  Every 32-bit process has its own virtual address space  4GB (why?)  How much for a 64-bit process?  As the space is virtual, memory by default isn’t shared between processes (but is between threads… why?)

5 Virtualization  Two different processes can have data stored at 0x12345678… but the values are different  Each Virtual space is partitioned into different areas

6 Null-Pointer Assignment  If you point to a NULL pointer, you want your code to throw an exception  This is accomplished by the NULL-pointer assignment partition space

7 User-Mode Partition  Contains the process’ private (unshared) address space  Not accessible from other Processes  However, not all of this is directly useable by the application

8 Kernel-mode partition  Used by the Kernel for various process- related things  Everything in this partition is shared among processes

9 Regions in Address Space  When a process is created, most of its virtual memory is free  To use this space, must call VirtualAlloc  Reserving memory happens in blocks called “chunks” (allocation granularity, to use a fancy term)  Memory is reserved in multiples of the systems “page” size

10 But…  You must free memory if you use it  Call VirtualFree

11 Physical Storage  If every 32-bit process has 4GB of storage, don’t I run out of memory?  No… because of the paging file  Transparent in operation  Pages blocks of memory to disk from RAM  So, Memory works a lot like this…

12 Paging

13 Thrashing  When you’re really short of memory, an application can thrash (demo…)  Happens when the OS spends most of its time fetching memory from disk  Hence, to speed up your machine add RAM (thanks Nate!)

14 Memory Protection  It is possible to share physical memory…  Thus, you need to know about memory protection attributes

15 Flags  PAGE_NOACCESS – Any attempt to read, write or execute raises a violation  PAGE_READONLY  PAGE_READWRITE  PAGE_EXECUTE  PAGE_EXECUTE_READ  PAGE_EXECUTE_READWRITE  PAGE_WRITECOPY – Attempts to write to this page causes the application to get its own private copy of this page  PAGE_EXECUTE_WRITECOPY

16 Copy On Write  Interesting idea  Memory is shared until you write  Exists to conserve RAM and prevent thrashing  When a write is detected: OS finds a free page in RAM Copies the page to the new page and marks it PAGE_READWRITE or PAGE_EXECUTE_READWRITE Updates the process’ page tables

17 Special Flags  PAGE_NOCACHE – Disable Cacheing on this page (why… Hardware)  PAGE_WRITECOMBINE – Multiple writes are linked together  PAGE_GUARD – alert when a write occurs

18 Assignment  Create a command line program in Visual Studio  Write an application which allocates memory and tries different protection schemes  Show how the system throws exceptions when you access this memory (for example, try and execute code from the memory location when PAGE_READONLY is set)  Use an exception handler to catch this exception  Reset the PAGE to EXECUTE and show how the exception is no longer thrown…


Download ppt "Win32 Programming Lesson 14: Introducing Windows Memory (C Rox…)"

Similar presentations


Ads by Google