Ticket #4221: HsFunction.c

File HsFunction.c, 552 bytes (added by ravi_n, 5 years ago)
Line 
1#include<stdio.h>
2#include<stdlib.h>
3
4typedef double (*hs_function_ptr)(double);
5
6typedef struct {
7  hs_function_ptr fn;
8  void (*free_fn)(hs_function_ptr);
9} fn_blob;
10
11fn_blob* create_fn_blob(hs_function_ptr fn, void (*free_fn)(hs_function_ptr)) {
12  fn_blob* new_blob = malloc(sizeof(fn_blob));
13  new_blob->fn = fn;
14  new_blob->free_fn = free_fn;
15  return new_blob;
16}
17
18double call_fn_blob(fn_blob* fn_blob, double arg) {
19  return(fn_blob->fn(arg));
20}
21
22void free_fn_blob(fn_blob* dead_blob) {
23  dead_blob->free_fn(dead_blob->fn);
24  free(dead_blob);
25}
26