P_getheap and P_rtnheap Procedures [ HP Pascal/iX Programmer's Guide ] MPE/iX 5.0 Documentation
HP Pascal/iX Programmer's Guide
P_getheap and P_rtnheap Procedures
The procedures p_getheap and p_rtnheap are intrinsics in the Pascal
run-time library. Any program that runs on the operating system can call
them, regardless of the language in which it is written. (For more
information on intrinsics, Chapter 10 ).
The procedure p_getheap tries to allocate a region of heap space of a
specified size and alignment. If it succeeds, it "points" its VAR
pointer parameter at the first element of the region and assigns its VAR
Boolean parameter the value true. If it fails, it assigns its VAR
Boolean parameter the value false.
Syntax
p_getheap (VAR regptr : localanyptr;
regsize : integer;
alignment : integer;
VAR ok : Boolean);
Parameters
regptr If p_getheap can allocate the region of heap space, it
"points" regptr at the first element of the region (that
is, p_getheap assigns the address of the first element of
the region to regptr).
regsize The size of the region of heap space, in bytes.
alignment Integer: Specifies the region of heap space to be:
1 Byte-aligned
2 Halfword-aligned
4 Word-aligned
8 Double-word-aligned
16 16-byte aligned
32 32-byte aligned
64 64-byte aligned
2048 Page-aligned
ok If p_getheap can allocate the region of heap space, it
assigns ok the value true; if not, it assigns ok the value
false.
The procedure p_rtnheap tries to deallocate a region of heap space that
the p_getheap procedure allocated. If it succeeds, it assigns its VAR
Boolean parameter the value true. If it fails, it assigns its VAR
Boolean parameter the value false. P_rtnheap does not close files
residing in the region allocated by p_getheap.
Syntax
p_rtnheap (VAR regptr : localanyptr;
regsize : integer;
alignment : integer;
VAR ok : Boolean);
Parameters
regptr A pointer whose current value was assigned to it by
the procedure p_getheap.
regsize The size in bytes of the region of heap space that
p_getheap assigned to regptr.
alignment The number that specified the alignment of the
region of heap space that p_getheap assigned to
regptr.
ok If p_rtnheap can deallocate the region of heap
space, it assigns ok the value true; if not, it
assigns ok the value false.
Example 1
$STANDARD_LEVEL 'HP_MODCAL'$
PROGRAM prog;
TYPE
intpointer = ^integer;
VAR
b : Boolean;
i : integer;
ptr1,
ptr2 : intpointer;
PROCEDURE p_getheap (VAR regptr : intpointer;
regsize : integer;
alignment : integer;
VAR ok : Boolean); EXTERNAL;
PROCEDURE p_rtnheap (VAR regptr : intpointer;
regsize : integer;
alignment : integer;
VAR ok : Boolean); EXTERNAL;
BEGIN
p_getheap(ptr1,40,4,b); {allocate a 40-byte region}
ptr2 := ptr1; {save ptr1 for later call to p_rtnheap}
FOR i := 1 TO 10 DO BEGIN
ptr2^ := i;
ptr2 := addtopointer(ptr2,4);
END;
p_rtnheap(ptr1,40,4,b); {deallocate the 40-byte region}
p_getheap(ptr1,50,2,b);
p_rtnheap(ptr1,20,2,b); {illegal -- 20 must be 50}
p_getheap(ptr1,16,8,b);
p_rtnheap(ptr1,16,1,b); {illegal -- 1 must be 8}
END.
The procedures p_getheap and p_rtnheap are independent from the
procedures mark, release, new, and dispose.
Example 2
$STANDARD_LEVEL 'HP_MODCAL'$
PROGRAM prog;
VAR
i : integer;
b : Boolean;
p1,p2,p3,
ptr1, ptr2, ptr3 : ^integer;
PROCEDURE p_getheap; INTRINSIC;
PROCEDURE p_rtnheap; INTRINSIC;
BEGIN
p_getheap(ptr1,28,4,b); {allocate a 28-byte region}
ptr3 := ptr1; {assign values in the 28-byte region}
FOR i := 1 TO 7 DO BEGIN
ptr3^ := i;
ptr3 := addtopointer(ptr3,4);
END;
ptr3 := ptr1;
mark(ptr2); {mark the heap}
new(p1); {allocate p1, p2, and p3}
new(p2);
new(p3);
p_rtnheap(ptr1,28,4,b); {deallocate the 28-byte region}
ptr3^ := 0; {illegal -- p_rtnheap deallocated ptr3^}
p1^ := 1; {p_rtnheap did not deallocate p1, p2, or p3;}
p2^ := 2; {they are still accessible}
p3^ := 3;
p_getheap(ptr1,4,4,b); {allocate a 4-byte region}
(Example continued on next page.)
release(ptr2);
ptr1^ := 0; {The 4-byte region was not
deallocated, and the values
in it are still accessible}
p1^ := p2^ + p3^; {illegal -- p1, p2, and p3 were deallocated}
END.
MPE/iX 5.0 Documentation