blob: 7c1e023b8646f8a258d715bbedb70ddd31f8e844 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_ALLOCATION_H_
6#define V8_ALLOCATION_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/globals.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +01009
Steve Blocka7e24c12009-10-30 11:49:00 +000010namespace v8 {
11namespace internal {
12
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080013// Called when allocation routines fail to allocate.
14// This function should not return, but should terminate the current
15// processing.
16void FatalProcessOutOfMemory(const char* message);
Steve Blocka7e24c12009-10-30 11:49:00 +000017
Steve Blocka7e24c12009-10-30 11:49:00 +000018// Superclass for classes managed with new & delete.
19class Malloced {
20 public:
21 void* operator new(size_t size) { return New(size); }
22 void operator delete(void* p) { Delete(p); }
23
Steve Blocka7e24c12009-10-30 11:49:00 +000024 static void* New(size_t size);
25 static void Delete(void* p);
26};
27
28
29// A macro is used for defining the base class used for embedded instances.
30// The reason is some compilers allocate a minimum of one word for the
31// superclass. The macro prevents the use of new & delete in debug mode.
32// In release mode we are not willing to pay this overhead.
33
34#ifdef DEBUG
35// Superclass for classes with instances allocated inside stack
36// activations or inside other objects.
37class Embedded {
38 public:
39 void* operator new(size_t size);
40 void operator delete(void* p);
41};
42#define BASE_EMBEDDED : public Embedded
43#else
44#define BASE_EMBEDDED
45#endif
46
47
48// Superclass for classes only using statics.
49class AllStatic {
50#ifdef DEBUG
51 public:
52 void* operator new(size_t size);
53 void operator delete(void* p);
54#endif
55};
56
57
58template <typename T>
Ben Murdoch3ef787d2012-04-12 10:51:47 +010059T* NewArray(size_t size) {
Steve Blocka7e24c12009-10-30 11:49:00 +000060 T* result = new T[size];
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000061 if (result == NULL) FatalProcessOutOfMemory("NewArray");
Steve Blocka7e24c12009-10-30 11:49:00 +000062 return result;
63}
64
65
66template <typename T>
Ben Murdoch3ef787d2012-04-12 10:51:47 +010067void DeleteArray(T* array) {
Steve Blocka7e24c12009-10-30 11:49:00 +000068 delete[] array;
69}
70
71
72// The normal strdup functions use malloc. These versions of StrDup
73// and StrNDup uses new and calls the FatalProcessOutOfMemory handler
74// if allocation fails.
75char* StrDup(const char* str);
Steve Blockd0582a62009-12-15 09:54:21 +000076char* StrNDup(const char* str, int n);
Steve Blocka7e24c12009-10-30 11:49:00 +000077
78
79// Allocation policy for allocating in the C free store using malloc
80// and free. Used as the default policy for lists.
81class FreeStoreAllocationPolicy {
82 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083 INLINE(void* New(size_t size)) { return Malloced::New(size); }
Steve Blocka7e24c12009-10-30 11:49:00 +000084 INLINE(static void Delete(void* p)) { Malloced::Delete(p); }
85};
86
87
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088void* AlignedAlloc(size_t size, size_t alignment);
89void AlignedFree(void *ptr);
Steve Blocka7e24c12009-10-30 11:49:00 +000090
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000091} // namespace internal
92} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +000093
94#endif // V8_ALLOCATION_H_