comp.lang.ada
 help / color / mirror / Atom feed
From: emery@goldfinger.mitre.org (David Emery)
Subject: Re: C-Ada Import of struct's -- Help
Date: 18 Oct 94 09:48:47
Date: 1994-10-18T09:48:47+00:00	[thread overview]
Message-ID: <EMERY.94Oct18094847@goldfinger.mitre.org> (raw)
In-Reply-To: mmcnett@sparc53.cs.uiuc.edu's message of 18 Oct 94 03:34:16 GMT

Given
  struct queue_noncyc_s {
    char *debugname;	      /* name for debugging purposes */
    int length;		      /* length (max items+1) of queue */
    int first, last;	      /* begin and end of queue */
    Qitem *entries;	      /* array[length] of entries */
  };

I come up with the following Ada data structure
  with System;
  package C_Types is
    type char_star is new System.address;
    type c_int is new integer;
  end C_Types;

  type queue_noncyc_s is record
    debugname : C_Types.char_star;
    length : C_Types.c_int;
    first, last  : C_Types.c_int;
    entries : System.address;  -- handle specially
  end record;
(There are 'standard tricks' for handling char * values.  See my paper
 in Tri-Ada '90.)

Now one problem is determining -how- the record is passed to C.  Some
C compilers support passing structs (required by ANSI C, I believe):
	int pass_struct (struct rec bar);
others do not, and you pass the address of the struct (Original K&R C):
	int pass_addr (struct rec *bar);
The latter case is more portable, and easier to represent.  

So, given our Ada type that we want to pass to C...
	q_rec : queue_noncyc_s;

To pass it to pass_addr() we have:
	function pass_addr (addr : system.address) return c_types.int;
	...
	pass_addr (q_rec'address);	-- pretty straightforward

A problem occurs with pass_struct().  I've seen cases where the C
compiler passes the structure, and other cases where it passes the
address of the structure.  And I've seen Ada compilers that did not
support the C compiler's choice.  

You'll either have to determine how your compiler does this, or,
better, write some wrapper code in C to reduce the problem to the
'known case'.  Write this C code, then call it from Ada.

	int pass_addr (bar)
		struct rec *bar;
	{	
		return pass_struct (*bar);	
		/*  calls original routine */
	};	

The Ada call for this is the as shown above.  

				dave
--
--The preceeding opinions do not necessarily reflect the opinions of
--The MITRE Corporation or its sponsors. 
-- "A good plan violently executed -NOW- is better than a perfect plan
--  next week"                                      George Patton
-- "Any damn fool can write a plan.  It's the execution that gets you
--  all screwed up"                              James Hollingsworth
-------------------------------------------------------------------------



  reply	other threads:[~1994-10-18  9:48 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-10-18  3:34 C-Ada Import of struct's -- Help mcnett michael david
1994-10-18  9:48 ` David Emery [this message]
  -- strict thread matches above, loose matches on Subject: below --
1994-10-18 10:20 Bob Wells #402
1994-10-18  9:58 ` David Emery
1994-10-18 19:11   ` Robert Dewar
1994-10-19 10:02     ` David Emery
1994-10-20  0:36     ` Keith Thompson @pulsar
1994-10-19 13:57 Bob Wells #402
replies disabled

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