Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return a string allocated in C to Ada and free it in Ada?

Tags:

c

ada

This is the inverse of this question.

I have a string allocated in a C function (specifically by the cJSON library) that I want to return to an Ada caller as an out parameter, and free the string from Ada.

The C interface looks like this:

typedef struct
{
    int f1;
    int f2;
    // ...
} A;

int Dump_Record(const A& rec, char** outstr);

The Dump_Record function sets outstr to the value returned by cJSON_Print, which allocates a string via malloc.

How should the Ada binding be specified and how can I retrieve the output string and properly deallocate it in Ada?

like image 744
Patrick Avatar asked Jan 28 '20 16:01

Patrick


1 Answers

The following shows the Ada, C and GPR file for returning a char ** as a return type and as an out Ada parameter.

with Ada.Text_IO; use Ada.Text_IO;
with Interfaces.C.Strings; use Interfaces.C.Strings;

procedure Str is
   function Get_Str return chars_ptr with
     Import => True,
     Convention => C,
     External_Name => "get_str";

   procedure Get_Str (Str : in out chars_ptr) with
     Import => True,
     Convention => C,
     External_Name => "get_str2";

   Str  : chars_ptr := Get_Str;
   Str2 : chars_ptr := Null_Ptr;
begin
   Get_Str (Str2);

   Put ("==> " & Value (Str));
   Put ("==> " & Value (Str2));

   Free (Str);
   Free (Str2);
end Str;
#include <malloc.h>
#include <string.h>

const char *str  = "Hello, Ada\n";
const char *str2 = "This is another C string!\n";

// As a return type.
const char *get_str (void) {
    char *ptr = malloc (strlen (str) + 1);

    strcpy (ptr, str);

    return ptr;
}

// As a return / out parameter.
void get_str2 (char **ptr) {
    *ptr = malloc (strlen (str2) + 1);

    strcpy (*ptr, str2);
}
project Str is
   for Languages use ("C", "Ada");
   for Source_Dirs use (".");
   for Exec_Dir use ".";
   for Main use ("str.adb");
end Str;

Compile with gprbuild -P str.

So to implement your function in Ada:

package C renames Interfaces.C;

type A is
   record
      F1, F2 : C.int;
   end record with
     Convention => C;

   function Dump_Record (Rec : aliased A; Out_Str : out chars_ptr) return C.int with
     Import => True,
     Convention => C,
     External_Name => "Dump_Record";

Apparently, the Rec parameter needs to be aliased so the compiler knows it's a reference in C and doesn't re-allocate it somewhere else, i.e. secondary stack.

like image 175
Luke A. Guest Avatar answered Nov 08 '22 01:11

Luke A. Guest