[comp.lang.modula2] JPI Short Heap Example

jordan@aerospace.aero.org (Larry M. Jordan) (10/25/90)

 The ShtHeap module in r1.06 works, but, don't expect to use the 
example code offered in the User's Guide to prove it.  I spent a few
trying minutes writing a memory-model-independent test that does work
and thought there may be someone out there who could benefit from my
headaches.)  The following compilable sample shows how to use the ShtHeap 
and based pointers, which have been around since v1.17, but until now I've
had no opportunity to use.  

  In summary:  The test module allocates a block from the far heap 
(via Storage module) and then grabs the paragraph address to give it to the 
ShtHeap manager.  Many allocations (more than 2) are made.  All of this 
storage is deallocated and the short heap integrity is checked 
after each deallocation.

The code:

MODULE TestHeap;

  IMPORT Storage;
  IMPORT ShtHeap;

  CONST 
    Low  =   1; 
    High = 100;

  TYPE
    T = CARDINAL;
    FarPointer = RECORD
	           offset, segment: CARDINAL
                 END;

  VAR
    ShortHeapAddress: FarADDRESS;
    ShortHeapSegment: CARDINAL;

    p: ARRAY [Low..High] OF POINTER ShortHeapSegment TO T;  
  
    i: CARDINAL;

BEGIN

  Storage.FarAllocate(ShortHeapAddress, 10000);

  (* 
   * We want the segment part of the pointer, and NOT:
   *
   *    ShortHeapSegment := CARDINAL(Seg(ShortHeapAddress));
   *
   * as indicated in the User's Guide which returns the segment "containing" 
   * the pointer.  Disasterous--we've given our data segment to
   * the ShtHeap manager to manage!
   *
   * But rather:
   *)

  ShortHeapSegment := FarPointer(ShortHeapAddress).segment;

  (*
   * Initialize the short heap.  We can ask that part or all be
   * managed.  Here we ask for all of it.
   *)
  ShtHeap.Initialize(ShortHeapSegment, 10000);

  (* allocate a bunch of CARDINALs *)

  FOR i := Low TO High DO
    ShtHeap.Allocate(ShortHeapSegment, p[i], SIZE(T));
    IF p[i] = ShtHeap.Nil THEN
      HALT
    END;
    p[i]^ := i;   (* use it *)
  END;

  (* deallocate them *)

  FOR i := Low TO High DO
    ShtHeap.Free(ShortHeapSegment, p[i], SIZE(T));
    (* check heap integrity *)
    ShtHeap.Test(ShortHeapSegment); 
  END;

END HeapTest.

Dominique.Willems@p4610.f46.n295.z2.fidonet.org (Dominique Willems) (10/31/90)

Hi Larry,

 > ... thought there may be someone out there who could benefit from my
 > headaches.)  The following compilable sample shows how to use the
 > ShtHeap and based pointers,...

Indeed, you were VERY right! I wast just rewriting lots of code to work 
with the Short Heap system and had already coded (luckily not yet 
executed) the BAD JPI Initialize example, without their wrong ALLOCATE 
invocation of course (which in their manual is treated as a FUNCTION 
PROCEDURE instead of a proper one!). 

 >    * But rather:
 >    *)
 >
 >   ShortHeapSegment := FarPointer(ShortHeapAddress).segment;

Thanks indeed, I had already blindly copied their example (shame on me to 
trust them! :-)). Imagine that; in a mere three lines of example code, they 
managed to put TWO grave errors (=66%)!

Enfin, let's keep smiling! :-)

Thanks again,
Cheers,
Domus


--  
uucp: uunet!m2xenix!puddle!2!295!46.4610!Dominique.Willems
Internet: Dominique.Willems@p4610.f46.n295.z2.fidonet.org