-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare.c
83 lines (62 loc) · 2.2 KB
/
prepare.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
73
74
75
76
77
78
79
80
81
82
83
// prepare.c
#include <alloca.h>
#include <string.h>
#include <assert.h>
#include <ctc/prepare.h>
#include <ctc/object.h>
#include <ctc/class.h>
#include <ctc/method.h>
#include "sorter.h"
#include "binary-searcher.h"
static int ctc_impl_compare(const struct ctc_impl *, const struct ctc_impl *);
static size_t ctc_count_ptrarray(const void **);
void ctc_sort_methods(size_t count, const struct ctc_method **methods) {
ctc_t *method_sorter_d = alloca(sizeof(ctc_t));
sorter_static_init(method_sorter_d, ctc_method_compare);
sorter_static->sort(method_sorter_d, count, (const void **)methods);
sorter_static->__fini__(method_sorter_d);
}
size_t ctc_count_methods(const struct ctc_method **methods) {
return ctc_count_ptrarray((const void **)methods);
}
void ctc_sort_impls(size_t count, const struct ctc_impl **impls) {
ctc_t *impl_sorter_d = alloca(sizeof(ctc_t));
sorter_static_init(impl_sorter_d, ctc_impl_compare);
sorter_static->sort(impl_sorter_d, count, (const void **)impls);
sorter_static->__fini__(impl_sorter_d);
}
size_t ctc_count_impls(const struct ctc_impl **impls) {
return ctc_count_ptrarray((const void **)impls);
}
void ctc_construct_impl(
const struct ctc_iface *class_iface,
const struct ctc_iface *impl_iface,
void *impl
) {
ctc_ensure_iface_methods_sorted(class_iface);
ctc_ensure_iface_methods_counted(class_iface);
ctc_ensure_iface_methods_counted(impl_iface);
ctc_t *method_binary_searcher_d = alloca(sizeof(ctc_t));
binary_searcher_static_init(method_binary_searcher_d, ctc_method_compare);
for (size_t i = 0; impl_iface->cache->methods_count > i; ++i) {
const struct ctc_method *impl_method = impl_iface->methods[i];
const struct ctc_method *class_method;
assert((class_method = binary_searcher_static->search(
method_binary_searcher_d, impl_method,
class_iface->cache->methods_count,
(const void **)class_iface->methods)));
ctc_function_t **impl_function =
(ctc_function_t **)((char *)impl + impl_method->offset);
*impl_function = class_method->function;
}
sorter_static->__fini__(method_binary_searcher_d);
}
static size_t ctc_count_ptrarray(const void **a) {
size_t count = 0;
do {
if (!a[count])
break;
count += 1;
} while (1);
return count;
}