Home | History | Annotate | Download | only in src
      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 
     11 namespace v8 {
     12 namespace internal {
     13 
     14 // Called when allocation routines fail to allocate.
     15 // This function should not return, but should terminate the current
     16 // processing.
     17 V8_EXPORT_PRIVATE void FatalProcessOutOfMemory(const char* message);
     18 
     19 // Superclass for classes managed with new & delete.
     20 class 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.
     36 class AllStatic {
     37 #ifdef DEBUG
     38  public:
     39   AllStatic() = delete;
     40 #endif
     41 };
     42 
     43 
     44 template <typename T>
     45 T* NewArray(size_t size) {
     46   T* result = new T[size];
     47   if (result == NULL) FatalProcessOutOfMemory("NewArray");
     48   return result;
     49 }
     50 
     51 
     52 template <typename T>
     53 void 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.
     61 V8_EXPORT_PRIVATE char* StrDup(const char* str);
     62 char* 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.
     67 class 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 
     74 void* AlignedAlloc(size_t size, size_t alignment);
     75 void AlignedFree(void *ptr);
     76 
     77 }  // namespace internal
     78 }  // namespace v8
     79 
     80 #endif  // V8_ALLOCATION_H_
     81