From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,c6acbb9f2027b8c9 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.scarlet.biz!news.scarlet.biz.POSTED!not-for-mail NNTP-Posting-Date: Wed, 05 Oct 2005 14:44:55 -0500 From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: volatile vs aliased References: <1128525722.605730.281980@g43g2000cwa.googlegroups.com> <87mzlnomca.fsf@ludovic-brenta.org> <1128537566.929419.121660@g14g2000cwa.googlegroups.com> Date: Wed, 05 Oct 2005 21:46:14 +0200 Message-ID: <874q7voigp.fsf@ludovic-brenta.org> User-Agent: Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:ViGFyQJ3ZihKgP9RbigTOwR05Uc= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii NNTP-Posting-Host: 83.134.241.83 X-Trace: sv3-GX3b85WK8+8eGg/lTVTMcRY5Ke3iYTgon2sT4tATa9COLnYkMe4SSM7EzKjAlHScnc+ktzD+MC10xs5!ZcXbaJEIbZlSuuch92kSGV97rKjWsIi6tHSXspmalCtvZHhv+ltD87iwcM89oGQLwG+LTbtklw== X-Complaints-To: abuse@scarlet.be X-DMCA-Complaints-To: abuse@scarlet.biz X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news1.google.com comp.lang.ada:5421 Date: 2005-10-05T21:46:14+02:00 List-Id: "REH" writes: > VERY helpful! Thank you so much. > > I'm dealing with a situation where I have to do symbol table lookups > from the OS which return an address to procedure or function. This is > done in several places in the code. The reason is I have to support > different hardware dynamically. For example, if I am on board A, I > call Foo. The comparible subprogram on B is Bar. So, I cannot just > make pragma exports for them both as one will be undefined. Anyways, > to make it easier, I wanted to make a generic which just returned an > access type without having to do the pragma Import and for X'address in > every case. Then calling the subprogram is simple. My first pass is: > > generic > type Data_Type is private; > type Data_Ptr is access all Data_Type; > function Address_To_Access(Addr : System.Address) return Data_Ptr; > > function Address_To_Access(Addr : System.Address) return Data_Ptr is > begin > if Addr = System.Null_Address then > return null; > else > declare > Data: aliased Data_Type; > for Data'Address use Addr; > begin > return Data'Unchecked_Access; > end; > end if; > end Address_To_Access; > > > Two questions: > 1) Is this even a smart thing to do? Why do you have to use a generic? Why not just: procedure Call_Procedure (At_Address : in System.Address) is procedure Proc; for Proc'Address use At_Address; begin Proc; end Call_Procedure; The above effectively converts from an address to an access-to-procedure, and calls the procedure. If what you want is convert from an address to an access-to-data, you should probably use Ada.Address_To_Access_Conversions. > 2) How would I define a generic like the above for an subprogram with > an unknown signature? I don't know how you could call a procedure with unknown parameters. Or perhaps you mean with one known parameter which is the address of a record containing arbitrary parameters? generic type Parameter_Type is limited private; -- [1] procedure Call_Procedure (At_Address : in System.Address; Params : in Parameter_Type); procedure Call_Procedure (At_Address : in System.Address; Params : in Parameter_Type) is procedure Proc (Params : in System.Address); for Proc'Address use At_Address; begin Proc (Params'Address); end Call_Procedure; Is this close to what you want? [1] I think the type has to be declared limited, so that it is always passed by reference to Call_Procedure. This guarantees that the parameter has an address, and is not in a register. I have not compiled or tried this code, so please take it with due care. -- Ludovic Brenta.