Managing Memory with the PAL

Memory is a fundamental computational resource, and not surprisingly, the PAL provides memory allocation as a service to the SSCLI. The four Win32 routines, VirtualAlloc, VirtualFree, VirtualProtect, and VirtualQuery, form the basis for this service and are what lie beneath the intricate memory management mechanisms that we examined in Chapter 7. In the Unix PAL, the logical implementation choice would seem to be to use the mmap functions as a basis for these APIs. This is, in fact, ultimately the choice, although a few problems had to be solved along the way to get the code to work, mostly having to do with the lower-level nature of mmap.

While mmap has the ability to allocate memory in a way that maps nicely to VirtualAlloc, there is no standard equivalent to VirtualQuery. What is ore problematical is that some implementations of mmap are unable to provide memory at a specific virtual address and, instead, assign the address unilaterally. (The hint parameter, which is provided in the API for this purpose, is honored by most implementations but not all.) VirtualAlloc, of course, has a nearly opposite usage: allocation either happens at the location specified or the request fails.

The SSCLI depends on the VirtualAlloc approach, since several of its algorithms use structures that have known starting addresses to simplify address calculations. Because of this, and because of the lack of VirtualQuery functionality, the PAL implements extra infrastructure ...

Get Shared Source CLI Essentials now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.