v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
allocation.cc
Go to the documentation of this file.
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #include "allocation.h"
29 
30 #include <stdlib.h> // For free, malloc.
31 #include "checks.h"
32 #include "platform.h"
33 #include "utils.h"
34 
35 #if V8_LIBC_BIONIC
36 #include <malloc.h> // NOLINT
37 #endif
38 
39 namespace v8 {
40 namespace internal {
41 
42 void* Malloced::New(size_t size) {
43  void* result = malloc(size);
44  if (result == NULL) {
45  v8::internal::FatalProcessOutOfMemory("Malloced operator new");
46  }
47  return result;
48 }
49 
50 
51 void Malloced::Delete(void* p) {
52  free(p);
53 }
54 
55 
58 }
59 
60 
61 #ifdef DEBUG
62 
63 static void* invalid = static_cast<void*>(NULL);
64 
65 void* Embedded::operator new(size_t size) {
66  UNREACHABLE();
67  return invalid;
68 }
69 
70 
71 void Embedded::operator delete(void* p) {
72  UNREACHABLE();
73 }
74 
75 
76 void* AllStatic::operator new(size_t size) {
77  UNREACHABLE();
78  return invalid;
79 }
80 
81 
82 void AllStatic::operator delete(void* p) {
83  UNREACHABLE();
84 }
85 
86 #endif
87 
88 
89 char* StrDup(const char* str) {
90  int length = StrLength(str);
91  char* result = NewArray<char>(length + 1);
92  OS::MemCopy(result, str, length);
93  result[length] = '\0';
94  return result;
95 }
96 
97 
98 char* StrNDup(const char* str, int n) {
99  int length = StrLength(str);
100  if (n < length) length = n;
101  char* result = NewArray<char>(length + 1);
102  OS::MemCopy(result, str, length);
103  result[length] = '\0';
104  return result;
105 }
106 
107 
108 void* AlignedAlloc(size_t size, size_t alignment) {
109  ASSERT(IsPowerOf2(alignment) && alignment >= V8_ALIGNOF(void*)); // NOLINT
110  void* ptr;
111 #if V8_OS_WIN
112  ptr = _aligned_malloc(size, alignment);
113 #elif V8_LIBC_BIONIC
114  // posix_memalign is not exposed in some Android versions, so we fall back to
115  // memalign. See http://code.google.com/p/android/issues/detail?id=35391.
116  ptr = memalign(alignment, size);
117 #else
118  if (posix_memalign(&ptr, alignment, size)) ptr = NULL;
119 #endif
120  if (ptr == NULL) FatalProcessOutOfMemory("AlignedAlloc");
121  return ptr;
122 }
123 
124 
125 void AlignedFree(void *ptr) {
126 #if V8_OS_WIN
127  _aligned_free(ptr);
128 #elif V8_LIBC_BIONIC
129  // Using free is not correct in general, but for V8_LIBC_BIONIC it is.
130  free(ptr);
131 #else
132  free(ptr);
133 #endif
134 }
135 
136 } } // namespace v8::internal
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
Definition: flags.cc:269
static void Delete(void *p)
Definition: allocation.cc:51
static void * New(size_t size)
Definition: allocation.cc:42
char * StrNDup(const char *str, int n)
Definition: allocation.cc:98
static void FatalProcessOutOfMemory()
Definition: allocation.cc:56
#define ASSERT(condition)
Definition: checks.h:329
#define UNREACHABLE()
Definition: checks.h:52
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object size
Definition: flags.cc:211
static void MemCopy(void *dest, const void *src, size_t size)
Definition: platform.h:399
void FatalProcessOutOfMemory(const char *message)
#define V8_ALIGNOF(type)
Definition: v8config.h:471
bool IsPowerOf2(T x)
Definition: utils.h:51
int StrLength(const char *string)
Definition: utils.h:253
void AlignedFree(void *ptr)
Definition: allocation.cc:125
void * AlignedAlloc(size_t size, size_t alignment)
Definition: allocation.cc:108
char * StrDup(const char *str)
Definition: allocation.cc:89