comp.lang.ada
 help / color / mirror / Atom feed
From: rgilbert@orl.mmc.com (Bob Gilbert)
Subject: Re: Interfacing Ada to Unix/execl var. arg
Date: 20 Oct 1994 12:42:25 GMT
Date: 1994-10-20T12:42:25+00:00	[thread overview]
Message-ID: <385ojh$2p8@theopolis.orl.mmc.com> (raw)
In-Reply-To: 3823s3$ioq@goanna.cs.rmit.oz.au

In article ioq@goanna.cs.rmit.oz.au, Dale Stanbrough <dale@goanna.cs.rmit.edu.au> () writes:
->
->The main problem lies in here - how do you assemble the various
->arguments into a call to a pragma import'ed C routine?
->
->The only solution appears to be to direct pass parameters via 
->aggregates (as your solution suggests) and then call an equivalent
->non-variable argument list function (e.g. execl - execv).
->
->The code below works, but I am unsure of whether it is memory safe,
->and whether all memory gets reclaimed at the end of the function.
->
->
->function execv( path    :string;
->                argv    :string_array := null_arg_list) return integer is
->
->        C_path  :constant interfaces.c.char_array(1..path'length+1)
->                                := interfaces.c.to_C(path);
->        type address_array is array(1..argv'length + 1) of system.address;
->
->        C_argv  :address_array;
->        index   :integer;
->
->        ------------------------------------------------------------
->        function C_execv(       path    :system.address;
->                                C_arg_list:system.address)
->                                        return interfaces.c.int;
->        pragma import(C, C_execv, "execv");
->        ------------------------------------------------------------
->begin
->        -- set up the array of pointers to the strings
->        index := 0;
->        for i in argv'range loop
->                index := index + 1;
->                -- can Ada release the memory pointed returned by 
->                -- interfaces.c.to_c after the loop (leaving the pointer
->                -- pointing at nothing), or will it wait until the end of
->                -- the function, or will it never free up the memory?
->
->                C_argv(index) := interfaces.c.to_c(argv(i).all)(0)'address;
->        end loop;
->
->        -- append C style null to the end of the array of addresses
->        -- (presumes that system.null_address = C style null).
->
->        C_argv(C_argv'last) := system.null_address;
->
->        -- pass the address of the first element of each
->        -- parameter, as C expects.
->
->        return integer(C_execv( C_path(1)'address,
->                                C_argv(1)'address));
->end execv;
->
->
->Thanks,
->
->Dale

Does interfaces.c.to_c cause memory to be allocated?

If not, then there is no problem since the array C_argv is created
on the calling stack and released upon exit from the function execv.

-Bob





  reply	other threads:[~1994-10-20 12:42 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-10-13  7:44 Interfacing Ada to Unix/execl var. arg. list function? Dale Stanbrough
1994-10-13  9:21 ` David Emery
1994-10-13 12:22 ` Robert Dewar
1994-10-13 17:37   ` Mark A Biggar
1994-10-13 18:46     ` Robert Dewar
1994-10-14 13:55   ` Norman H. Cohen
1994-10-13 13:31 ` Nicolas Courtel
1994-10-13 19:28 ` Tucker Taft
1994-10-14 13:55 ` Interfacing Ada to Unix/execl var ncohen
1994-10-17 18:18 ` Interfacing Ada to Unix/execl var. arg. li Bob Gilbert
1994-10-19  3:30   ` Interfacing Ada to Unix/execl var. arg. list function? Dale Stanbrough
1994-10-20 12:42     ` Bob Gilbert [this message]
1994-10-20  4:18   ` Interfacing Ada to Unix/execl var. arg. li S M Ryan
replies disabled

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