-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathinterface.cpp
68 lines (57 loc) · 1.79 KB
/
interface.cpp
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
// Copyright (C) 2018 Cornell University
#include "rep.h"
#include <cstdlib>
#include <inttypes.h>
#include <string.h>
#include "interface.h"
#include "monitor.h"
#include "threads.h"
extern "C" {
void __createInterfaceTables(DispatchVector *D, int capacity, int size,
int intf_id_hashcodes[], void *intf_ids[],
void *intf_tables[]) {
ScopedLock lock(Monitor::Instance().globalMutex());
idv_ht *ittab = new idv_ht(capacity);
for (int i = 0; i < size; ++i)
ittab->put(intf_id_hashcodes[i], intf_ids[i], intf_tables[i]);
D->SetIdv(ittab);
}
void *__getInterfaceMethod(jobject obj, int intf_id_hash, void *intf_id,
int method_index) {
idv_ht *ittab = Unwrap(obj)->Cdv()->Idv();
void **itab = reinterpret_cast<void **>(ittab->get(intf_id_hash, intf_id));
return itab[method_index];
}
} // extern "C"
idv_ht::idv_ht(size_t capacity) {
this->table = new idv_ht_node *[capacity];
this->capacity = capacity;
}
void *idv_ht::get(int hashcode, void *intf_id) {
int index = getIndexForHash(hashcode);
idv_ht_node *node = table[index];
while (node->next != nullptr) {
if (node->intf_id == intf_id)
return node->idv;
node = node->next;
}
return node->idv;
}
void idv_ht::put(int hashcode, void *intf_id, void *idv) {
idv_ht_node *node = new idv_ht_node;
node->intf_id = intf_id;
node->idv = idv;
int index = getIndexForHash(hashcode);
idv_ht_node *lst = table[index];
if (lst == nullptr) {
table[index] = node;
} else {
idv_ht_node *tail = lst;
table[index] = node;
node->next = tail;
}
}
size_t idv_ht::getIndexForHash(int h) {
size_t index = h & (this->capacity - 1);
return index;
}