allocation.h 2.32 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_ALLOCATION_H_
#define V8_ALLOCATION_H_

8
#include "src/globals.h"
9

10 11
namespace v8 {
namespace internal {
12

13 14 15 16
// Called when allocation routines fail to allocate.
// This function should not return, but should terminate the current
// processing.
void FatalProcessOutOfMemory(const char* message);
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

// Superclass for classes managed with new & delete.
class 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);
};


// A macro is used for defining the base class used for embedded instances.
// The reason is some compilers allocate a minimum of one word for the
// superclass. The macro prevents the use of new & delete in debug mode.
// In release mode we are not willing to pay this overhead.

#ifdef DEBUG
// Superclass for classes with instances allocated inside stack
// activations or inside other objects.
class Embedded {
 public:
  void* operator new(size_t size);
  void  operator delete(void* p);
};
#define BASE_EMBEDDED : public Embedded
#else
#define BASE_EMBEDDED
#endif


// Superclass for classes only using statics.
class AllStatic {
#ifdef DEBUG
 public:
  void* operator new(size_t size);
  void operator delete(void* p);
#endif
};


template <typename T>
59
T* NewArray(size_t size) {
60
  T* result = new T[size];
61
  if (result == NULL) FatalProcessOutOfMemory("NewArray");
62 63 64 65 66
  return result;
}


template <typename T>
67
void DeleteArray(T* array) {
68 69 70 71
  delete[] array;
}


72 73 74
// The normal strdup functions use malloc.  These versions of StrDup
// and StrNDup uses new and calls the FatalProcessOutOfMemory handler
// if allocation fails.
75
char* StrDup(const char* str);
76
char* StrNDup(const char* str, int n);
77 78 79 80 81 82


// Allocation policy for allocating in the C free store using malloc
// and free. Used as the default policy for lists.
class FreeStoreAllocationPolicy {
 public:
83
  INLINE(void* New(size_t size)) { return Malloced::New(size); }
84 85 86 87
  INLINE(static void Delete(void* p)) { Malloced::Delete(p); }
};


88 89 90
void* AlignedAlloc(size_t size, size_t alignment);
void AlignedFree(void *ptr);

91 92
}  // namespace internal
}  // namespace v8
93 94

#endif  // V8_ALLOCATION_H_