From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Size of a pointer to a tagged record
Date: Wed, 4 Dec 2002 15:16:04 GMT
Date: 2002-12-04T15:16:04+00:00 [thread overview]
Message-ID: <wcc3cpd3jd7.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: asl1ri$l1t$1@pegasus.csx.cam.ac.uk
news01+Steven.Murdoch@cl.cam.ac.uk (Steven Murdoch) writes:
> I am writing a binding to the Lua (http://www.lua.org/)
> scripting language (which is written in C). One thing I
> need to do is store a pointer to a tagged record in a
> memory location allocated by malloc. For this reason I need
> to know the size.
>
> If a pointer to a tagged record is equivalent to a
> System.Address then I can store this in a memory location
> of sizeof(void *), however if the tag is stored in the
> pointer rather than the record then I would need to
> allocate space for the tag as well.
>
> If a pointer to a tagged record is equivalent to a
> System.Address then I would use the following code:
All the Ada compilers I am familiar with represent access-to-tagged as a
single address. The tag is stored in the tagged record, not in the
pointer.
I don't think it's even feasible to store the tag with the pointer,
because of a last-minute change to Ada 95 -- all tagged parameters are
aliased.
However, I don't understand what you're trying to do. How can C code
produce a pointer to a tagged type? I mean, the tag has to be
initialized on the Ada side. And why must the *pointer* be heap
allocated?
If the tag *were* stored in the pointer, then you would not only have to
allocate space for it, but somehow set it to the right value.
> --
>
> type Parameter is tagged private;
> type Parameter_Access is access all Parameter'Class;
>
> package Convert is new
> System.Address_To_Access_Conversions(Parameter);
>
> addr: System.Address;
> param: Parameter_Access;
>
> addr:=Get_Void_Pointer; --Void* from C
> param:=Parameter_Access(User_Data_Convert.To_Pointer(Address));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
That code is wrong, so I'm not sure what you mean.
I presume Get_Void_Pointer is pragma Import(C). And it does a "malloc",
passing sizeof(void *). So it's returning a pointer to a pointer. Is
that right?
Anyway, why not declare Get_Void_Pointer to return the access type?
I see no need for Address_To_Access_Conversions.
> At the end of this code I would use param as a normal pointer
> to a tagged record. Does this look OK? Is there a better way
> to achieve the same thing.
>
> I am using GNAT 3.13p but I would like the code to be as
> cross platform/compiler as possible.
You can find out the size of a pointer by writing a little test program
that prints out Parmeter_Access'Size, and running it on as many Ada
compilers as you can get your hands on.
- Bob
next prev parent reply other threads:[~2002-12-04 15:16 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-12-04 14:01 Size of a pointer to a tagged record Steven Murdoch
2002-12-04 15:16 ` Robert A Duff [this message]
2002-12-04 15:44 ` Steven Murdoch
2002-12-04 17:17 ` Robert A Duff
2002-12-04 17:56 ` Steven Murdoch
2002-12-04 19:10 ` Robert A Duff
2002-12-07 6:55 ` David Thompson
2002-12-04 20:27 ` Simon Wright
2002-12-04 18:11 ` tmoran
2002-12-04 17:05 ` Jeffrey Carter
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox