O'Reilly logo

Shared Source CLI Essentials by Ted Neward, Geoff Shilling, David Stutz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required