File: ForeignTest.c

package info (click to toggle)
polyml 5.8.1-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 57,736 kB
  • sloc: cpp: 44,918; ansic: 26,921; asm: 13,495; sh: 4,670; makefile: 610; exp: 525; python: 253; awk: 91
file content (119 lines) | stat: -rw-r--r-- 2,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Example code for a C-library accessible from ML
using the CInterface structure.

Copyright David C.J. Matthews 1999, 2009, 2015

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
/*
Linux: cc -shared -o Foreign ForeignTest.c
Windows: cl /MT ForeignTest.c /link /dll /out:Foreign.dll /def:Foreign.def
Mac OS X : cc -dynamiclib -o Foreign.dylib ForeignTest.c
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

/* Return a string duplicated n Times. */
char *DupNString(int n, char *str)
{
    int nSize = strlen(str);
    char *res = malloc(n*nSize + 1);
    int i;
    *res = 0;
    for (i = 0; i < n; i++) strcat(res, str);
    return res;
}
typedef struct _tree {
    struct _tree *left, *right;
    int	nValue;
} *tree;

int SumTree(tree t)
{
    if (t == NULL) return 0;
    else return t->nValue + SumTree(t->left) + SumTree(t->right);
}

/* Tests to see whether small structures are returned specially. */
struct r2 { int n, m; };

struct r2 ReturnR2(int n, int m)
{
    struct r2 ret;
    ret.n = n + 1;
    ret.m = m + 1;
    return ret;
}

/* Added.  Callback function. */

typedef int(*INT_INT_CALLBACK) (int a, int b);

int MakeCallback(int i, INT_INT_CALLBACK p)
{
    return (*p)(i, 4) + (*p)(i + 1, 5);
}


/* Another callback function.  This tests the various argument types. */
typedef double(*DBL_CALLBACK) (int a, char b, double c, float d, short e, int *f);

double MakeCallback2(DBL_CALLBACK p)
{
    int x = 1;
    double y = p(12345, 'X', (double)1.414, (float)2.8, 44, &x);
    return y;
}

// Check that void results work for callbacks.
void MakeCallback3(void(*mlcall)(int), int i)
{
    mlcall(i + 1);
}

/* Test for finalisation. */
void *AllocateIt()
{
    void *p = malloc(1);
    printf("Allocated object at %p\n", p);
    fflush(stdout);
    return p;
}

void FreeIt(void *p)
{
    printf("Freed object at %p\n", p);
    fflush(stdout);
    free(p);
}

/* Test for call-by-reference.  Added in updated FFI. */
void UpdateArg(int i, int *p)
{
    *p += i;
}

/* Test for returning a function.  Added in updated FFI. */
static int acallBack(int q)
{
    return q * 2;
}

typedef int(*CB)(int);

void ReturnFn(CB *v)
{
    *v = acallBack;
}