comp.lang.ada
 help / color / mirror / Atom feed
From: brennanw@wanda.vf.pond.com (Bill Brennan)
Subject: Re: Question about new allocator
Date: 1997/02/11
Date: 1997-02-11T00:00:00+00:00	[thread overview]
Message-ID: <5dqeee$2qn@wanda.vf.pond.com> (raw)
In-Reply-To: Pine.SOL.3.91.970210095755.3813A-100000@general3.asu.edu


In article <Pine.SOL.3.91.970210095755.3813A-100000@general3.asu.edu>,
 <jes2358@imap3.asu.edu> wrote:
>I am wondering if the new allocator will return NULL if the heap is full 
>as it does in C++ or if Ada has some other convention.  I am new to Ada 
>and can't seem to find this discussed anywhere.  The context I am 
>thinking about this is in trying to write a Boolean type IsFull function 
>for a Queue package.  If you can help with a suggestion, please email to
>jsherman@asu.edu.  Thanks in advance.

The method in which Ada communicates that the heap is exahausted is
to raise the Storage_Error exception.  Your IsFull function would look
something like this:

function IsFull( Queue: Queue_Type ) return Boolean is

   Heap_Exhausted: Boolean := True;

   Dummy_Ptr: Item_Ptr_Type;

   procedure Delete is new Unchecked_Deallocation( Item_Type, Item_Ptr_Type );

begin
   begin
      Dummy_Ptr := new Item_Type;
      Delete( Dummy_Ptr );  -- restore the Item to the heap.
   exception 
   when Storage_Error =>
      Heap_Exhausted := True; 
   end;
   return Heap_Exhausted;
end;


(this hasn't been compiled, so beware of small errors!)

In general, I find that a function like "IsFull" is not very useful for
allocating off of the heap.  For one thing, it's quite inefficient to 
allocate-deallocate-reallocate for every addition to the Queue (assuming
you're calling IsFull before each addition).  It's more efficient and
idiomatic to include an exception handler for the dreaded Storage_Error
in the same code which would call IsFull.

--bill







  reply	other threads:[~1997-02-11  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-02-10  0:00 Question about new allocator jes2358
1997-02-11  0:00 ` Bill Brennan [this message]
1997-02-12  0:00 ` Keith Allan Shillington
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox