AtlatestRepositorysigil-ffi
1
/* Tiny shared library for FFI dlopen smoke tests.2
*3
* Compiled during test with:4
* cc -shared -fPIC -o libffi-test.so ffi-test-lib.c5
*/7
#include <string.h>8
#include <stdlib.h>10
/* Basic arithmetic — tests int and double marshaling */11
int add_ints(int a, int b)12
{13
return a + b;14
}16
double multiply_doubles(double a, double b)17
{18
return a * b;19
}21
/* Mixed types — tests cross-type signatures */22
double int_to_double(int x)23
{24
return (double)x * 2.5;25
}27
int double_to_int(double x)28
{29
return (int)x;30
}32
/* String handling — tests string marshaling across dlopen */33
int string_length(const char *s)34
{35
return (int)strlen(s);36
}38
/* Returns a static string — tests string return */39
const char *greeting(void)40
{41
return "hello from ffi-test-lib";42
}44
/* Pointer operations — tests pointer passing */45
void fill_buffer(int *buf, int count, int value)46
{47
for (int i = 0; i < count; i++) {48
buf[i] = value + i;49
}50
}52
int sum_buffer(const int *buf, int count)53
{54
int total = 0;55
for (int i = 0; i < count; i++) {56
total += buf[i];57
}58
return total;59
}61
/* Struct-by-value — tests dyncall struct passing */62
typedef struct {63
double x;64
double y;65
} Point;67
Point make_point(double x, double y)68
{69
Point p = { x, y };70
return p;71
}73
double point_distance(Point a, Point b)74
{75
double dx = b.x - a.x;76
double dy = b.y - a.y;77
return dx * dx + dy * dy; /* squared distance, avoids needing sqrt */78
}80
/* Callback test — calls a function pointer */81
typedef int (*IntBinaryFn)(int, int);83
int apply_fn(IntBinaryFn fn, int a, int b)84
{85
return fn(a, b);86
}