[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/843230
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Commit-Queue: Bill Budge <bbudge@chromium.org>
Cr-Commit-Position: refs/heads/master@{#50305}
This commit is contained in:
Bill Budge 2017-12-22 11:39:00 -08:00 committed by Commit Bot
parent 8e33a405f6
commit 538e584ab4
3 changed files with 14 additions and 17 deletions

View File

@ -67,24 +67,17 @@ const int kAllocationTries = 2;
} // namespace
void* Malloced::New(size_t size) {
void* result = nullptr;
for (int i = 0; i < kAllocationTries; ++i) {
result = malloc(size);
if (result != nullptr) break;
if (!OnCriticalMemoryPressure(size)) break;
}
void* result = AllocWithRetry(size);
if (result == nullptr) {
V8::FatalProcessOutOfMemory("Malloced operator new");
}
return result;
}
void Malloced::Delete(void* p) {
free(p);
}
char* StrDup(const char* str) {
int length = StrLength(str);
char* result = NewArray<char>(length + 1);
@ -93,7 +86,6 @@ char* StrDup(const char* str) {
return result;
}
char* StrNDup(const char* str, int n) {
int length = StrLength(str);
if (n < length) length = n;
@ -103,6 +95,15 @@ char* StrNDup(const char* str, int n) {
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) {
DCHECK_LE(V8_ALIGNOF(void*), alignment);
@ -119,7 +120,6 @@ void* AlignedAlloc(size_t size, size_t alignment) {
return result;
}
void AlignedFree(void *ptr) {
#if V8_OS_WIN
_aligned_free(ptr);

View File

@ -72,6 +72,9 @@ class FreeStoreAllocationPolicy {
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 AlignedFree(void *ptr);

View File

@ -84,13 +84,7 @@ Segment* AccountingAllocator::GetSegment(size_t bytes) {
}
Segment* AccountingAllocator::AllocateSegment(size_t bytes) {
const int kAllocationTries = 2;
void* memory = nullptr;
for (int i = 0; i < kAllocationTries; ++i) {
memory = malloc(bytes);
if (memory != nullptr) break;
if (!OnCriticalMemoryPressure(bytes)) break;
}
void* memory = AllocWithRetry(bytes);
if (memory != nullptr) {
base::AtomicWord current =
base::Relaxed_AtomicIncrement(&current_memory_usage_, bytes);