Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: Memory /
Chapter 2 - Memory Manager / Memory Manager Reference
Memory Manager Routines / Freeing Memory


CompactMem

The Memory Manager compacts the heap for you when you make a memory request that it can't fill. However, you can use the CompactMem function to compact the current heap zone manually.

FUNCTION CompactMem (cbNeeded: Size): Size;
cbNeeded
The size, in bytes, of the block for which CompactMem should attempt to make room.
DESCRIPTION
The CompactMem function compacts the current heap zone by moving unlocked, relocatable blocks down until they encounter nonrelocatable blocks or locked, relocatable blocks, but not by purging blocks. It continues compacting until it either finds a contiguous block of at least cbNeeded free bytes or has compacted the entire zone.

The CompactMem function returns the size, in bytes, of the largest contiguous free block for which it could make room, but it does not actually allocate that block.

To compact the entire heap zone, call CompactMem(maxSize). The Memory Manager defines the constant maxSize for the largest contiguous block possible in the 24-bit Memory Manager:

CONST
   maxSize           = $800000;        {maximum size of a block}
SPECIAL CONSIDERATIONS
Because CompactMem moves memory, you should not call it at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION
The registers on entry and exit for CompactMem are
Registers on entry
D0Size of block to make room for
Registers on exit
D0Size of largest allocatable block

The CompactMem function compacts the current heap zone. If you want to compact the system heap zone rather than the current heap zone, set bit 10 of the routine trap word. In most development systems, you can do this by supplying the word SYS as the second argument to the routine macro, as follows:

_CompactMem ,SYS
RESULT CODES
noErr0No error

Previous Book Contents Book Index Next

© Apple Computer, Inc.
3 JUL 1996