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 "include/v8-platform.h"
9 #include "src/base/compiler-specific.h"
10 #include "src/base/platform/platform.h"
11 #include "src/globals.h"
12 #include "src/v8.h"
13
14 namespace v8 {
15 namespace internal {
16
17 class Isolate;
18
19 // This file defines memory allocation functions. If a first attempt at an
20 // allocation fails, these functions call back into the embedder, then attempt
21 // the allocation a second time. The embedder callback must not reenter V8.
22
23 // Called when allocation routines fail to allocate, even with a possible retry.
24 // This function should not return, but should terminate the current processing.
25 [[noreturn]] V8_EXPORT_PRIVATE void FatalProcessOutOfMemory(
26 Isolate* isolate, const char* message);
27
28 // Superclass for classes managed with new & delete.
29 class V8_EXPORT_PRIVATE Malloced {
30 public:
new(size_t size)31 void* operator new(size_t size) { return New(size); }
delete(void * p)32 void operator delete(void* p) { Delete(p); }
33
34 static void* New(size_t size);
35 static void Delete(void* p);
36 };
37
38 template <typename T>
NewArray(size_t size)39 T* NewArray(size_t size) {
40 T* result = new (std::nothrow) T[size];
41 if (result == nullptr) {
42 V8::GetCurrentPlatform()->OnCriticalMemoryPressure();
43 result = new (std::nothrow) T[size];
44 if (result == nullptr) FatalProcessOutOfMemory(nullptr, "NewArray");
45 }
46 return result;
47 }
48
49 template <typename T, typename = typename std::enable_if<
50 base::is_trivially_copyable<T>::value>::type>
NewArray(size_t size,T default_val)51 T* NewArray(size_t size, T default_val) {
52 T* result = reinterpret_cast<T*>(NewArray<uint8_t>(sizeof(T) * size));
53 for (size_t i = 0; i < size; ++i) result[i] = default_val;
54 return result;
55 }
56
57 template <typename T>
DeleteArray(T * array)58 void DeleteArray(T* array) {
59 delete[] array;
60 }
61
62
63 // The normal strdup functions use malloc. These versions of StrDup
64 // and StrNDup uses new and calls the FatalProcessOutOfMemory handler
65 // if allocation fails.
66 V8_EXPORT_PRIVATE char* StrDup(const char* str);
67 char* StrNDup(const char* str, int n);
68
69
70 // Allocation policy for allocating in the C free store using malloc
71 // and free. Used as the default policy for lists.
72 class FreeStoreAllocationPolicy {
73 public:
New(size_t size)74 V8_INLINE void* New(size_t size) { return Malloced::New(size); }
Delete(void * p)75 V8_INLINE static void Delete(void* p) { Malloced::Delete(p); }
76 };
77
78 // Performs a malloc, with retry logic on failure. Returns nullptr on failure.
79 // Call free to release memory allocated with this function.
80 void* AllocWithRetry(size_t size);
81
82 void* AlignedAlloc(size_t size, size_t alignment);
83 void AlignedFree(void *ptr);
84
85 // Gets the page granularity for AllocatePages and FreePages. Addresses returned
86 // by AllocatePages and AllocatePage are aligned to this size.
87 V8_EXPORT_PRIVATE size_t AllocatePageSize();
88
89 // Gets the granularity at which the permissions and release calls can be made.
90 V8_EXPORT_PRIVATE size_t CommitPageSize();
91
92 // Sets the random seed so that GetRandomMmapAddr() will generate repeatable
93 // sequences of random mmap addresses.
94 V8_EXPORT_PRIVATE void SetRandomMmapSeed(int64_t seed);
95
96 // Generate a random address to be used for hinting allocation calls.
97 V8_EXPORT_PRIVATE void* GetRandomMmapAddr();
98
99 // Allocates memory. Permissions are set according to the access argument.
100 // |address| is a hint. |size| and |alignment| must be multiples of
101 // AllocatePageSize(). Returns the address of the allocated memory, with the
102 // specified size and alignment, or nullptr on failure.
103 V8_EXPORT_PRIVATE
104 V8_WARN_UNUSED_RESULT void* AllocatePages(void* address, size_t size,
105 size_t alignment,
106 PageAllocator::Permission access);
107
108 // Frees memory allocated by a call to AllocatePages. |address| and |size| must
109 // be multiples of AllocatePageSize(). Returns true on success, otherwise false.
110 V8_EXPORT_PRIVATE
111 V8_WARN_UNUSED_RESULT bool FreePages(void* address, const size_t size);
112
113 // Releases memory that is no longer needed. The range specified by |address|
114 // and |size| must be an allocated memory region. |size| and |new_size| must be
115 // multiples of CommitPageSize(). Memory from |new_size| to |size| is released.
116 // Released memory is left in an undefined state, so it should not be accessed.
117 // Returns true on success, otherwise false.
118 V8_EXPORT_PRIVATE
119 V8_WARN_UNUSED_RESULT bool ReleasePages(void* address, size_t size,
120 size_t new_size);
121
122 // Sets permissions according to |access|. |address| and |size| must be
123 // multiples of CommitPageSize(). Setting permission to kNoAccess may
124 // cause the memory contents to be lost. Returns true on success, otherwise
125 // false.
126 V8_EXPORT_PRIVATE
127 V8_WARN_UNUSED_RESULT bool SetPermissions(void* address, size_t size,
128 PageAllocator::Permission access);
SetPermissions(Address address,size_t size,PageAllocator::Permission access)129 inline bool SetPermissions(Address address, size_t size,
130 PageAllocator::Permission access) {
131 return SetPermissions(reinterpret_cast<void*>(address), size, access);
132 }
133
134 // Convenience function that allocates a single system page with read and write
135 // permissions. |address| is a hint. Returns the base address of the memory and
136 // the page size via |allocated| on success. Returns nullptr on failure.
137 V8_EXPORT_PRIVATE
138 V8_WARN_UNUSED_RESULT byte* AllocatePage(void* address, size_t* allocated);
139
140 // Function that may release reserved memory regions to allow failed allocations
141 // to succeed. |length| is the amount of memory needed. Returns |true| if memory
142 // could be released, false otherwise.
143 V8_EXPORT_PRIVATE bool OnCriticalMemoryPressure(size_t length);
144
145 // Represents and controls an area of reserved memory.
146 class V8_EXPORT_PRIVATE VirtualMemory {
147 public:
148 // Empty VirtualMemory object, controlling no reserved memory.
149 VirtualMemory();
150
151 // Reserves virtual memory containing an area of the given size that is
152 // aligned per alignment. This may not be at the position returned by
153 // address().
154 VirtualMemory(size_t size, void* hint, size_t alignment = AllocatePageSize());
155
156 // Construct a virtual memory by assigning it some already mapped address
157 // and size.
VirtualMemory(Address address,size_t size)158 VirtualMemory(Address address, size_t size)
159 : address_(address), size_(size) {}
160
161 // Releases the reserved memory, if any, controlled by this VirtualMemory
162 // object.
163 ~VirtualMemory();
164
165 // Returns whether the memory has been reserved.
IsReserved()166 bool IsReserved() const { return address_ != kNullAddress; }
167
168 // Initialize or resets an embedded VirtualMemory object.
169 void Reset();
170
171 // Returns the start address of the reserved memory.
172 // If the memory was reserved with an alignment, this address is not
173 // necessarily aligned. The user might need to round it up to a multiple of
174 // the alignment to get the start of the aligned block.
address()175 Address address() const {
176 DCHECK(IsReserved());
177 return address_;
178 }
179
end()180 Address end() const {
181 DCHECK(IsReserved());
182 return address_ + size_;
183 }
184
185 // Returns the size of the reserved memory. The returned value is only
186 // meaningful when IsReserved() returns true.
187 // If the memory was reserved with an alignment, this size may be larger
188 // than the requested size.
size()189 size_t size() const { return size_; }
190
191 // Sets permissions according to the access argument. address and size must be
192 // multiples of CommitPageSize(). Returns true on success, otherwise false.
193 bool SetPermissions(Address address, size_t size,
194 PageAllocator::Permission access);
195
196 // Releases memory after |free_start|. Returns the number of bytes released.
197 size_t Release(Address free_start);
198
199 // Frees all memory.
200 void Free();
201
202 // Assign control of the reserved region to a different VirtualMemory object.
203 // The old object is no longer functional (IsReserved() returns false).
204 void TakeControl(VirtualMemory* from);
205
InVM(Address address,size_t size)206 bool InVM(Address address, size_t size) {
207 return (address_ <= address) && ((address_ + size_) >= (address + size));
208 }
209
210 private:
211 Address address_; // Start address of the virtual memory.
212 size_t size_; // Size of the virtual memory.
213 };
214
215 bool AllocVirtualMemory(size_t size, void* hint, VirtualMemory* result);
216 bool AlignedAllocVirtualMemory(size_t size, size_t alignment, void* hint,
217 VirtualMemory* result);
218
219 } // namespace internal
220 } // namespace v8
221
222 #endif // V8_ALLOCATION_H_
223