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
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.
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.
28 #include "allocation.h"
30 #include <stdlib.h> // For free, malloc.
36 #include <malloc.h> // NOLINT
42 void* Malloced::New(size_t size) {
43 void* result = malloc(size);
45 v8::internal::FatalProcessOutOfMemory("Malloced operator new");
51 void Malloced::Delete(void* p) {
56 void Malloced::FatalProcessOutOfMemory() {
57 v8::internal::FatalProcessOutOfMemory("Out of memory");
63 static void* invalid = static_cast<void*>(NULL);
65 void* Embedded::operator new(size_t size) {
71 void Embedded::operator delete(void* p) {
76 void* AllStatic::operator new(size_t size) {
82 void AllStatic::operator delete(void* p) {
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';
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';
108 void* AlignedAlloc(size_t size, size_t alignment) {
109 ASSERT(IsPowerOf2(alignment) && alignment >= V8_ALIGNOF(void*)); // NOLINT
112 ptr = _aligned_malloc(size, alignment);
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);
118 if (posix_memalign(&ptr, alignment, size)) ptr = NULL;
120 if (ptr == NULL) FatalProcessOutOfMemory("AlignedAlloc");
125 void AlignedFree(void *ptr) {
129 // Using free is not correct in general, but for V8_LIBC_BIONIC it is.
136 } } // namespace v8::internal