1// Copyright 2012 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef V8_ALLOCATION_H_ 6#define V8_ALLOCATION_H_ 7 8#include "src/base/compiler-specific.h" 9#include "src/globals.h" 10 11namespace v8 { 12namespace internal { 13 14// Called when allocation routines fail to allocate. 15// This function should not return, but should terminate the current 16// processing. 17V8_EXPORT_PRIVATE void FatalProcessOutOfMemory(const char* message); 18 19// Superclass for classes managed with new & delete. 20class V8_EXPORT_PRIVATE Malloced { 21 public: 22 void* operator new(size_t size) { return New(size); } 23 void operator delete(void* p) { Delete(p); } 24 25 static void* New(size_t size); 26 static void Delete(void* p); 27}; 28 29// DEPRECATED 30// TODO(leszeks): Delete this during a quiet period 31#define BASE_EMBEDDED 32 33 34// Superclass for classes only using static method functions. 35// The subclass of AllStatic cannot be instantiated at all. 36class AllStatic { 37#ifdef DEBUG 38 public: 39 AllStatic() = delete; 40#endif 41}; 42 43 44template <typename T> 45T* NewArray(size_t size) { 46 T* result = new T[size]; 47 if (result == NULL) FatalProcessOutOfMemory("NewArray"); 48 return result; 49} 50 51 52template <typename T> 53void DeleteArray(T* array) { 54 delete[] array; 55} 56 57 58// The normal strdup functions use malloc. These versions of StrDup 59// and StrNDup uses new and calls the FatalProcessOutOfMemory handler 60// if allocation fails. 61V8_EXPORT_PRIVATE char* StrDup(const char* str); 62char* StrNDup(const char* str, int n); 63 64 65// Allocation policy for allocating in the C free store using malloc 66// and free. Used as the default policy for lists. 67class FreeStoreAllocationPolicy { 68 public: 69 INLINE(void* New(size_t size)) { return Malloced::New(size); } 70 INLINE(static void Delete(void* p)) { Malloced::Delete(p); } 71}; 72 73 74void* AlignedAlloc(size_t size, size_t alignment); 75void AlignedFree(void *ptr); 76 77} // namespace internal 78} // namespace v8 79 80#endif // V8_ALLOCATION_H_ 81