Overlays and Underhandedness
String constants in the C language are considered to be static arrays of
characters accessed through a pointer constant. The arrays are
potentially writable even though their pointer is a constant. SMARTALLOC
uses the compile-time definition ./smartall.wml
to obtain the name
of the file in which a call on buffer allocation was performed. Rather
than reserve space in a buffer to save this information, SMARTALLOC
simply stores the pointer to the compiled-in text of the file name. This
works fine as long as the program does not overlay its data among
modules. If data are overlayed, the area of memory which contained the
file name at the time it was saved in the buffer may contain something
else entirely when sm_dump()
gets around to using the pointer to
edit the file name which allocated the buffer.
If you want to use SMARTALLOC in a program with overlayed data, you’ll
have to modify smartall.c to either copy the file name to a fixed-length
field added to the abufhead
structure, or else allocate storage with
malloc()
, copy the file name there, and set the abfname
pointer
to that buffer, then remember to release the buffer in sm_free
.
Either of these approaches are wasteful of storage and time, and should
be considered only if there is no alternative. Since most initial
debugging is done in non-overlayed environments, the restrictions on
SMARTALLOC with data overlaying may never prove a problem. Note that
conventional overlaying of code, by far the most common form of
overlaying, poses no problems for SMARTALLOC; you need only be concerned
if you’re using exotic tools for data overlaying on MS-DOS or other
address-space-challenged systems.
Since a C language ”constant“ string can actually be written into, most C compilers generate a unique copy of each string used in a module, even if the same constant string appears many times. In modules that contain many calls on allocation functions, this results in substantial wasted storage for the strings that identify the file name. If your compiler permits optimization of multiple occurrences of constant strings, enabling this mode will eliminate the overhead for these strings. Of course, it’s up to you to make sure choosing this compiler mode won’t wreak havoc on some other part of your program.
See also
Possible Next Steps
Go back to Smart Memory Allocation.
Go back to Developer Guide.