comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@on2.com (Matthew Heaney)
Subject: Re: Elegant 'realloc'?
Date: 31 Jul 2003 07:42:27 -0700
Date: 2003-07-31T14:42:28+00:00	[thread overview]
Message-ID: <1ec946d1.0307310642.74d93921@posting.google.com> (raw)
In-Reply-To: slrnbihq0e.2j7.lutz@taranis.iks-jena.de

Lutz Donnerhacke <lutz@iks-jena.de> wrote in message news:<slrnbihq0e.2j7.lutz@taranis.iks-jena.de>...
> When dealing with dynamically allocated variable length arrays, the
> allocated space might change. Is there a common idiom to simulate an
> 'realloc' (especially shrinking) other than, allocate, copy, free?
> 
>    declare
>       procedure Free is new Unchecked_Deallocation(T_Array, T_Array_Access);
>       oldp : constant T_Array_Access := current.field;
>    begin
>       current.field := new T_Array'(oldp(oldp'First .. current.last));
>       Free(oldp);
>    end;

I don't know how to implement a realloc in Ada, unless you write your own
storage pool.

You can try using the vector container in Charles, which hides all the
allocate, copy, and free calls.

The length of the vector refers to the number of active elements in the
vector.  The size of the vector refers to the length of the underlying
array, which is at least the number of elements.

To increase the size of a vector, you can just call Resize.

To decrease the size of a vector, you use the swap trick:

procedure Decrease_Size (V : in out Vector_Subtype) is
   V2 : Vector_Subtype := V;
begin
   Swap (V, V2);
end;

Charles makes a guarantee that the new vector V2 has the minimum size
necessary to contain all the elements in source vector V.  (Presumably the
size of V is much larger than its length, which is why we want to shrink
it.)

When you swap, the new vector V2 gets the (large) internal array of V, and V
gets the (small) internal array of V2.  V2 is immediately finalized, which
free's the internal array (and which originally belonged to V).

When you Clear a vector, like this:

Clear (V);

all this does is set the count of the number of active elements (the length)
to 0.  It leaves the internal array untouched, which means the size doesn't
change.

If you want to remove all the elements in a vector, and set the size of the
vector to 0, then you can use the swap trick:

procedure Clear_And_Decrease_Size (V : in out Vector_Subtype) is
   V2 : Vector_Subtype;  --Length and Size are both 0
begin
   Swap (V, V2);
end;

Charles makes a guarantee that there is no cost for simply declaring a
vector, in the sense that no internal memory is allocated during elaboration
of the vector object.

The Charles library is available from my home page.

http://home.earthlink.net/~matthewjheaney/charles/

Look for a new release of Charles early next week.



  reply	other threads:[~2003-07-31 14:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-07-31  9:57 Elegant 'realloc'? Lutz Donnerhacke
2003-07-31 14:42 ` Matthew Heaney [this message]
2003-07-31 14:59   ` Lutz Donnerhacke
2003-07-31 16:50     ` Matthew Heaney
2003-07-31 16:35 ` Nick Roberts
2003-08-01  0:01 ` Robert I. Eachus
replies disabled

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