comp.lang.ada
 help / color / mirror / Atom feed
From: news01+Steven.Murdoch@cl.cam.ac.uk (Steven Murdoch)
Subject: Re: Size of a pointer to a tagged record
Date: 4 Dec 2002 15:44:03 GMT
Date: 2002-12-04T15:44:03+00:00	[thread overview]
Message-ID: <asl7s3$qjb$1@pegasus.csx.cam.ac.uk> (raw)
In-Reply-To: wcc3cpd3jd7.fsf@shell01.TheWorld.com

Thanks for your reply.

In article <wcc3cpd3jd7.fsf@shell01.TheWorld.com>,
 you write:
>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.

OK, that is what I hoped.

>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?

The tagged record and pointer are created by a Ada "new" statement
the the pointer is passed to the Lua API and stored by it.
In order to store things in Lua you give it the contents of the object
and size (in bytes). This is effectively passed to malloc and the space
for the pointer is allocated (plus some Lua internals).

Control then passes to Lua and at some point it will ask the Ada
program to perform some action on the data so will pass the data
back to Ada, which is the same as the contents passed in
earlier, i.e. a pointer to a tagged record. 

>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.

Ada would handle setting the tag, the C part just takes a value 
and stores it, it eventually returns it.

>
>> --
>> 
>> 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.

Well it compiles.

>I presume Get_Void_Pointer is pragma Import(C).  And it does a "malloc",
>passing sizeof(void *).

Get_Void_Pointer doesn't do a malloc, it returns a pointer
to a tagged record, which was stored somewhere in the heap.

At a previous point a malloc was done to create a memory location X,
in which was stored the address of a tagged record. The address of
X is hidden by Lua, but it's contents are passed back to Ada by
Get_Void_Pointer.

> So it's returning a pointer to a pointer.  Is
> that right?

No, just a pointer to a tagged record.

>Anyway, why not declare Get_Void_Pointer to return the access type?
>I see no need for Address_To_Access_Conversions.

What would it's type in C be in that case? Currently is returns
a "void *" which I believe is equivalent to a System.Address. What
would it's type be if it it returned an "Parameter_Access"?
Is it safe to mark a pragma Import(C) function as returning a
Parameter_Access? Would this be a better idea?

>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.

On GNAT 3.13p it is 32 (I assume bytes) which is indeed the same
size as System.Address.

Thank you,
Steven Murdoch.



  reply	other threads:[~2002-12-04 15:44 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
2002-12-04 15:44   ` Steven Murdoch [this message]
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