allocation.h 9.95 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 "include/v8-platform.h"
9
#include "src/base/address-region.h"
10
#include "src/base/compiler-specific.h"
11
#include "src/base/platform/platform.h"
12
#include "src/globals.h"
13
#include "src/v8.h"
14

15 16
namespace v8 {
namespace internal {
17

18 19
class Isolate;

20 21 22 23 24 25
// This file defines memory allocation functions. If a first attempt at an
// allocation fails, these functions call back into the embedder, then attempt
// the allocation a second time. The embedder callback must not reenter V8.

// Called when allocation routines fail to allocate, even with a possible retry.
// This function should not return, but should terminate the current processing.
26
[[noreturn]] V8_EXPORT_PRIVATE void FatalProcessOutOfMemory(
27
    Isolate* isolate, const char* message);
28 29

// Superclass for classes managed with new & delete.
30
class V8_EXPORT_PRIVATE Malloced {
31 32 33 34 35 36 37 38 39
 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);
};

template <typename T>
40
T* NewArray(size_t size) {
41 42
  T* result = new (std::nothrow) T[size];
  if (result == nullptr) {
43
    V8::GetCurrentPlatform()->OnCriticalMemoryPressure();
44
    result = new (std::nothrow) T[size];
45
    if (result == nullptr) FatalProcessOutOfMemory(nullptr, "NewArray");
46
  }
47
  return result;
48 49
}

50 51
template <typename T, typename = typename std::enable_if<
                          base::is_trivially_copyable<T>::value>::type>
52 53 54 55 56 57
T* NewArray(size_t size, T default_val) {
  T* result = reinterpret_cast<T*>(NewArray<uint8_t>(sizeof(T) * size));
  for (size_t i = 0; i < size; ++i) result[i] = default_val;
  return result;
}

58
template <typename T>
59
void DeleteArray(T* array) {
60 61 62 63
  delete[] array;
}


64 65 66
// The normal strdup functions use malloc.  These versions of StrDup
// and StrNDup uses new and calls the FatalProcessOutOfMemory handler
// if allocation fails.
67
V8_EXPORT_PRIVATE char* StrDup(const char* str);
68
char* StrNDup(const char* str, int n);
69 70 71 72 73 74


// Allocation policy for allocating in the C free store using malloc
// and free. Used as the default policy for lists.
class FreeStoreAllocationPolicy {
 public:
75 76
  V8_INLINE void* New(size_t size) { return Malloced::New(size); }
  V8_INLINE static void Delete(void* p) { Malloced::Delete(p); }
77 78
};

79 80 81
// Performs a malloc, with retry logic on failure. Returns nullptr on failure.
// Call free to release memory allocated with this function.
void* AllocWithRetry(size_t size);
82

83 84 85
void* AlignedAlloc(size_t size, size_t alignment);
void AlignedFree(void *ptr);

86 87 88
// Returns platfrom page allocator instance. Guaranteed to be a valid pointer.
V8_EXPORT_PRIVATE v8::PageAllocator* GetPlatformPageAllocator();

89 90 91 92 93 94 95
// Sets the given page allocator as the platform page allocator and returns
// the current one. This function *must* be used only for testing purposes.
// It is not thread-safe and the testing infrastructure should ensure that
// the tests do not modify the value simultaneously.
V8_EXPORT_PRIVATE v8::PageAllocator* SetPlatformPageAllocatorForTesting(
    v8::PageAllocator* page_allocator);

96 97 98 99 100 101 102
// Gets the page granularity for AllocatePages and FreePages. Addresses returned
// by AllocatePages and AllocatePage are aligned to this size.
V8_EXPORT_PRIVATE size_t AllocatePageSize();

// Gets the granularity at which the permissions and release calls can be made.
V8_EXPORT_PRIVATE size_t CommitPageSize();

103 104
// Sets the random seed so that GetRandomMmapAddr() will generate repeatable
// sequences of random mmap addresses.
105 106
V8_EXPORT_PRIVATE void SetRandomMmapSeed(int64_t seed);

107 108 109 110 111 112 113 114
// Generate a random address to be used for hinting allocation calls.
V8_EXPORT_PRIVATE void* GetRandomMmapAddr();

// Allocates memory. Permissions are set according to the access argument.
// |address| is a hint. |size| and |alignment| must be multiples of
// AllocatePageSize(). Returns the address of the allocated memory, with the
// specified size and alignment, or nullptr on failure.
V8_EXPORT_PRIVATE
115 116
V8_WARN_UNUSED_RESULT void* AllocatePages(v8::PageAllocator* page_allocator,
                                          void* address, size_t size,
117
                                          size_t alignment,
118
                                          PageAllocator::Permission access);
119 120 121 122

// Frees memory allocated by a call to AllocatePages. |address| and |size| must
// be multiples of AllocatePageSize(). Returns true on success, otherwise false.
V8_EXPORT_PRIVATE
123 124
V8_WARN_UNUSED_RESULT bool FreePages(v8::PageAllocator* page_allocator,
                                     void* address, const size_t size);
125 126 127 128 129 130 131

// Releases memory that is no longer needed. The range specified by |address|
// and |size| must be an allocated memory region. |size| and |new_size| must be
// multiples of CommitPageSize(). Memory from |new_size| to |size| is released.
// Released memory is left in an undefined state, so it should not be accessed.
// Returns true on success, otherwise false.
V8_EXPORT_PRIVATE
132 133
V8_WARN_UNUSED_RESULT bool ReleasePages(v8::PageAllocator* page_allocator,
                                        void* address, size_t size,
134 135 136 137 138 139 140
                                        size_t new_size);

// Sets permissions according to |access|. |address| and |size| must be
// multiples of CommitPageSize(). Setting permission to kNoAccess may
// cause the memory contents to be lost. Returns true on success, otherwise
// false.
V8_EXPORT_PRIVATE
141 142
V8_WARN_UNUSED_RESULT bool SetPermissions(v8::PageAllocator* page_allocator,
                                          void* address, size_t size,
143
                                          PageAllocator::Permission access);
144 145 146 147
inline bool SetPermissions(v8::PageAllocator* page_allocator, Address address,
                           size_t size, PageAllocator::Permission access) {
  return SetPermissions(page_allocator, reinterpret_cast<void*>(address), size,
                        access);
148
}
149 150 151 152 153

// Convenience function that allocates a single system page with read and write
// permissions. |address| is a hint. Returns the base address of the memory and
// the page size via |allocated| on success. Returns nullptr on failure.
V8_EXPORT_PRIVATE
154 155
V8_WARN_UNUSED_RESULT byte* AllocatePage(v8::PageAllocator* page_allocator,
                                         void* address, size_t* allocated);
156

157 158 159 160 161
// Function that may release reserved memory regions to allow failed allocations
// to succeed. |length| is the amount of memory needed. Returns |true| if memory
// could be released, false otherwise.
V8_EXPORT_PRIVATE bool OnCriticalMemoryPressure(size_t length);

162
// Represents and controls an area of reserved memory.
163
class V8_EXPORT_PRIVATE VirtualMemory final {
164 165
 public:
  // Empty VirtualMemory object, controlling no reserved memory.
166
  VirtualMemory() = default;
167

168
  // Reserves virtual memory containing an area of the given size that is
169
  // aligned per |alignment| rounded up to the |page_allocator|'s allocate page
170
  // size. The |size| must be aligned with |page_allocator|'s commit page size.
171
  // This may not be at the position returned by address().
172
  VirtualMemory(v8::PageAllocator* page_allocator, size_t size, void* hint,
173
                size_t alignment = 1);
174 175 176

