forked from mooware/CtrlFFI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FFIExternHdl.hxx
124 lines (87 loc) · 3.17 KB
/
FFIExternHdl.hxx
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
120
121
122
123
124
#ifndef _FFIEXTERNHDL_H_
#define _FFIEXTERNHDL_H_
#include <FFITypes.hxx>
#include <FFIValue.hxx>
#include <BaseExternHdl.hxx>
#include <SimplePtrArray.hxx>
#include <Types.hxx>
#include <ffi.h>
#include <vector>
// forward declarations
class Variable;
//------------------------------------------------------------------------------
class FFIExternHdl : public BaseExternHdl
{
public:
/// Function pointer type as used by libffi
typedef void (*VoidFunction)(void);
/// Stores a function declaration
struct FFIFunction
{
/// Constructor. Necessary to recognize an empty object in the dtor.
FFIFunction() : funcPtr(0)
{
// set just enough to be able to recognize an empty cif
callInterface.nargs = 0;
callInterface.arg_types = 0;
}
/// Destructor. Necessary because the ffi_cif contains a heap allocated array.
~FFIFunction()
{
// Delete the arg_types array, which should be the only heap allocated
// memory here
if (callInterface.arg_types)
{
delete[] callInterface.arg_types;
}
}
/// Name of the function (e.g. "memset")
CharString funcName;
/// Filename of the library containing the function
CharString libName;
/// Function pointer to call the function
VoidFunction funcPtr;
/// Return type of the function
IntegralType returnType;
/// List of argument types for the function
std::vector<IntegralType> argTypes;
/// libffi call interface definition
ffi_cif callInterface;
};
// boilerplate stuff
FFIExternHdl(BaseExternHdl *nextHdl, PVSSulong funcCount, FunctionListRec fnList[]);
virtual ~FFIExternHdl();
virtual const Variable *execute(ExecuteParamRec ¶m);
private:
// ctrl functions
unsigned int ffiDeclareFunction(ExecuteParamRec ¶m);
bool ffiCallFunction(ExecuteParamRec ¶m);
DynVar *ffiGetAllFunctions(ExecuteParamRec ¶m);
unsigned int ffiGetTypeSize(ExecuteParamRec ¶m);
const char *ffiGetTypeName(ExecuteParamRec ¶m);
PVSSulonglong ffiAllocBuffer(ExecuteParamRec ¶m);
void ffiFreeBuffer(ExecuteParamRec ¶m);
char *ffiBufferToString(ExecuteParamRec ¶m);
DynVar *ffiBufferToStruct(ExecuteParamRec ¶m);
DynVar *ffiBufferToDyn(ExecuteParamRec ¶m);
void ffiFillBufferWithString(ExecuteParamRec ¶m);
void ffiFillBufferWithStruct(ExecuteParamRec ¶m);
void ffiFillBufferWithDyn(ExecuteParamRec ¶m);
Variable *ffiReadFromPointer(ExecuteParamRec ¶m);
void ffiWriteToPointer(ExecuteParamRec ¶m);
// helpers
/// Returns the ffi_type struct to be used for an IntegralType
static ffi_type *getFFIType(int type);
/// Returns true if the given type is valid for readAddress and writeAddress
static bool isValidForRawMemoryOperation(int type);
/// Reads from a pointer to a new Ctrl var
static Variable *readAddress(int type, const char *buffer);
/// Writes from a Ctrl var to a pointer
static void writeAddress(int type, char *buffer, const Variable &var);
// members
/// List of the declared functions
SimplePtrArray<FFIFunction> functions;
/// The number of the CTRLFFI -dbg flag
static PVSSshort dbgFlag;
};
#endif // _FFIEXTERNHDL_H_