comp.lang.ada
 help / color / mirror / Atom feed
* Question about new allocator
@ 1997-02-10  0:00 jes2358
  1997-02-11  0:00 ` Bill Brennan
  1997-02-12  0:00 ` Keith Allan Shillington
  0 siblings, 2 replies; 3+ messages in thread
From: jes2358 @ 1997-02-10  0:00 UTC (permalink / raw)



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.




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Question about new allocator
  1997-02-10  0:00 Question about new allocator jes2358
@ 1997-02-11  0:00 ` Bill Brennan
  1997-02-12  0:00 ` Keith Allan Shillington
  1 sibling, 0 replies; 3+ messages in thread
From: Bill Brennan @ 1997-02-11  0:00 UTC (permalink / raw)



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







^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Question about new allocator
  1997-02-10  0:00 Question about new allocator jes2358
  1997-02-11  0:00 ` Bill Brennan
@ 1997-02-12  0:00 ` Keith Allan Shillington
  1 sibling, 0 replies; 3+ messages in thread
From: Keith Allan Shillington @ 1997-02-12  0:00 UTC (permalink / raw)



The convention in Ada is to raise the exception STORAGE_ERROR.  In RM95 it
is alluded to in the note at 4.8(14), and explained more fully in 13.11
(Storage Management).  You will find, in reading 13.11 that the implementor
has some choice in the matter.  In order to accomplish your objective, you
may want to implement a Storage_Pool that allows you to check space
available.

jes2358@imap3.asu.edu wrote in article
<Pine.SOL.3.91.970210095755.3813A-100000@general3.asu.edu>...
> 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.
> 




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1997-02-12  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-02-10  0:00 Question about new allocator jes2358
1997-02-11  0:00 ` Bill Brennan
1997-02-12  0:00 ` Keith Allan Shillington

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