| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6a0eb9f commit e18df46
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -198,6 +198,7 @@ bool trace_warnings = false; | |||
| 198 | 198 | // that is used by lib/module.js | |
| 199 | 199 | bool config_preserve_symlinks = false; | |
| 200 | 200 | ||
| 201 | + bool v8_initialized = false; | ||
| 201 | 202 | ||
| 202 | 203 | // Set in node.cc by ParseArgs when --expose-internals or --expose_internals is | |
| 203 | 204 | // used. | |
@@ -4895,6 +4896,7 @@ int Start(int argc, char** argv) { | |||
| 4895 | 4896 | ||
| 4896 | 4897 | v8_platform.Initialize(v8_thread_pool_size); | |
| 4897 | 4898 | V8::Initialize(); | |
| 4899 | + v8_initialized = true; | ||
| 4898 | 4900 | ||
| 4899 | 4901 | int exit_code = 1; | |
| 4900 | 4902 | { | |
@@ -4908,6 +4910,7 @@ int Start(int argc, char** argv) { | |||
| 4908 | 4910 | StartNodeInstance(&instance_data); | |
| 4909 | 4911 | exit_code = instance_data.exit_code(); | |
| 4910 | 4912 | } | |
| 4913 | + v8_initialized = false; | ||
| 4911 | 4914 | V8::Dispose(); | |
| 4912 | 4915 | ||
| 4913 | 4916 | v8_platform.Dispose(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,6 +43,9 @@ extern std::string openssl_config; | |||
| 43 | 43 | // that is used by lib/module.js | |
| 44 | 44 | extern bool config_preserve_symlinks; | |
| 45 | 45 | ||
| 46 | + // Tells whether it is safe to call v8::Isolate::GetCurrent(). | ||
| 47 | + extern bool v8_initialized; | ||
| 48 | + | ||
| 46 | 49 | // Set in node.cc by ParseArgs when --expose-internals or --expose_internals is | |
| 47 | 50 | // used. | |
| 48 | 51 | // Used in node_config.cc to set a constant on process.binding('config') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -344,7 +344,15 @@ T* UncheckedRealloc(T* pointer, size_t n) { | |||
| 344 | 344 | return nullptr; | |
| 345 | 345 | } | |
| 346 | 346 | ||
| 347 | - return static_cast<T*>(realloc(pointer, full_size)); | ||
| 347 | + void* allocated = realloc(pointer, full_size); | ||
| 348 | + | ||
| 349 | + if (UNLIKELY(allocated == nullptr)) { | ||
| 350 | + // Tell V8 that memory is low and retry. | ||
| 351 | + LowMemoryNotification(); | ||
| 352 | + allocated = realloc(pointer, full_size); | ||
| 353 | + } | ||
| 354 | + | ||
| 355 | + return static_cast<T*>(allocated); | ||
| 348 | 356 | } | |
| 349 | 357 | ||
| 350 | 358 | // As per spec realloc behaves like malloc if passed nullptr. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -77,4 +77,13 @@ BufferValue::BufferValue(Isolate* isolate, Local<Value> value) { | |||
| 77 | 77 | } | |
| 78 | 78 | } | |
| 79 | 79 | ||
| 80 | + void LowMemoryNotification() { | ||
| 81 | + if (v8_initialized) { | ||
| 82 | + auto isolate = v8::Isolate::GetCurrent(); | ||
| 83 | + if (isolate != nullptr) { | ||
| 84 | + isolate->LowMemoryNotification(); | ||
| 85 | + } | ||
| 86 | + } | ||
| 87 | + } | ||
| 88 | + | ||
| 80 | 89 | } // namespace node | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,6 +53,11 @@ inline char* Calloc(size_t n) { return Calloc<char>(n); } | |||
| 53 | 53 | inline char* UncheckedMalloc(size_t n) { return UncheckedMalloc<char>(n); } | |
| 54 | 54 | inline char* UncheckedCalloc(size_t n) { return UncheckedCalloc<char>(n); } | |
| 55 | 55 | ||
| 56 | + // Used by the allocation functions when allocation fails. | ||
| 57 | + // Thin wrapper around v8::Isolate::LowMemoryNotification() that checks | ||
| 58 | + // whether V8 is initialized. | ||
| 59 | + void LowMemoryNotification(); | ||
| 60 | + | ||
| 56 | 61 | #ifdef __GNUC__ | |
| 57 | 62 | #define NO_RETURN __attribute__((noreturn)) | |
| 58 | 63 | #else | |
| Back | FazBrowse Home | New Git URL |
0 commit comments