 |
» |
|
|
 |
The predefined procedure mark takes a
pointer variable p as a parameter, marks the
state of the heap, and sets the value of p
to specify that state. The pointer variable p is called a mark
(once a pointer variable becomes a mark, you cannot dereference
it). You can allocate heap space beyond the mark, and then deallocate
that space with the predefined procedure release. The predefined procedure release takes
a mark pointer variable as a parameter and deallocates the heap
space that was dynamically allocated after the mark was set. Variables
in that space become inaccessible. Files in that space are closed.
After release executes, the mark pointer variable
is undefined. The procedure new can reallocate
the released space (even if the program does not contain the compiler
option HEAP_DISPOSE). Example 1 PROGRAM prog; TYPE ftype = FILE OF integer; ptype1 = ^ftype; ptype2 = ^integer; VAR fptr : ptype1; iptr1, iptr2, m, iptr3, iptr4: ptype2; BEGIN new(iptr1); {Allocate heap space to iptr1^} new(iptr2); {Allocate heap space to iptr2^} iptr1^ := 0; iptr2^ := 0; mark(m); {Mark the heap with m} new(iptr3); {Allocate heap space to iptr3^} new(iptr4); {Allocate heap space to iptr4^} new(fptr); {Allocate heap space to fptr^, a file} iptr3^ := 0; iptr4^ := 0; reset(fptr^); {Open fptr^} release(m); {Close fptr^, deallocating heap after m} iptr1^ := 1; iptr2^ := 2; iptr3^ := 3; {illegal iptr3^ was deallocated} iptr4^ := 4; {illegal iptr4^ was deallocated} write(fptr^,5); {illegal iptr5^ was deallocated} m^ := 0; {illegal cannot assign value to mark pointer} END. |
The parameter of mark (the mark) can
be any pointer variable. The parameter of release must be a mark
— a pointer variable whose current value was assigned by
the mark procedure. It is an error to call
release with a pointer whose current value
was not assigned by the mark procedure. Example 2 PROGRAM prog; TYPE ptr1 = ^integer; ptr2 = ^real; ptr3 = ^char; ptr4 = ^ptr3; VAR m1 : ptr1; m2 : ptr2; m3 : ptr3; m4 : ptr4; m6 : ptr1; r : RECORD i : integer; m5 : ptr1; END; BEGIN mark(m1); mark(m2); mark(m3); new(m4); {m4^ is of type ptr3} mark(m4^); mark(r.m5); new(m6); release(m6); {illegal current value of m6 was assigned by new} END. |
If you set several marks, and release one of them, those set
after it are also released. Example 3 PROGRAM prog; TYPE ptr = ^integer; VAR m1, m2, i1, i2, i3, j1, j2, j3, k1, k2, k3 : ptr; BEGIN new(i1); new(i2); new(i3); mark(m1); new(j1); new(j2); new(j3); mark(m2); new(k1); new(k2); new(k3); release(m1); {deallocates j1,j2,j3,k1,k2,k3; releases m1 and m2} release(m2); {illegal m2 is undefined because it was released with m1} END. |
|