comp.lang.ada
 help / color / mirror / Atom feed
From: James Rogers <jimmaureenrogers@worldnet.att.net>
Subject: Re: Ada with Protected Object & Importing C program in Linux
Date: Fri, 14 Sep 2001 01:33:05 GMT
Date: 2001-09-14T01:33:05+00:00	[thread overview]
Message-ID: <3BA15E67.D41D1D84@worldnet.att.net> (raw)
In-Reply-To: 86772402.0109131712.7583f07f@posting.google.com

Lin wrote:
> 
> I've a problem of binding an Ada program(with protected objects) with
> C programs in Linux. Any clue is helpful to me. Thanks.
> 
> The working environment is Slackware Linux,Gnat3.13p
> My problem: In Linux, the program with protected object can work fine
> without importing a C program; the program will raise "Segmentation
> fault when it  imports a C program. The same program works fine in
> WinNT. The code is shown below, which is to asks the protected object
> for an new request id.
> 
> ---------------------
> ---Ada programs--
> ---------------------
> with my_test;
> with Ada.Text_IO;use Ada.Tex_IO;
> procedure my_main is
>    my_id : my_test.Request_Id_Type := 0;
> begin
>    Put_Line ("Hello World!!");
>    for i in 1 .. 10 loop
>      my_test.Allocate_New_Id (my_id);
>      my_test.Calling_C;
>      Put_Line (
>       "Request id=" & my_test.Request_Id_Type'Image (my_id));
>    end loop;
> end my_main;
> ------------
> package my_test is
>    type Request_Id_Type is mod 2 ** 16;
>    procedure Allocate_New_Id (Id : out Request_Id_Type);
>    procedure Calling_C;
>    pragma Import (C, Calling_C);
>    pragma Import_Procedure (Calling_C, "Calling_C");
> end my_test;
> ------------
> package body my_test is
>    type Is_Used_Array is array (Request_Id_Type) of Boolean;
>    protected type Request_Id_Server_Type is
>       entry Get (Id : out Request_Id_Type);
>       procedure Free (Id : in Request_Id_Type);
>    private
>       Latest : Request_Id_Type := Request_Id_Type'First;
>       Is_Used : Is_Used_Array := (others => False);
>       Count       : Request_Id_Type := 0;
>    end Request_Id_Server_Type;
> 
>    protected body Request_Id_Server_Type
>    is
>       procedure Free (Id : in Request_Id_Type)
>       is
>       begin
>          if Is_Used (Id) then
>             Is_Used (Id) := False;
>             Count := Count - 1;
>          end if;
>       end Free;
> 
>       entry Get (Id : out Request_Id_Type)
>          when Count < Request_Id_Type'Last is
>       begin
>          while Is_Used (Latest)
>          loop
>             Latest := Latest + 1;
>          end loop;
>          Id := Latest;
>          Is_Used (Id) := True;
>          Count := Count + 1;
>          Latest := Latest + 1;
>       end Get;
>    end Request_Id_Server_Type;
>    type Access_Request_Id_Server_Type is access
> Request_Id_Server_Type;
>    Request_Id_Server : Access_Request_Id_Server_Type :=
>                                             new
> Request_Id_Server_Type;
>    procedure Allocate_New_Id (Id : out Request_Id_Type)
>    is
>    begin
>       Request_Id_Server.Get (Id);
>    end Allocate_New_Id;
> end my_test;
> ---------------------
> ---the C program---
> ---------------------
> #include <stdio.h>
>  Calling_C(){
>    printf ("\nC is Called now");
>  }
> ----------------------

This C function does not match the signature of the procedure
"Calling_C". The function Calling_C does not return void. A C
program will return "int" unless you specify otherwise. Either
the C function or the Ada procedure must be modified so that they
both agree.

Jim Rogers
Colorado Springs, Colorado USA



  reply	other threads:[~2001-09-14  1:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-09-14  1:12 Ada with Protected Object & Importing C program in Linux Lin
2001-09-14  1:33 ` James Rogers [this message]
2001-09-15 14:50   ` Lin
2001-09-15 17:14   ` Lin
replies disabled

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