home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
progc
/
c_all592.arj
/
TI863.ASC
< prev
next >
Wrap
Text File
|
1992-03-17
|
3KB
|
133 lines
PRODUCT : Borland C++ NUMBER : 863
VERSION : 3.0
OS : WIN
DATE : March 17, 1992 PAGE : 1/2
TITLE : Understanding Memory Allocation Under Windows
This document is a brief summary of how functions such as malloc,
calloc, and new allocate memory in a Windows application created
with Borland C++ 3.0
In all cases, new calls malloc to allocate the memory. What
happens next depends on the memory model your in and whether your
building a Dynamic Link Library (.DLL) or an Exectuable (.EXE).
If you are in the small or medium memory model building an .EXE
file, where data pointers default to near...
new calls malloc which calls LocalAlloc( LMEM_FIXED );
calloc calls LocalAlloc( LMEM_FIXED | LMEM_ZEROINIT );
If you are in the compact or large model building an .EXE file,
where data pointers default to far, malloc calls farmalloc...
new calls malloc which calls farmalloc which calls
GlobalLock( GlobalAlloc( GMEM_MOVEABLE | _WinAllocFlag ) );
calloc calls farcalloc which ultimately make a call to
GlobalLock( GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT |
_WinAllocFlag );
If you are in any memory model (Small, Medium, Compact, or Large)
building a .DLL file, all data pointers, by default, are far and
the procedure used by the Runtime Library to allocate memory is
exactly the same as a large model .EXE. Large model .EXE files
are discussed in the previous paragraph.
_WinAllocFlag is a WORD in the startup code which is always 0.
One could set it to GMEM_SHARE or whatever other flags are needed
before doing a new or malloc. To use it, first make an extern
for it in your program...
extern WORD _WinAllocFlag;
Some additional Notes, in Borland C++ 3.0, and in the Object
Windows Library for Borland C++ 2.0, Borland has implemented a
suballocation scheme for use with global allocations. This means
memory is allocated as discussed before, but if a memory request
is small, a pointer will be returned into a larger chunk. This
reduces the amount of selectors Windows has to allocate.
PRODUCT : Borland C++ NUMBER : 863
VERSION : 3.0
OS : WIN
DATE : March 17, 1992 PAGE : 2/2
TITLE : Understanding Memory Allocation Under Windows
In Borland C++ 3.0, the size of the chunks the runtime library
allocates are always 4K of RAM. If you ask for more than 4K, the
request for memory goes straight to GlobalAlloc, and you can
expect an offset of 0 for your pointer.
In BC3, if you link to the static libraries and you set
_WinAllocFlag to GMEM_DDESHARE before doing an allocation, we do
not use the sub allocator, your memory request maps directly to
GlobalAlloc. Several customers have suggested this be changed in
a future release. Borland is currently looking into this matter.