OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/allocation.h" | 5 #include "src/allocation.h" |
6 | 6 |
7 #include <stdlib.h> // For free, malloc. | 7 #include <stdlib.h> // For free, malloc. |
| 8 #include "src/base/bits.h" |
8 #include "src/base/logging.h" | 9 #include "src/base/logging.h" |
9 #include "src/base/platform/platform.h" | 10 #include "src/base/platform/platform.h" |
10 #include "src/utils.h" | 11 #include "src/utils.h" |
11 | 12 |
12 #if V8_LIBC_BIONIC | 13 #if V8_LIBC_BIONIC |
13 #include <malloc.h> // NOLINT | 14 #include <malloc.h> // NOLINT |
14 #endif | 15 #endif |
15 | 16 |
16 namespace v8 { | 17 namespace v8 { |
17 namespace internal { | 18 namespace internal { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 int length = StrLength(str); | 77 int length = StrLength(str); |
77 if (n < length) length = n; | 78 if (n < length) length = n; |
78 char* result = NewArray<char>(length + 1); | 79 char* result = NewArray<char>(length + 1); |
79 MemCopy(result, str, length); | 80 MemCopy(result, str, length); |
80 result[length] = '\0'; | 81 result[length] = '\0'; |
81 return result; | 82 return result; |
82 } | 83 } |
83 | 84 |
84 | 85 |
85 void* AlignedAlloc(size_t size, size_t alignment) { | 86 void* AlignedAlloc(size_t size, size_t alignment) { |
86 DCHECK(IsPowerOf2(alignment) && alignment >= V8_ALIGNOF(void*)); // NOLINT | 87 DCHECK_LE(V8_ALIGNOF(void*), alignment); |
| 88 DCHECK(base::bits::IsPowerOfTwo32(alignment)); |
87 void* ptr; | 89 void* ptr; |
88 #if V8_OS_WIN | 90 #if V8_OS_WIN |
89 ptr = _aligned_malloc(size, alignment); | 91 ptr = _aligned_malloc(size, alignment); |
90 #elif V8_LIBC_BIONIC | 92 #elif V8_LIBC_BIONIC |
91 // posix_memalign is not exposed in some Android versions, so we fall back to | 93 // posix_memalign is not exposed in some Android versions, so we fall back to |
92 // memalign. See http://code.google.com/p/android/issues/detail?id=35391. | 94 // memalign. See http://code.google.com/p/android/issues/detail?id=35391. |
93 ptr = memalign(alignment, size); | 95 ptr = memalign(alignment, size); |
94 #else | 96 #else |
95 if (posix_memalign(&ptr, alignment, size)) ptr = NULL; | 97 if (posix_memalign(&ptr, alignment, size)) ptr = NULL; |
96 #endif | 98 #endif |
97 if (ptr == NULL) FatalProcessOutOfMemory("AlignedAlloc"); | 99 if (ptr == NULL) FatalProcessOutOfMemory("AlignedAlloc"); |
98 return ptr; | 100 return ptr; |
99 } | 101 } |
100 | 102 |
101 | 103 |
102 void AlignedFree(void *ptr) { | 104 void AlignedFree(void *ptr) { |
103 #if V8_OS_WIN | 105 #if V8_OS_WIN |
104 _aligned_free(ptr); | 106 _aligned_free(ptr); |
105 #elif V8_LIBC_BIONIC | 107 #elif V8_LIBC_BIONIC |
106 // Using free is not correct in general, but for V8_LIBC_BIONIC it is. | 108 // Using free is not correct in general, but for V8_LIBC_BIONIC it is. |
107 free(ptr); | 109 free(ptr); |
108 #else | 110 #else |
109 free(ptr); | 111 free(ptr); |
110 #endif | 112 #endif |
111 } | 113 } |
112 | 114 |
113 } } // namespace v8::internal | 115 } } // namespace v8::internal |
OLD | NEW |