infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
202_in_structs.c File Reference

Unit test for passing structs that contain pointer members. More...

#include "common/double_tap.h"
#include "types.h"
#include <infix/infix.h>
#include <string.h>
Include dependency graph for 202_in_structs.c:

Macros

#define DBLTAP_IMPLEMENTATION
 

Functions

int process_pointer_struct (PointerStruct ps)
 A C function that takes a struct containing pointers by value.
 
 if (!ok(infix_type_create_struct(arena, &struct_type, members, 2)==INFIX_SUCCESS, "Type created"))
 
 ok (infix_forward_create_unbound_manual(&unbound_t, return_type, &struct_type, 1, 1)==INFIX_SUCCESS, "Unbound created")
 
 unbound_cif ((void *) process_pointer_struct, &unbound_result, args)
 
 ok (unbound_result==550, "Unbound call correct")
 
 ok (infix_forward_create_manual(&bound_t, return_type, &struct_type, 1, 1,(void *) process_pointer_struct)==INFIX_SUCCESS, "Bound created")
 
 ok (bound_result==550, "Bound call correct")
 
 infix_forward_destroy (unbound_t)
 
 infix_forward_destroy (bound_t)
 
 infix_arena_destroy (arena)
 

Variables

 TEST
 
infix_arena_tarena = infix_arena_create(4096)
 
infix_struct_membermembers
 
infix_typestruct_type = nullptr
 
infix_typereturn_type = infix_type_create_primitive(INFIX_PRIMITIVE_SINT32)
 
int value_to_point_to = 500
 
const char * string_to_point_to = "Hello Pointers"
 
PointerStruct struct_instance = {&value_to_point_to, string_to_point_to}
 
void * args [] = {&struct_instance}
 
infix_forward_tunbound_t = nullptr
 
int unbound_result = 0
 
infix_unbound_cif_func unbound_cif = infix_forward_get_unbound_code(unbound_t)
 
infix_forward_tbound_t = nullptr
 
int bound_result = 0
 
infix_cif_func bound_cif = infix_forward_get_code(bound_t)
 

Detailed Description

Unit test for passing structs that contain pointer members.

This test case is an important follow-up to 201_basic.c. It verifies that infix can correctly handle structs that are passed by value, but which themselves contain pointer members.

The ABI classifier must correctly identify the struct as an aggregate to be passed according to the platform's rules (e.g., in registers or on the stack). The JIT code must then correctly copy the entire struct, including the pointer values within it.

The test defines a PointerStruct containing an int* and a const char*. It calls a C function that takes this struct by value, dereferences the pointers inside it, and verifies their contents. This confirms that the pointer values were correctly preserved through the FFI call.

Macro Definition Documentation

◆ DBLTAP_IMPLEMENTATION

#define DBLTAP_IMPLEMENTATION

Function Documentation

◆ if()

if ( okinfix_type_create_struct(arena, &struct_type, members, 2)==INFIX_SUCCESS, "Type created")

◆ infix_arena_destroy()

infix_arena_destroy ( arena  )

◆ infix_forward_destroy() [1/2]

infix_forward_destroy ( bound_t  )

◆ infix_forward_destroy() [2/2]

infix_forward_destroy ( unbound_t  )

◆ ok() [1/4]

ok ( bound_result  = =550,
"Bound call correct"   
)

◆ ok() [2/4]

ok ( infix_forward_create_manual(&,, &, 1, 1, *void  process_pointer_struct)

◆ ok() [3/4]

ok ( infix_forward_create_unbound_manual &,, &, 1,  1struct_type = =INFIX_SUCCESS,
"Unbound created"   
)

◆ ok() [4/4]

ok ( unbound_result  = =550,
"Unbound call correct"   
)

◆ process_pointer_struct()

int process_pointer_struct ( PointerStruct  ps)

A C function that takes a struct containing pointers by value.

The ABI will pass this struct according to its size and alignment. The function then dereferences the pointers to verify they were passed correctly.

◆ unbound_cif()

unbound_cif ( (void *)  process_pointer_struct,
unbound_result,
args   
)

Variable Documentation

◆ arena

◆ args

void* args[] = {&struct_instance}

◆ bound_cif

◆ bound_result

bound_cif & bound_result = 0

◆ bound_t

◆ members

members[1]
Initial value:
=
infix_arena_t * arena
Definition 202_in_structs.c:47
c23_nodiscard void * infix_arena_alloc(infix_arena_t *, size_t, size_t)
Allocates a block of memory from an arena.
Definition arena.c:126
Describes a single member of a C struct or union.
Definition infix.h:277

◆ return_type

◆ string_to_point_to

const char* string_to_point_to = "Hello Pointers"

◆ struct_instance

◆ struct_type

infix_type* struct_type = nullptr

◆ TEST

TEST
Initial value:
{
plan(5)
#define plan(count)
Definition double_tap.h:163

◆ unbound_cif

◆ unbound_result

int unbound_result = 0

◆ unbound_t

infix_forward_t* unbound_t = nullptr

◆ value_to_point_to

int value_to_point_to = 500