The examples below show the use of MM_LNKFR/MEM_REHP to set up a user heap and to return space to the user heap.
Setting up a user heap
We suppose that A6 points to the program's data space and that heapad is the offset to a long word containing the pointer to free space.
moveq #MT_ALCHP,d0 ; $18 move.l space,d1 ; Number of bytes required. moveq #-1,d2 ; This job trap #1 ; The base address is set in A0 tst.l d0 bne.s Error_handler ; Oops! lea heapad(a6),a1 ; Set the pointer to the pointer to the user heap clr.l (a1) ; Set the pointer to zero movea.w mm_lnkfr,a2 ; Put the vector in A2 . . jsr (a2) ; . . and use it ; Now the user heap is set up at A0 space dc.l $2000 ; Amount of space needed
Returning space to the user heap
Suppose that buffer(a6) is the pointer to a buffer allocated from the user heap and that buff_len(a6) contains its length. The code shows this buffer being returned to the user heap.
movea.l buffer(a6),a0 ; Base of space to be freed move.l buff_len(a6),d1 ; Size of space lea heapad(a6),a1 ; Set the pointer to the pointer to the user heap movea.w mm_lnkfr,a2 ; Put the vector in A2 . . jsr (a2) ; . . and use it