allocation.cc 3.15 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
#include "src/allocation.h"
6 7

#include <stdlib.h>  // For free, malloc.
8
#include "src/base/bits.h"
9 10
#include "src/base/logging.h"
#include "src/base/platform/platform.h"
11
#include "src/utils.h"
12
#include "src/v8.h"
13

14 15 16 17
#if V8_LIBC_BIONIC
#include <malloc.h>  // NOLINT
#endif

18 19
namespace v8 {
namespace internal {
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
namespace {

void* AlignedAllocInternal(size_t size, size_t alignment) {
  void* ptr;
#if V8_OS_WIN
  ptr = _aligned_malloc(size, alignment);
#elif V8_LIBC_BIONIC
  // posix_memalign is not exposed in some Android versions, so we fall back to
  // memalign. See http://code.google.com/p/android/issues/detail?id=35391.
  ptr = memalign(alignment, size);
#else
  if (posix_memalign(&ptr, alignment, size)) ptr = nullptr;
#endif
  return ptr;
}

}  // namespace

39 40
void* Malloced::New(size_t size) {
  void* result = malloc(size);
41 42 43 44 45 46
  if (result == nullptr) {
    V8::GetCurrentPlatform()->OnCriticalMemoryPressure();
    result = malloc(size);
    if (result == nullptr) {
      V8::FatalProcessOutOfMemory("Malloced operator new");
    }
47
  }
48 49 50 51 52 53 54 55 56 57
  return result;
}


void Malloced::Delete(void* p) {
  free(p);
}


char* StrDup(const char* str) {
58
  int length = StrLength(str);
59
  char* result = NewArray<char>(length + 1);
60
  MemCopy(result, str, length);
61 62 63 64 65
  result[length] = '\0';
  return result;
}


66 67
char* StrNDup(const char* str, int n) {
  int length = StrLength(str);
68 69
  if (n < length) length = n;
  char* result = NewArray<char>(length + 1);
70
  MemCopy(result, str, length);
71 72 73 74
  result[length] = '\0';
  return result;
}

75 76

void* AlignedAlloc(size_t size, size_t alignment) {
77
  DCHECK_LE(V8_ALIGNOF(void*), alignment);
78
  DCHECK(base::bits::IsPowerOfTwo(alignment));
79 80 81 82 83 84 85 86
  void* ptr = AlignedAllocInternal(size, alignment);
  if (ptr == nullptr) {
    V8::GetCurrentPlatform()->OnCriticalMemoryPressure();
    ptr = AlignedAllocInternal(size, alignment);
    if (ptr == nullptr) {
      V8::FatalProcessOutOfMemory("AlignedAlloc");
    }
  }
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  return ptr;
}


void AlignedFree(void *ptr) {
#if V8_OS_WIN
  _aligned_free(ptr);
#elif V8_LIBC_BIONIC
  // Using free is not correct in general, but for V8_LIBC_BIONIC it is.
  free(ptr);
#else
  free(ptr);
#endif
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
bool AllocVirtualMemory(size_t size, void* hint, base::VirtualMemory* result) {
  base::VirtualMemory first_try(size, hint);
  if (first_try.IsReserved()) {
    result->TakeControl(&first_try);
    return true;
  }

  V8::GetCurrentPlatform()->OnCriticalMemoryPressure();
  base::VirtualMemory second_try(size, hint);
  result->TakeControl(&second_try);
  return result->IsReserved();
}

bool AlignedAllocVirtualMemory(size_t size, size_t alignment, void* hint,
                               base::VirtualMemory* result) {
  base::VirtualMemory first_try(size, alignment, hint);
  if (first_try.IsReserved()) {
    result->TakeControl(&first_try);
    return true;
  }

  V8::GetCurrentPlatform()->OnCriticalMemoryPressure();
  base::VirtualMemory second_try(size, alignment, hint);
  result->TakeControl(&second_try);
  return result->IsReserved();
}

129 130
}  // namespace internal
}  // namespace v8