Commit 538e584a authored by Bill Budge's avatar Bill Budge Committed by Commit Bot

[Memory] Add v8::internal::AllocWithRetry function.

- Moves all allocation retry logic into allocation.

Bug: chromium:756050
Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng
Change-Id: I810d01f0a0a002b08bf3f82ea4195d636ab6e03a
Reviewed-on: https://chromium-review.googlesource.com/843230Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Commit-Queue: Bill Budge <bbudge@chromium.org>
Cr-Commit-Position: refs/heads/master@{#50305}
parent 8e33a405
...@@ -67,24 +67,17 @@ const int kAllocationTries = 2; ...@@ -67,24 +67,17 @@ const int kAllocationTries = 2;
} // namespace } // namespace
void* Malloced::New(size_t size) { void* Malloced::New(size_t size) {
void* result = nullptr; void* result = AllocWithRetry(size);
for (int i = 0; i < kAllocationTries; ++i) {
result = malloc(size);
if (result != nullptr) break;
if (!OnCriticalMemoryPressure(size)) break;
}
if (result == nullptr) { if (result == nullptr) {
V8::FatalProcessOutOfMemory("Malloced operator new"); V8::FatalProcessOutOfMemory("Malloced operator new");
} }
return result; return result;
} }
void Malloced::Delete(void* p) { void Malloced::Delete(void* p) {
free(p); free(p);
} }
char* StrDup(const char* str) { char* StrDup(const char* str) {
int length = StrLength(str); int length = StrLength(str);
char* result = NewArray<char>(length + 1); char* result = NewArray<char>(length + 1);
...@@ -93,7 +86,6 @@ char* StrDup(const char* str) { ...@@ -93,7 +86,6 @@ char* StrDup(const char* str) {
return result; return result;
} }
char* StrNDup(const char* str, int n) { char* StrNDup(const char* str, int n) {
int length = StrLength(str); int length = StrLength(str);
if (n < length) length = n; if (n < length) length = n;
...@@ -103,6 +95,15 @@ char* StrNDup(const char* str, int n) { ...@@ -103,6 +95,15 @@ char* StrNDup(const char* str, int n) {
return result; return result;
} }
void* AllocWithRetry(size_t size) {
void* result = nullptr;
for (int i = 0; i < kAllocationTries; ++i) {
result = malloc(size);
if (result != nullptr) break;
if (!OnCriticalMemoryPressure(size)) break;
}
return result;
}
void* AlignedAlloc(size_t size, size_t alignment) { void* AlignedAlloc(size_t size, size_t alignment) {
DCHECK_LE(V8_ALIGNOF(void*), alignment); DCHECK_LE(V8_ALIGNOF(void*), alignment);
...@@ -119,7 +120,6 @@ void* AlignedAlloc(size_t size, size_t alignment) { ...@@ -119,7 +120,6 @@ void* AlignedAlloc(size_t size, size_t alignment) {
return result; return result;
} }
void AlignedFree(void *ptr) { void AlignedFree(void *ptr) {
#if V8_OS_WIN #if V8_OS_WIN
_aligned_free(ptr); _aligned_free(ptr);
......
...@@ -72,6 +72,9 @@ class FreeStoreAllocationPolicy { ...@@ -72,6 +72,9 @@ class FreeStoreAllocationPolicy {
INLINE(static void Delete(void* p)) { Malloced::Delete(p); } INLINE(static void Delete(void* p)) { Malloced::Delete(p); }
}; };
// Performs a malloc, with retry logic on failure. Returns nullptr on failure.
// Call free to release memory allocated with this function.
void* AllocWithRetry(size_t size);
void* AlignedAlloc(size_t size, size_t alignment); void* AlignedAlloc(size_t size, size_t alignment);
void AlignedFree(void *ptr); void AlignedFree(void *ptr);
......
...@@ -84,13 +84,7 @@ Segment* AccountingAllocator::GetSegment(size_t bytes) { ...@@ -84,13 +84,7 @@ Segment* AccountingAllocator::GetSegment(size_t bytes) {
} }
Segment* AccountingAllocator::AllocateSegment(size_t bytes) { Segment* AccountingAllocator::AllocateSegment(size_t bytes) {
const int kAllocationTries = 2; void* memory = AllocWithRetry(bytes);
void* memory = nullptr;
for (int i = 0; i < kAllocationTries; ++i) {
memory = malloc(bytes);
if (memory != nullptr) break;
if (!OnCriticalMemoryPressure(bytes)) break;
}
if (memory != nullptr) { if (memory != nullptr) {
base::AtomicWord current = base::AtomicWord current =
base::Relaxed_AtomicIncrement(&current_memory_usage_, bytes); base::Relaxed_AtomicIncrement(&current_memory_usage_, bytes);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment