-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathFFICall.h
More file actions
128 lines (102 loc) · 3.56 KB
/
FFICall.h
File metadata and controls
128 lines (102 loc) · 3.56 KB
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
125
126
127
128
#ifndef FFICall_h
#define FFICall_h
#include <malloc/malloc.h>
#include <map>
#include "DataWrapper.h"
#include "Metadata.h"
#include "libffi.h"
#include "robin_hood.h"
namespace tns {
class BaseCall {
public:
BaseCall(uint8_t* buffer, size_t returnOffset = 0)
: buffer_(buffer), returnOffset_(returnOffset) {}
~BaseCall() {}
inline void* ResultBuffer() { return this->buffer_ + this->returnOffset_; }
template <typename T>
inline T& GetResult() {
return *static_cast<T*>(this->ResultBuffer());
}
protected:
uint8_t* buffer_;
size_t returnOffset_;
};
class ParametrizedCall {
public:
ParametrizedCall(ffi_cif* cif) : Cif(cif), ReturnOffset(0), StackSize(0) {
unsigned int argsCount = cif->nargs;
this->StackSize = 0;
if (argsCount > 0) {
// compute total bytes = number of pointers × size of a pointer
size_t needed = sizeof(void*) * static_cast<size_t>(argsCount);
this->StackSize = malloc_good_size(needed);
}
this->ReturnOffset = this->StackSize;
this->StackSize +=
malloc_good_size(std::max(cif->rtype->size, sizeof(ffi_arg)));
this->ArgValueOffsets.reserve(argsCount);
for (size_t i = 0; i < argsCount; i++) {
this->ArgValueOffsets.push_back(this->StackSize);
ffi_type* argType = cif->arg_types[i];
this->StackSize +=
malloc_good_size(std::max(argType->size, sizeof(ffi_arg)));
}
}
static ParametrizedCall* Get(const TypeEncoding* typeEncoding,
const int initialParameterIndex,
const int argsCount);
ffi_cif* Cif;
size_t ReturnOffset;
size_t StackSize;
std::vector<size_t> ArgValueOffsets;
private:
static robin_hood::unordered_map<const TypeEncoding*, ParametrizedCall*>
callsCache_;
};
class FFICall : public BaseCall {
public:
FFICall(ParametrizedCall* parametrizedCall) : BaseCall(nullptr) {
this->returnOffset_ = parametrizedCall->ReturnOffset;
this->useDynamicBuffer_ = parametrizedCall->StackSize > 512;
if (this->useDynamicBuffer_) {
this->buffer_ =
reinterpret_cast<uint8_t*>(malloc(parametrizedCall->StackSize));
} else {
this->buffer_ = reinterpret_cast<uint8_t*>(this->staticBuffer);
}
this->argsArray_ = reinterpret_cast<void**>(this->buffer_);
for (size_t i = 0; i < parametrizedCall->Cif->nargs; i++) {
this->argsArray_[i] =
this->buffer_ + parametrizedCall->ArgValueOffsets[i];
}
}
~FFICall() {
if (this->useDynamicBuffer_) {
free(this->buffer_);
}
}
/**
When calling this, always make another call to DisposeFFIType with the same
parameters
*/
static ffi_type* GetArgumentType(const TypeEncoding* typeEncoding,
bool isStructMember = false);
static void DisposeFFIType(ffi_type* type, const TypeEncoding* typeEncoding);
static StructInfo GetStructInfo(const StructMeta* structMeta,
std::string structName = "");
static StructInfo GetStructInfo(size_t fieldsCount,
const TypeEncoding* fieldEncoding,
const String* fieldNames,
std::string structName = "");
inline void* ArgumentBuffer(unsigned index) {
return this->argsArray_[index];
}
inline void** ArgsArray() { return this->argsArray_; }
private:
static robin_hood::unordered_map<std::string, StructInfo> structInfosCache_;
void** argsArray_;
bool useDynamicBuffer_;
uint8_t staticBuffer[512];
};
} // namespace tns
#endif /* FFICall_h */