From: prichtmyer@yahoo.com (Peter Richtmyer)
Subject: Re: Allocating Memory with "new"
Date: 25 Apr 2003 15:19:57 -0700
Date: 2003-04-25T22:19:58+00:00 [thread overview]
Message-ID: <1b585154.0304251419.5268b818@posting.google.com> (raw)
In-Reply-To: mailman.13.1051274855.13478.comp.lang.ada@ada.eu.org
Thanks, all the responses have been helpful, and I have tried to
incorporate
the suggestions.
So my new (still ugly) solution, for 1 to 256 bytes (thats enough for
me):
with system;
with System.Storage_Elements;
with system.address_to_access_conversions;
with Text_Io;
use text_io;
with unchecked_conversion;
procedure Hi is
subtype mem_bytes is natural range 1 .. 256;
type nat_8 is new natural range 0 .. 255;
for nat_8'size use 8;
type array_t is array (nat_8 range <>) of character;
type Mem_Block_T ( n_minus_1 : nat_8) is
record
t : array_t (1 .. n_minus_1);
end record;
for Mem_Block_T use
record
n_minus_1 at 0 range 0 .. 7;
end record;
pragma pack (Mem_Block_T);
package Mem_Block_Ptr_Pkg is new
system.address_to_access_conversions (Mem_Block_T);
Mem_Block_Ptr : Mem_Block_Ptr_Pkg.Object_Pointer := null;
--------------------------------------------------------------------
procedure alloc (nbytes : mem_bytes) is
n_minus_1 : nat_8 := nat_8 (nbytes - 1);
addr : system.address;
In_Work_Area : String (1 .. 12) := " ";
package data_io is new integer_io(integer);
begin
Mem_Block_Ptr := new Mem_Block_T (n_minus_1);
Addr := Mem_Block_Ptr_Pkg.to_address (Mem_Block_Ptr);
put ("Bytes allocated: " & Integer'image(Mem_Block_Ptr.all'size
/ 8));
Data_Io.Put (In_Work_Area, Integer(System.Storage_Elements.to_integer
(addr)), 16);
put_line (" at " & In_Work_Area);
end alloc;
--------------------------------------------------------------------
begin
for i in 1 .. 256 loop
Put ("len" & integer'image(i) & " : ");
alloc (i);
end loop;
end Hi;
============================
some output:
len 1 : Bytes allocated: 1 at 16#2602450#
len 2 : Bytes allocated: 2 at 16#2602520#
len 3 : Bytes allocated: 3 at 16#2602538#
len 4 : Bytes allocated: 4 at 16#2602550#
len 5 : Bytes allocated: 5 at 16#2602568#
. .
: :
len 252 : Bytes allocated: 252 at 16#260C140#
len 253 : Bytes allocated: 253 at 16#260C248#
len 254 : Bytes allocated: 254 at 16#260C350#
len 255 : Bytes allocated: 255 at 16#260C458#
len 256 : Bytes allocated: 256 at 16#260C560#
next prev parent reply other threads:[~2003-04-25 22:19 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-04-25 12:46 Allocating Memory with "new" David C. Hoos
2003-04-25 22:19 ` Peter Richtmyer [this message]
-- strict thread matches above, loose matches on Subject: below --
2003-04-24 19:30 Peter Richtmyer
2003-04-25 6:05 ` Vincent Smeets
2003-04-25 10:33 ` Peter Richtmyer
2003-04-25 15:09 ` Stephen Leake
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox