-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
plv8_allocator.cc
71 lines (62 loc) · 2.02 KB
/
plv8_allocator.cc
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
#include "plv8_allocator.h"
#define RECHECK_INCREMENT 1_MB
size_t operator""_MB( unsigned long long const x ) { return 1024L * 1024L * x; }
ArrayAllocator::ArrayAllocator(size_t limit) : heap_limit(limit),
heap_size(RECHECK_INCREMENT),
next_size(RECHECK_INCREMENT),
allocated(0),
allocator(v8::ArrayBuffer::Allocator::NewDefaultAllocator()) {}
ArrayAllocator::~ArrayAllocator() {
delete this->allocator;
}
bool ArrayAllocator::check(const size_t length) {
if (heap_size + allocated + length > next_size) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::HeapStatistics heap_statistics;
isolate->GetHeapStatistics(&heap_statistics);
heap_size = heap_statistics.used_heap_size();
if (heap_size + allocated + length > heap_limit) {
return false;
// we need to force GC here,
// otherwise the next allocation will fail even if there is a space for it
isolate->LowMemoryNotification();
isolate->GetHeapStatistics(&heap_statistics);
heap_size = heap_statistics.used_heap_size();
if (heap_size + allocated + length > heap_limit) {
return false;
}
}
next_size = heap_size + allocated + length + RECHECK_INCREMENT;
}
return heap_size + allocated + length <= heap_limit;
}
void* ArrayAllocator::Allocate(size_t length) {
if (check(length)) {
allocated += length;
return this->allocator->Allocate(length);
} else {
return nullptr;
}
}
void* ArrayAllocator::AllocateUninitialized(size_t length) {
if (check(length)) {
allocated += length;
return std::malloc(length);
} else {
return nullptr;
}
}
void ArrayAllocator::Free(void* data, size_t length) {
allocated -= length;
next_size -= length;
this->allocator->Free(data, length);
}
void* ArrayAllocator::Reallocate(void *data, size_t old_length, size_t new_length) {
ssize_t delta = static_cast<ssize_t>(new_length) - static_cast<ssize_t>(old_length);
if (delta > 0) {
if (!check(delta)) {
return nullptr;
}
}
return this->allocator->Reallocate(data, old_length, new_length);
}