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/logging.h" | 8 #include "src/base/logging.h" |
9 #include "src/base/platform/platform.h" | 9 #include "src/base/platform/platform.h" |
10 #include "src/utils.h" | 10 #include "src/utils.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 int length = StrLength(str); | 76 int length = StrLength(str); |
77 if (n < length) length = n; | 77 if (n < length) length = n; |
78 char* result = NewArray<char>(length + 1); | 78 char* result = NewArray<char>(length + 1); |
79 MemCopy(result, str, length); | 79 MemCopy(result, str, length); |
80 result[length] = '\0'; | 80 result[length] = '\0'; |
81 return result; | 81 return result; |
82 } | 82 } |
83 | 83 |
84 | 84 |
85 void* AlignedAlloc(size_t size, size_t alignment) { | 85 void* AlignedAlloc(size_t size, size_t alignment) { |
86 ASSERT(IsPowerOf2(alignment) && alignment >= V8_ALIGNOF(void*)); // NOLINT | 86 DCHECK(IsPowerOf2(alignment) && alignment >= V8_ALIGNOF(void*)); // NOLINT |
87 void* ptr; | 87 void* ptr; |
88 #if V8_OS_WIN | 88 #if V8_OS_WIN |
89 ptr = _aligned_malloc(size, alignment); | 89 ptr = _aligned_malloc(size, alignment); |
90 #elif V8_LIBC_BIONIC | 90 #elif V8_LIBC_BIONIC |
91 // posix_memalign is not exposed in some Android versions, so we fall back to | 91 // 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. | 92 // memalign. See http://code.google.com/p/android/issues/detail?id=35391. |
93 ptr = memalign(alignment, size); | 93 ptr = memalign(alignment, size); |
94 #else | 94 #else |
95 if (posix_memalign(&ptr, alignment, size)) ptr = NULL; | 95 if (posix_memalign(&ptr, alignment, size)) ptr = NULL; |
96 #endif | 96 #endif |
97 if (ptr == NULL) FatalProcessOutOfMemory("AlignedAlloc"); | 97 if (ptr == NULL) FatalProcessOutOfMemory("AlignedAlloc"); |
98 return ptr; | 98 return ptr; |
99 } | 99 } |
100 | 100 |
101 | 101 |
102 void AlignedFree(void *ptr) { | 102 void AlignedFree(void *ptr) { |
103 #if V8_OS_WIN | 103 #if V8_OS_WIN |
104 _aligned_free(ptr); | 104 _aligned_free(ptr); |
105 #elif V8_LIBC_BIONIC | 105 #elif V8_LIBC_BIONIC |
106 // Using free is not correct in general, but for V8_LIBC_BIONIC it is. | 106 // Using free is not correct in general, but for V8_LIBC_BIONIC it is. |
107 free(ptr); | 107 free(ptr); |
108 #else | 108 #else |
109 free(ptr); | 109 free(ptr); |
110 #endif | 110 #endif |
111 } | 111 } |
112 | 112 |
113 } } // namespace v8::internal | 113 } } // namespace v8::internal |
OLD | NEW |