// Copyright 2012 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_ALLOCATION_H_ #define V8_ALLOCATION_H_ #include "include/v8-platform.h" #include "src/base/compiler-specific.h" #include "src/base/platform/platform.h" #include "src/globals.h" #include "src/v8.h" namespace v8 { namespace internal { // This file defines memory allocation functions. If a first attempt at an // allocation fails, these functions call back into the embedder, then attempt // the allocation a second time. The embedder callback must not reenter V8. // Called when allocation routines fail to allocate, even with a possible retry. // This function should not return, but should terminate the current processing. V8_EXPORT_PRIVATE void FatalProcessOutOfMemory(const char* message); // Superclass for classes managed with new & delete. class V8_EXPORT_PRIVATE Malloced { public: void* operator new(size_t size) { return New(size); } void operator delete(void* p) { Delete(p); } static void* New(size_t size); static void Delete(void* p); }; template T* NewArray(size_t size) { T* result = new (std::nothrow) T[size]; if (result == nullptr) { V8::GetCurrentPlatform()->OnCriticalMemoryPressure(); result = new (std::nothrow) T[size]; if (result == nullptr) FatalProcessOutOfMemory("NewArray"); } return result; } template void DeleteArray(T* array) { delete[] array; } // The normal strdup functions use malloc. These versions of StrDup // and StrNDup uses new and calls the FatalProcessOutOfMemory handler // if allocation fails. V8_EXPORT_PRIVATE char* StrDup(const char* str); char* StrNDup(const char* str, int n); // Allocation policy for allocating in the C free store using malloc // and free. Used as the default policy for lists. class FreeStoreAllocationPolicy { public: INLINE(void* New(size_t size)) { return Malloced::New(size); } INLINE(static void Delete(void* p)) { Malloced::Delete(p); } }; void* AlignedAlloc(size_t size, size_t alignment); void AlignedFree(void *ptr); bool AllocVirtualMemory(size_t size, void* hint, base::VirtualMemory* result); bool AlignedAllocVirtualMemory(size_t size, size_t alignment, void* hint, base::VirtualMemory* result); } // namespace internal } // namespace v8 #endif // V8_ALLOCATION_H_