Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/allocation.h" |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 6 | |
| 7 | #include <stdlib.h> // For free, malloc. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "src/base/bits.h" |
| 9 | #include "src/base/logging.h" |
| 10 | #include "src/base/platform/platform.h" |
| 11 | #include "src/utils.h" |
| 12 | |
| 13 | #if V8_LIBC_BIONIC |
| 14 | #include <malloc.h> // NOLINT |
| 15 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16 | |
| 17 | namespace v8 { |
| 18 | namespace internal { |
| 19 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 20 | void* Malloced::New(size_t size) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 21 | void* result = malloc(size); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 22 | if (result == NULL) { |
| 23 | v8::internal::FatalProcessOutOfMemory("Malloced operator new"); |
| 24 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 25 | return result; |
| 26 | } |
| 27 | |
| 28 | |
| 29 | void Malloced::Delete(void* p) { |
| 30 | free(p); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | void Malloced::FatalProcessOutOfMemory() { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 35 | v8::internal::FatalProcessOutOfMemory("Out of memory"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 68 | int length = StrLength(str); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 69 | char* result = NewArray<char>(length + 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 70 | MemCopy(result, str, length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 71 | result[length] = '\0'; |
| 72 | return result; |
| 73 | } |
| 74 | |
| 75 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 76 | char* StrNDup(const char* str, int n) { |
| 77 | int length = StrLength(str); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 78 | if (n < length) length = n; |
| 79 | char* result = NewArray<char>(length + 1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 80 | MemCopy(result, str, length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 81 | result[length] = '\0'; |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 86 | void* AlignedAlloc(size_t size, size_t alignment) { |
| 87 | DCHECK_LE(V8_ALIGNOF(void*), alignment); |
| 88 | DCHECK(base::bits::IsPowerOfTwo32(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; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | } } // namespace v8::internal |