PRODUCT : Borland C++ NUMBER : 1537 VERSION : 3.x OS : DOS DATE : October 25, 1993 PAGE : 1/3 TITLE : Resizing the overlay buffer at program startup /* Title: Resizing the overlay buffer at program startup The Borland C++ overlay manager works reasonably well all by itself but it would be nice to adjust performance of an overlaid program to best utilize the available resources of the system it is executing on. One simple way of changing this is to modify the overlay buffer. This is done by changing the value of the global variable _ovrBuffer. For example, the following line sets the overlay buffer to 128K: extern unsigned _ovrbuffer = 0x2000; But programs may run on systems with anywhere from 400,000 to 640,000 bytes of available conventional memory. It would be nice to query the system for the available RAM and size the buffer larger or smaller based on that number. This actually possible, but first a little background on the C startup table is required. The C startup table is a table of function pointers built by the linker. Each function in the table has a priority value from 0 to 255. At run-time, the startup code loops through the table and calls each function, in order of priority first, and order in table second. User startup functions normally are priority 64-255. See the Borland C++ Programmer's Guide under "#pragma startup" for more information on defining these functions. The run-time library has functions set up at higher priorities to ensure proper system initialization. One of these functions is __OvrPrepare(). This function sets up the overlay manager and is priority 1. The value of _ovrbuffer can be modified before this function is called with no ill effects, which is what we want. This can be done via adding a startup function that is priority 0 that checks the amount of system RAM and sets the variable based on the results. Here is an example function that could be used to check to make sure enough RAM is available. If not it sets a fail flag. Then inside main(), the flag can be checked and the program terminated if need be. */ #include // For MK_FP() macro #include // For printf() prototype PRODUCT : Borland C++ NUMBER : 1537 VERSION : 3.x OS : DOS DATE : October 25, 1993 PAGE : 2/3 TITLE : Resizing the overlay buffer at program startup #include // For min() macro in C extern unsigned _ovrbuffer = 0; // Use default size initially unsigned appFail = 0; // Fail flag if not enough RAM /* * If compiling in C++ mode, the min() and max() macros are * not available. Here is a template for an inline min function * that is actually better than the macro. */ #ifdef __cplusplus template inline T min( T x, T y ) { return (x > y) ? y : x; } #endif void _resizeOvrBuffer() { /* * _psp is the PSP of the program. Offset 2 from the PSP is a * the segment value of the top of memory (usually A000.) The * PSP itself is the base segment of the current program, thus * the size of available conventional memory before the program * loaded is the difference between segment values (times 16 if * you want units in bytes.) */ unsigned far *mem_top = (unsigned far *) MK_FP( _psp, 0x02 ); unsigned total = *mem_top - _psp; if( total < 0x7400 ) // Less than 464K and we fail. appFail = 1; else // Otherwise, size buffer between // 0x1000-0x3000 base on RAM size. _ovrbuffer = min( 0x3000U, total - 0x6400 ); /* * Remember, when inside this code, nothing has been setup on * the system, so do NOT call any RTL functions for I/O or * anything else that might rely on a startup function. This * includes accessing any global objects as their constructors * have not been called yet. */ PRODUCT : Borland C++ NUMBER : 1537 VERSION : 3.x OS : DOS DATE : October 25, 1993 PAGE : 3/3 TITLE : Resizing the overlay buffer at program startup } #pragma startup _resizeOvrBuffer 0 // Put function in table int main() { if( appFail ) { printf( "Insufficient memory to start application.\n" "464K of available conventional RAM required.\n" ); return -1; } /* Normal program code can progress from here. */ printf( "Sufficient memory to load and execute program.\n" "Overlay buffer size will be %ldK\n", _ovrbuffer * 16L / 1024 ); return 0; } DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.