V8 Project
allocation.cc
Go to the documentation of this file.
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 #include "src/allocation.h"
6 
7 #include <stdlib.h> // For free, malloc.
8 #include "src/base/bits.h"
9 #include "src/base/logging.h"
11 #include "src/utils.h"
12 
13 #if V8_LIBC_BIONIC
14 #include <malloc.h> // NOLINT
15 #endif
16 
17 namespace v8 {
18 namespace internal {
19 
20 void* Malloced::New(size_t size) {
21  void* result = malloc(size);
22  if (result == NULL) {
23  v8::internal::FatalProcessOutOfMemory("Malloced operator new");
24  }
25  return result;
26 }
27 
28 
29 void Malloced::Delete(void* p) {
30  free(p);
31 }
32 
33 
36 }
37 
38 
39 #ifdef DEBUG
40 
41 static void* invalid = static_cast<void*>(NULL);
42 
43 void* Embedded::operator new(size_t size) {
44  UNREACHABLE();
45  return invalid;
46 }
47 
48 
49 void Embedded::operator delete(void* p) {
50  UNREACHABLE();
51 }
52 
53 
54 void* AllStatic::operator new(size_t size) {
55  UNREACHABLE();
56  return invalid;
57 }
58 
59 
60 void AllStatic::operator delete(void* p) {
61  UNREACHABLE();
62 }
63 
64 #endif
65 
66 
67 char* StrDup(const char* str) {
68  int length = StrLength(str);
69  char* result = NewArray<char>(length + 1);
70  MemCopy(result, str, length);
71  result[length] = '\0';
72  return result;
73 }
74 
75 
76 char* StrNDup(const char* str, int n) {
77  int length = StrLength(str);
78  if (n < length) length = n;
79  char* result = NewArray<char>(length + 1);
80  MemCopy(result, str, length);
81  result[length] = '\0';
82  return result;
83 }
84 
85 
86 void* AlignedAlloc(size_t size, size_t alignment) {
87  DCHECK_LE(V8_ALIGNOF(void*), alignment);
89  void* ptr;
90 #if V8_OS_WIN
91  ptr = _aligned_malloc(size, alignment);
92 #elif V8_LIBC_BIONIC
93  // posix_memalign is not exposed in some Android versions, so we fall back to
94  // memalign. See http://code.google.com/p/android/issues/detail?id=35391.
95  ptr = memalign(alignment, size);
96 #else
97  if (posix_memalign(&ptr, alignment, size)) ptr = NULL;
98 #endif
99  if (ptr == NULL) FatalProcessOutOfMemory("AlignedAlloc");
100  return ptr;
101 }
102 
103 
104 void AlignedFree(void *ptr) {
105 #if V8_OS_WIN
106  _aligned_free(ptr);
107 #elif V8_LIBC_BIONIC
108  // Using free is not correct in general, but for V8_LIBC_BIONIC it is.
109  free(ptr);
110 #else
111  free(ptr);
112 #endif
113 }
114 
115 } } // namespace v8::internal
static void * New(size_t size)
Definition: allocation.cc:20
static void FatalProcessOutOfMemory()
Definition: allocation.cc:34
static void Delete(void *p)
Definition: allocation.cc:29
enable harmony numeric enable harmony object literal extensions Optimize object size
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
#define UNREACHABLE()
Definition: logging.h:30
#define DCHECK_LE(v1, v2)
Definition: logging.h:210
#define DCHECK(condition)
Definition: logging.h:205
bool IsPowerOfTwo32(uint32_t value)
Definition: bits.h:77
void * AlignedAlloc(size_t size, size_t alignment)
Definition: allocation.cc:86
void FatalProcessOutOfMemory(const char *message)
char * StrNDup(const char *str, int n)
Definition: allocation.cc:76
int StrLength(const char *string)
Definition: vector.h:147
void MemCopy(void *dest, const void *src, size_t size)
Definition: utils.h:350
void AlignedFree(void *ptr)
Definition: allocation.cc:104
char * StrDup(const char *str)
Definition: allocation.cc:67
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20
#define V8_ALIGNOF(type)
Definition: v8config.h:416