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

Cookbook Chapter 12: High-Performance Language Bindings. More...

#include <infix/infix.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for Ch12_DirectMarshalling.c:

Classes

struct  Point
 A simple struct with two doubles, often used to test pass-by-value on registers. More...
 
struct  MockObject
 

Enumerations

enum  MockType { MOCK_INT , MOCK_POINT }
 

Functions

void move_point_ref (Point *p, int dx, int dy)
 
MockObjectNewMockInt (int v)
 
MockObjectNewMockPoint (double x, double y)
 
infix_direct_value_t marshal_mock_int (void *source_obj)
 Scalar Marshaller for Integer arguments. Extracts the integer value from a MockObject.
 
void marshal_mock_point (void *source_obj, void *dest_buffer, const infix_type *type)
 Aggregate Marshaller for Point struct arguments. Copies data from the MockObject into the temporary C buffer provided by the JIT.
 
void writeback_mock_point (void *source_obj, void *c_data_ptr, const infix_type *type)
 Write-back Handler for Point struct arguments. Called after the C function returns. Copies data from the C buffer back to the MockObject.
 
int main ()
 

Detailed Description

Cookbook Chapter 12: High-Performance Language Bindings.

This example demonstrates the "Direct Marshalling" (or "Bundle") API. This is an advanced feature for writing language bindings (like for Python, Lua, or Perl) where performance is critical.

Instead of unboxing your language's objects into temporary C variables and creating a void* args[] array for every call, you provide "Marshaller" functions. The infix JIT compiler calls these functions directly to fetch data from your objects just-in-time.

Scenario: We simulate a simple scripting language ("MockLang") and bind a C function move_point that takes a struct pointer and modifies it. This demonstrates:

  1. Marshalling a complex object (Struct) from "Script" to C.
  2. Marshalling primitive values (Int) from "Script" to C.
  3. "Write-back": Updating the "Script" object after the C function modifies the struct.

Enumeration Type Documentation

◆ MockType

enum MockType
Enumerator
MOCK_INT 
MOCK_POINT 

Function Documentation

◆ main()

int main ( void  )

◆ marshal_mock_int()

infix_direct_value_t marshal_mock_int ( void *  source_obj)

Scalar Marshaller for Integer arguments. Extracts the integer value from a MockObject.

◆ marshal_mock_point()

void marshal_mock_point ( void *  source_obj,
void *  dest_buffer,
const infix_type type 
)

Aggregate Marshaller for Point struct arguments. Copies data from the MockObject into the temporary C buffer provided by the JIT.

◆ move_point_ref()

void move_point_ref ( Point p,
int  dx,
int  dy 
)

◆ NewMockInt()

MockObject * NewMockInt ( int  v)

◆ NewMockPoint()

MockObject * NewMockPoint ( double  x,
double  y 
)

◆ writeback_mock_point()

void writeback_mock_point ( void *  source_obj,
void *  c_data_ptr,
const infix_type type 
)

Write-back Handler for Point struct arguments. Called after the C function returns. Copies data from the C buffer back to the MockObject.