Customizing Memory Management

SOM is designed to be policy free and highly adaptable. Most of the SOM behavior can be customized by subclassing the built-in classes and overriding their methods, or by replacing selected functions in the SOM run-time library with application code. This chapter contains more advanced topics describing how to customize the following aspects of SOM behavior: memory management, dynamic class loading and unloading, character output, error handling, and method resolution. Information on customizing Distributed SOM is provided in Chapter 6.

The memory management functions used by the SOM run-time environment are a subset of those supplied in the ANSI C standard library. They have the same calling interface and return the equivalent types of results as their ANSI C counterparts, but include some supplemental error checking. Errors detected in these functions result in the invocation of the error-handling function to which SOMError points.

The correspondence between the SOM memory-management function variables and their ANSI standard library equivalents is given in the table below.

Memory-Management Functions

┌───────────────┬───────────────┬────────────┬──────────────────┐
│SOM FUNCTION   │ANSI STANDARD C│RETURN TYPE │ARGUMENT TYPES    │
│VARIABLE       │LIBRARY        │            │                  │
│               │FUNCTION       │            │                  │
├───────────────┼───────────────┼────────────┼──────────────────┤
│SOMCalloc      │calloc( )      │somToken    │size_t, size_t    │
├───────────────┼───────────────┼────────────┼──────────────────┤
│SOMFree        │free( )        │void        │somToken          │
├───────────────┼───────────────┼────────────┼──────────────────┤
│SOMMalloc      │malloc( )      │somToken    │size_t            │
├───────────────┼───────────────┼────────────┼──────────────────┤
│SOMRealloc     │realloc( )     │somToken    │somToken, size_t  │
└───────────────┴───────────────┴────────────┴──────────────────┘

An application program can replace SOM's memory management functions with its own memory management functions to change the way SOM allocates memory (for example, to perform all memory allocations as suballocations in a shared memory heap). This replacement is possible because SOMCalloc, SOMMalloc, SOMRealloc, and SOMFree are actually global variables that point to SOM's default memory management functions, rather than being the names of the functions themselves. Thus, an application program can replace SOM's default memory management functions by assigning the entry-point address of the user-defined memory management function to the appropriate global variable. For example, to replace the default free procedure with the user-defined function MyFree (which must have the same signature as the ANSI C free function), an application program would require the following code:

#include <som.h>
/* Define a replacement routine: */

#ifdef __OS2__                  /* not for SOM 3.0 */
#pragma linkage(myFree, system) /* not for SOM 3.0 */
#endif                          /* not for SOM 3.0 */

void SOMLINK myFree (somToken memPtr)
{
     (Customized code goes here)
}
...
SOMFree = myFree;

Note: In general, all of these routines should be replaced as a group. For instance, if an application supplies a customized version of SOMMalloc, it should also supply correponding SOMCalloc, SOMFree, and SOMRealloc functions that conform to this same style of memory management.