-
Notifications
You must be signed in to change notification settings - Fork 68
/
sqFFIPlugin.c
72 lines (65 loc) · 1.93 KB
/
sqFFIPlugin.c
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
/****************************************************************************
* PROJECT: Squeak threaded foreign function interface
* FILE: sqFFIPlugin.c
* CONTENT: C support code for the threaded FFIPlugin
*
* AUTHOR: Eliot Miranda
*
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h> /* proto for alloca in MINGW */
#if !_WIN32 && !__FreeBSD__ && !__OpenBSD__
# include <alloca.h>
#endif
#include <string.h>
#ifdef _MSC_VER
# include <windows.h>
# define alloca _alloca
#endif
/* this is a stub through which floating-point register arguments can be loaded
* prior to an FFI call proper. e.g. on the PowerPC this would be declared as
* extern void loadFloatRegs(double, double, double, double,
* double, double, double, double);
* and called with the appropriate values necessary to load the floating-point
* argument registers. Immediately after the actual call is made, using the
* undisturbed register contents created by the call of loadFloatRegs.
*/
void
loadFloatRegs(void) { return; }
static FILE *ffiLogFile = NULL;
int
ffiLogFileNameOfLength(void *nameIndex, int nameLength)
{
if (nameIndex && nameLength) {
char *fileName;
FILE *fp;
if (!(fileName = alloca(nameLength+1)))
return 0;
strncpy(fileName, nameIndex, nameLength);
fileName[nameLength] = 0;
/* attempt to open the file and if we can't, fail */
if (!(fp = fopen(fileName, "at")))
return 0;
/* close the old log file if needed and use the new one */
if (ffiLogFile)
fclose(ffiLogFile);
ffiLogFile = fp;
fprintf(ffiLogFile, "------- Log started -------\n");
fflush(fp);
}
else {
if (ffiLogFile)
fclose(ffiLogFile);
ffiLogFile = NULL;
}
return 1;
}
int
ffiLogCallOfLength(void *nameIndex, int nameLength)
{
if (!ffiLogFile)
return 0;
fprintf(ffiLogFile, "%.*s\n", nameLength, (char *)nameIndex);
fflush(ffiLogFile);
return 1;
}