comp.lang.ada
 help / color / mirror / Atom feed
* Creating tagged records with C "malloc" rather than Ada "new"
@ 2002-12-07  0:58 Steven Murdoch
  2002-12-07 14:32 ` Robert A Duff
  0 siblings, 1 reply; 2+ messages in thread
From: Steven Murdoch @ 2002-12-07  0:58 UTC (permalink / raw)


This post is again about my binding from Ada95 to Lua. In the last
thread I proposed and asked for help for a way of giving Lua access
to tagged records by asking Lua to assign enough space for a pointer
to a tagged record, then storing the pointer in the memory location
provided by Lua. This works (thanks to all those who helped) but I
have another possibility and would greatly appreciate any advice on
it.

Lua provide a garbage collection facility, which I would like to use
(along with some other neat features), however I can only do this
if I get Lua to allocate the memory for the object. Currently I
get Lua to allocate space for the access type, but I allocate space
for the tagged record using new. In order to reclaim the space
allocated by new I would need to add a hook to the Lua garbage
collector to deallocate this space when the pointer is freed.

However I think I can simplify this by allocating space for the
tagged record in Lua, and hence it will deallocate this space at
the right time. I would like to know if this is possible to do this
reliably, and if it is relativly safe,

The code I am proposing would look something like as follows:

C:
/* Actually part of Lua, but this is equivalent */
void *allocate(int size) {
        return malloc(size);
}

Ada:
type Parameter is tagged private;
type Parameter_Access is access Parameter'Class;

function Allocate(Size: Storage_Unit) return Parameter_Access;
pragma Import(C, Allocate);

function Add_To_Lua(Param: in Parameter) return Parameter_Access
is
	Param_Ptr: Parameter_Access;
begin
        Param_Ptr:=Allocate(Parameter'Max_Size_In_Storage_Units);
        Param_Ptr.all:=Param; --[1]
        return Param_Ptr;
end;

Will this do what I want and is is comapritively safe. In particular
will statement [1] sort of the tag of Param properly? Are there any
other potential problems.

Thank you in advance,
Steven Murdoch.



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

* Re: Creating tagged records with C "malloc" rather than Ada "new"
  2002-12-07  0:58 Creating tagged records with C "malloc" rather than Ada "new" Steven Murdoch
@ 2002-12-07 14:32 ` Robert A Duff
  0 siblings, 0 replies; 2+ messages in thread
From: Robert A Duff @ 2002-12-07 14:32 UTC (permalink / raw)


news01+Steven.Murdoch@cl.cam.ac.uk (Steven Murdoch) writes:

> However I think I can simplify this by allocating space for the
> tagged record in Lua, and hence it will deallocate this space at
> the right time. I would like to know if this is possible to do this
> reliably, and if it is relativly safe,

You should look at RM-13.11, "Storage Management".  You can declare your
own storage pool type.  The Allocate operation could call Lua.
The Deallocate operation could do nothing (which might be appropriate if
you're doing garbage collection).

But there are lots of other issues related to GC.  I don't know anything
about Lua, but I don't see how Lua can know where all the pointers are.
So does it do so-called "conservative" GC?

> The code I am proposing would look something like as follows:
> 
> C:
> /* Actually part of Lua, but this is equivalent */
> void *allocate(int size) {
>         return malloc(size);
> }
> 
> Ada:
> type Parameter is tagged private;
> type Parameter_Access is access Parameter'Class;
> 
> function Allocate(Size: Storage_Unit) return Parameter_Access;
> pragma Import(C, Allocate);
> 
> function Add_To_Lua(Param: in Parameter) return Parameter_Access
> is
> 	Param_Ptr: Parameter_Access;
> begin
>         Param_Ptr:=Allocate(Parameter'Max_Size_In_Storage_Units);
>         Param_Ptr.all:=Param; --[1]
>         return Param_Ptr;
> end;
> 
> Will this do what I want and is is comapritively safe. In particular
> will statement [1] sort of the tag of Param properly? Are there any
> other potential problems.

You might be able to make the above scheme work for *some* types, but it
has some problems.  Parameter'Max_Size_In_Storage_Units returns the
maximum size for all objects of type Parameter (not Parameter'Class).
What you want is the size of Param itself.
Parameter'Max_Size_In_Storage_Units could be (way) too big if the type
has discriminant dependent arrays.  And
Parameter'Max_Size_In_Storage_Units will be too small if Param'Tag /=
Parameter'Tag (i.e. the caller passed in an object from deeper in the
type hierarchy).

What you want is Param'Size (but that's in bits).

It won't work for limited types (because you have an assignment).

There's no way to create an uninitialized heap object.

The storage pool scheme solves all of these problems.
You just do a normal Ada "new", with or without initialization,
as you wish, perhaps with discriminant constraint and whatever
other bells and whistles Ada allows.  The compiler will translate the
"new" into two steps: a call on your Allocate (which calls Lua),
followed by all the Ada-specific default or explicit initializations.

- Bob



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

end of thread, other threads:[~2002-12-07 14:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-07  0:58 Creating tagged records with C "malloc" rather than Ada "new" Steven Murdoch
2002-12-07 14:32 ` Robert A Duff

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