Re: Calling Ada from C



hannibal.holm@xxxxxxxxx wrote:

The data structure in C is just a pointer to a block of data to be
sent over a network link. This need to be converted to a byte array
defined as:

Byte_Size : constant := 8;

type Byte is range 0 .. 2**Byte_Size - 1;
for Byte'Size use Byte_Size;

Anything you use to talk to C should be convention C. Since, from below, in C this seems to be char, you should use Interfaces.C.Char and Interfaces.C.Char_Array. Since Char_Array is convention C, Ada won't expect it to have bounds associated with it.


type Byte_Array is array (Natural range <>) of Byte;
pragma Pack (Byte_Array);

As said, I am not extremly familiar with Ada and come from a C-
background, so I have a few problems trying to figure out what the ada
runtime and type system actually does. I tried something like

-- This proc is exported as with C-conventions
procedure Some_Ada_Proc(Msg : in Byte_Array;
Len : in Natural) is
Msg_Constr : Byte_Array := Msg(0 .. Length - 1);

Again, you should use Interfaces.C.Int instead of Natural.

Where does Length come from?

This is called with:

char foo[] = {0xde, 0xad, 0xbe, 0xef};
some_ada_proc(foo, sizeof(foo)); // sizeof static array = 4

This is where I get char from above.

If using Char_Array still crashes, you can use a very large constrained subtype of Char_Array:

subtype Many_Chars is Interfaces.C.Char_Array (Interfaces.C.Size_T);

and slice the result with 0 .. Interfaces.C.Size_T (Len). This can then be converted into your array of Bytes.

--
Jeff Carter
"I would never want to belong to any club that
would have someone like me for a member."
Annie Hall
41
.



Relevant Pages

  • Re: Possible compiler bug with this simple program
    ... I suspect that one problem is using the C convention to pass a parameter that is of an unconstrained array type, ... B.3, the C convention passes only a single pointer to the first element of the array, so the 'Range attribute will not be available to the subprogram. ... The compiler should IMHO have rejected the use of x'range here, ... In fact, when an Ada subprogram has an unconstrained array parameter with Convention C, it seems to me that the subprogram's body cannot make any use of individual elements of the array, because it doesn't know the index range, so the compiler should reject any indexing of such an array parameter, as well as any attempt to pass it on as a Convention Ada parameter. ...
    (comp.lang.ada)
  • Re: 10 rules for benchmarking (was Re: Teaching new tricks to an old dog (C++ -->Ada))
    ... so I did the same in Ada, results are in the T and TC columns. ... array are used, ... I take it you mean the one best performing assembly language ... that a given assembly language program cannot be beaten by ...
    (comp.programming)
  • Re: 10 rules for benchmarking (was Re: Teaching new tricks to an old dog (C++ -->Ada))
    ... so I did the same in Ada, results are in the T and TC columns. ... array are used, ... I take it you mean the one best performing assembly language ... that a given assembly language program cannot be beaten by ...
    (comp.lang.ada)
  • Re: Surprise in array concatenation
    ... > Dmitry A. Kazakov wrote: ... >> Better ADT is what Ada needs. ... The concept of array must be consistent ... for I in A'First..A'Last loop ...
    (comp.lang.ada)
  • Re: Surprise in array concatenation
    ... >> computational states for which A is considered be 1. ... >> memory dump and discover a bit pattern 000000001 at the address FF07712CA0 ... could you stop naming this ADT an array? ... > These are Ada arrays, rock solid low level stuff, based on preexisting ...
    (comp.lang.ada)