Dec 262017
 
Turbo Pascal 5.5 source code that will free heap memory by dynamically reducing overlay buffer during program execution.
File OVERIN.ZIP from The Programmer’s Corner in
Category Pascal Source Code
Turbo Pascal 5.5 source code that will free heap memory by dynamically reducing overlay buffer during program execution.
File Name File Size Zip Size Zip Type
OVERINIT.DOC 3785 1665 deflated
OVERINIT.PAS 2368 876 deflated

Download File OVERIN.ZIP Here

Contents of the OVERINIT.DOC file


ROB_OVERLAY: Free memory by dynamically reducing overlay buffer
during program execution. "Robbing Peter to pay Paul"

Steve Safigan 8/14/90 CompuServe ID 72251,2274
Universal Tax Systems, Inc.
1003 Clearview Ave.
Fredericksburg, VA 22405
(703) 371-9396

Donated to the public domain.
Thanks to Kim Kokonen for providing direction.

The most common means of reducing run-time memory requirements is by using
overlays. This presents a paradox, because the overlay buffer must be
fixed at program startup, reducing total free memory. The programmer is
usually forced to define the smallest overlay buffer he (she) can get away
with in order to leave enough room for dynamic variables. This slows
program performance as the program has to swap code into and out of the
overlay buffer often.

A better solution is to start the program with a very large overlay buffer
to reduce swapping. As the program requires more dynamic memory, the
boundary between the overlay buffer and the heap can be adjusted to provide
more memory to the heap, and less to the overlay buffer. Eventually,
the overlay buffer may be reduced to a minimum size. At this point, the
program may be thrashing terribly, but it may be more acceptable than
running out of memory completely.

Unfortunately, TP5.5 does not support changing the overlay buffer size
after any variables are allocated. But the routine ROB_OVERLAY does.
It works by clearing the overlay buffer, changing the boundary between the
overlay buffer and the heap, then adding a free list entry to the heap.

ROB_OVERLAY must be called explicitly by your program. It will not
automatically be called whenever you run out of heap space. If you call
a single procedure to allocate all of your dynamic variables (such as
OPro's GETMEMCHK procedure), then you can simply add a line of the
following form:

IF MEMAVAIL < 20000 THEN ROB_OVERLAY(MEMORY_AMT);

MEMORY_AMT is the amount of memory to rob from the overlay buffer. It
must be passed via a variable, because ROB_OVERLAY returns the
amount of memory actually freed. If the overlay buffer is already at
its minimum size, or if no heap free list entries are available,
ROB_OVERLAY returns 0. The maximum amount of memory freed in a single
call to ROB_OVERLAY is 64K. ROB_OVERLAY will round the memory freed
to the next lower multiple of 16 bytes, so that it frees an even
number of paragraphs.

Although calling ROB_OVERLAY is as simple as that, there are a few
additional steps needed in order to initialize. The sample unit OVERINIT
shows initializing the memory buffer in preparation for ROB_OVERLAY.
Since this unit initializes the overlay buffer, it should appear first
in your USES clause. You may replace the overlay initialization section
with your own, but I've left my own initialization section as an example.

First the variable OVRMINBUF must be initialized to the minimum overlay
buffer size your program will accept. This value must not be less than
the initial value of OVRGETBUF, or you will not be able to fit your
largest overlayed unit into the overlay buffer. Note that the example
sets a different minimum buffer size depending on whether expanded
memory is available or not. The program can tolerate a little overlay
thrashing as long as it is thrashing to memory, rather than to disk.

Then the overlay buffer is expanded to its maximum size (in the example,
to its original size plus 128K). It is then up to the program to
call ROB_OVERLAY whenever total heap space gets low. ROB_OVERLAY will
never let the size of the overlay buffer be reduced to less than
OVRMINBUF.

If anybody has any corrections or enhancements to ROB_OVERLAY, contact
me at the above address.


 December 26, 2017  Add comments

Leave a Reply