Article View: comp.lang.modula2
Article #1286JPI Short Heap Example
From: Larry M. Jordan
Date: Wed, 24 Oct 1990 17:57
Date: Wed, 24 Oct 1990 17:57
83 lines
2597 bytes
2597 bytes
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.
Technical Details
Message-ID:
<89521@aerospace.AERO.ORG>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!crdgw1!uakari.primate.wisc.edu!sdd.hp.com!elroy.jpl.nasa.gov!aero!jordanShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!crdgw1!uakari.primate.wisc.edu!sdd.hp.com!elroy.jpl.nasa.gov!aero!jordan From: jordan@aerospace.aero.org (Larry M. Jordan) Newsgroups: comp.lang.modula2 Subject: JPI Short Heap Example Message-ID: <89521@aerospace.AERO.ORG> Date: 24 Oct 90 17:57:00 GMT Organization: The Aerospace Corporation, El Segundo, CA Lines: 83