[comp.sys.atari.st] Malloc vs. malloc in MWC

dag@per2.UUCP (Daniel A. Glasser) (01/17/90)

In article <1534@cs.rit.edu>, ajy2208%ritcv@cs.rit.edu writes:
> I have Mark Williams C, and am curious:  What is the difference between
> malloc() and Malloc() ????  I am writing a slideshow/picture viewing
> program, and when I used malloc() to get a 32k block of memory for a
> picture, the picture always had some stray data at the top (looked
> suspiciously like the other variables I was using)..  However, when I
> used Malloc() this disappeared.  Boy, I almost pulled my hair out
> trying to figure out what was wrong!!!

Malloc is a C macro binding (in the file osbind.h) which builds a trap
to GEMDOS for memory its allocation function (0x48) in line.  (Unless
you use the -VNOTRAPS option, which I don't recommend.)  This function
takes a long parameter which is the size of the memory you want allocated
or -1L if you want to know the size of the largest chunk of memory available
from the system.  The standard library function malloc() takes a single
unsigned integer (2 bytes) and allocates memory from the library managed
memory pool.  If there is insufficient memory in the library managed free
pool, malloc() requests more memory from the system through a Malloc() call.
The MWC library function lmalloc() is similar to malloc() except that it
takes a long argument, thus allowing allocation of chunks of memory larger
than 64k bytes.

When using malloc(), lmalloc(), calloc(), lcalloc(), realloc(), and lrealloc(),
you free the memory with the library routine free().  When you use the GEMDOS
function Malloc, you free the memory with the GEMDOS function Mfree.  Never
use free() on memory you've obtained with Malloc, and never use Mfree on
memory you've obtained from one of the library memory management routines.

This is all documented in the Mark Williams C manual.

The ST hardware enforces alignment restrictions on displayed memory.
Memory blocks returned by the MWC library allocation routines don't
necessarily conform to those restrictions, so it is useful to allocate
additional memory and keep an aligned pointer as well as an allocation
pointer.  Use the aligned pointer in setting the physical screen base
and for picture reads.  The XBIOS function Setscreen just truncates the
low few bits of the supplied address (actually, I think the retrace
routine does the truncation, but I don't remember for sure.)  This
results in a lower address than that specified when given a non-aligned
address.  These restrictions may vary between the ST and the STE/TT.

Also, malloc() does not zero out allocated memory, Malloc returns chunks
of memory which have most likely been untouched since the program was
started, which on some versions of TOS clears all free system memory.

I hope this all helps.

					Daniel A. Glasser
-- 
 _____________________________________________________________________________
    Daniel A. Glasser                 "Their brains were small and they died."
    uwvax.cs.wisc.edu!per2.uucp!dag
 ---Persoft, Inc.---------465 Science Drive-------Madison, WI 53711-----------