-
Notifications
You must be signed in to change notification settings - Fork 4
/
js_alloc.hpp
124 lines (102 loc) · 2.62 KB
/
js_alloc.hpp
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 JS_ALLOC_HPP
#define JS_ALLOC_HPP
#include <quickjs.h>
#include <unistd.h>
#include <cstdlib>
#ifndef _WIN32
#include <sys/mman.h>
template<class T> struct js_alloc_mmap {
static size_t page_size;
static constexpr size_t
round_to_page_size(size_t n) {
size_t pages = (n + (page_size - 1)) / page_size;
return pages * page_size;
}
static constexpr size_t size = round_to_page_size(sizeof(T));
static constexpr size_t offset = size - sizeof(T);
static T*
allocate(JSContext* ctx) {
return reinterpret_cast<T*>(static_cast<char*>(mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)) + offset);
}
static void
deallocate(JSContext* ctx, T* ptr) {
munmap(reinterpret_cast<char*>(ptr) - offset, size);
}
static void
deallocate(JSRuntime* rt, T* ptr) {
munmap(reinterpret_cast<char*>(ptr) - offset, size);
}
};
template<class T> size_t js_alloc_mmap<T>::page_size = ::getpagesize();
#endif
template<class T> struct js_alloc_libc {
static constexpr size_t size = ((sizeof(T) + 7) >> 3) << 3;
static T*
allocate(JSContext* ctx) {
return static_cast<T*>(malloc(size));
}
static void
deallocate(JSContext* ctx, T* ptr) {
free(ptr);
}
static void
deallocate(JSRuntime* rt, T* ptr) {
free(ptr);
}
};
template<class T> struct js_alloc_quickjs {
static constexpr size_t size = ((sizeof(T) + 7) >> 3) << 3;
static T*
allocate(JSContext* ctx) {
return static_cast<T*>(js_mallocz(ctx, size));
}
static T*
allocate(JSContext* ctx, size_t n) {
return static_cast<T*>(js_mallocz(ctx, size * n));
}
static void
deallocate(JSContext* ctx, T* ptr) {
js_free(ctx, ptr);
}
static void
deallocate(JSRuntime* rt, T* ptr) {
js_free_rt(rt, ptr);
}
};
template<class T> struct js_alloc_cxx {
static constexpr size_t size = ((sizeof(T) + 7) >> 3) << 3;
static T*
allocate(JSContext* ctx) {
return new T();
}
static void
deallocate(JSContext* ctx, T* ptr) {
delete ptr;
}
static void
deallocate(JSRuntime* rt, T* ptr) {
delete ptr;
}
};
template<class T> using js_allocator = js_alloc_quickjs<T>;
template<class T>
static inline T*
js_allocate(JSContext* ctx) {
return js_allocator<T>::allocate(ctx);
}
template<class T>
static inline T*
js_allocate(JSContext* ctx, size_t n) {
return js_allocator<T>::allocate(ctx, n);
}
template<class T>
static inline void
js_deallocate(JSContext* ctx, T* ptr) {
js_allocator<T>::deallocate(ctx, ptr);
}
template<class T>
static inline void
js_deallocate(JSRuntime* rt, T* ptr) {
js_allocator<T>::deallocate(rt, ptr);
}
#endif /* defined(JS_ALLOC_HPP) */