  // Construct a virtual memory by assigning it some already mapped address
  // and size.
177
  VirtualMemory(v8::PageAllocator* page_allocator, Address address, size_t size)
178
      : page_allocator_(page_allocator), region_(address, size) {
179
    DCHECK_NOT_NULL(page_allocator);
180 181
    DCHECK(IsAligned(address, page_allocator->AllocatePageSize()));
    DCHECK(IsAligned(size, page_allocator->CommitPageSize()));
182
  }
183 184 185 186 187

  // Releases the reserved memory, if any, controlled by this VirtualMemory
  // object.
  ~VirtualMemory();

188 189 190 191 192 193 194 195 196
  // Move constructor.
  VirtualMemory(VirtualMemory&& other) V8_NOEXCEPT { TakeControl(&other); }

  // Move assignment operator.
  VirtualMemory& operator=(VirtualMemory&& other) V8_NOEXCEPT {
    TakeControl(&other);
    return *this;
  }

197
  // Returns whether the memory has been reserved.
198
  bool IsReserved() const { return region_.begin() != kNullAddress; }
199 200 201 202

  // Initialize or resets an embedded VirtualMemory object.
  void Reset();

203 204
  v8::PageAllocator* page_allocator() { return page_allocator_; }

205 206
  const base::AddressRegion& region() const { return region_; }

207 208 209 210
  // Returns the start address of the reserved memory.
  // If the memory was reserved with an alignment, this address is not
  // necessarily aligned. The user might need to round it up to a multiple of
  // the alignment to get the start of the aligned block.
211
  Address address() const {
212
    DCHECK(IsReserved());
213
    return region_.begin();
214 215
  }

216
  Address end() const {
217
    DCHECK(IsReserved());
218
    return region_.end();
219 220 221 222 223 224
  }

  // Returns the size of the reserved memory. The returned value is only
  // meaningful when IsReserved() returns true.
  // If the memory was reserved with an alignment, this size may be larger
  // than the requested size.
225
  size_t size() const { return region_.size(); }
226

227 228
  // Sets permissions according to the access argument. address and size must be
  // multiples of CommitPageSize(). Returns true on success, otherwise false.
229
  bool SetPermissions(Address address, size_t size,
230
                      PageAllocator::Permission access);
231

232
  // Releases memory after |free_start|. Returns the number of bytes released.
233
  size_t Release(Address free_start);
234

235 236
  // Frees all memory.
  void Free();
237 238 239 240 241

  // Assign control of the reserved region to a different VirtualMemory object.
  // The old object is no longer functional (IsReserved() returns false).
  void TakeControl(VirtualMemory* from);

242
  bool InVM(Address address, size_t size) {
243
    return region_.contains(address, size);
244 245 246
  }

 private:
247
  // Page allocator that controls the virtual memory.
248
  v8::PageAllocator* page_allocator_ = nullptr;
249
  base::AddressRegion region_;
250 251

  DISALLOW_COPY_AND_ASSIGN(VirtualMemory);
252 253
};

254 255
}  // namespace internal
}  // namespace v8
256 257

#endif  // V8_ALLOCATION_H_