ffi.h
1 // ffi.h 2 // C interface for Haskell FFI 3 // 4 // This header defines the C ABI that Haskell's FFI can call. 5 // The implementation is in C++ (ffi.cpp) but uses extern "C" linkage. 6 7 #ifndef ALEPH_FFI_H 8 #define ALEPH_FFI_H 9 10 #include <stddef.h> 11 #include <stdint.h> 12 13 #ifdef __cplusplus 14 extern "C" { 15 #endif 16 17 // ============================================================================= 18 // Simple arithmetic (demonstrates basic FFI) 19 // ============================================================================= 20 21 int32_t ffi_add(int32_t a, int32_t b); 22 int32_t ffi_multiply(int32_t a, int32_t b); 23 24 // ============================================================================= 25 // Vector operations (demonstrates pointer passing) 26 // ============================================================================= 27 28 // Compute dot product of two vectors 29 // Returns the result, vectors are not modified 30 double ffi_dot_product(const double* a, const double* b, size_t len); 31 32 // Compute L2 norm of a vector 33 double ffi_norm(const double* v, size_t len); 34 35 // Scale a vector in-place: v[i] *= scalar 36 void ffi_scale(double* v, size_t len, double scalar); 37 38 // ============================================================================= 39 // String operations (demonstrates string passing) 40 // ============================================================================= 41 42 // Returns a greeting string. Caller must free with ffi_free_string. 43 char* ffi_greet(const char* name); 44 45 // Free a string returned by FFI functions 46 void ffi_free_string(char* str); 47 48 // ============================================================================= 49 // Opaque handle pattern (demonstrates resource management) 50 // ============================================================================= 51 52 // Opaque handle to a Counter object 53 typedef struct Counter Counter; 54 55 // Create a new counter with initial value 56 Counter* ffi_counter_new(int32_t initial); 57 58 // Destroy a counter 59 void ffi_counter_free(Counter* counter); 60 61 // Get current value 62 int32_t ffi_counter_get(const Counter* counter); 63 64 // Increment and return new value 65 int32_t ffi_counter_increment(Counter* counter); 66 67 // Add n and return new value 68 int32_t ffi_counter_add(Counter* counter, int32_t n); 69 70 #ifdef __cplusplus 71 } 72 #endif 73 74 #endif // ALEPH_FFI_H