OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 14 matching lines...) Expand all Loading... |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include "allocation.h" | 28 #include "allocation.h" |
29 | 29 |
30 #include <stdlib.h> // For free, malloc. | 30 #include <stdlib.h> // For free, malloc. |
31 #include "checks.h" | 31 #include "checks.h" |
32 #include "platform.h" | 32 #include "platform.h" |
33 #include "utils.h" | 33 #include "utils.h" |
34 | 34 |
| 35 #if V8_LIBC_BIONIC |
| 36 #include <malloc.h> // NOLINT |
| 37 #endif |
| 38 |
35 namespace v8 { | 39 namespace v8 { |
36 namespace internal { | 40 namespace internal { |
37 | 41 |
38 void* Malloced::New(size_t size) { | 42 void* Malloced::New(size_t size) { |
39 void* result = malloc(size); | 43 void* result = malloc(size); |
40 if (result == NULL) { | 44 if (result == NULL) { |
41 v8::internal::FatalProcessOutOfMemory("Malloced operator new"); | 45 v8::internal::FatalProcessOutOfMemory("Malloced operator new"); |
42 } | 46 } |
43 return result; | 47 return result; |
44 } | 48 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 | 97 |
94 char* StrNDup(const char* str, int n) { | 98 char* StrNDup(const char* str, int n) { |
95 int length = StrLength(str); | 99 int length = StrLength(str); |
96 if (n < length) length = n; | 100 if (n < length) length = n; |
97 char* result = NewArray<char>(length + 1); | 101 char* result = NewArray<char>(length + 1); |
98 OS::MemCopy(result, str, length); | 102 OS::MemCopy(result, str, length); |
99 result[length] = '\0'; | 103 result[length] = '\0'; |
100 return result; | 104 return result; |
101 } | 105 } |
102 | 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 |
103 } } // namespace v8::internal | 136 } } // namespace v8::internal |
OLD | NEW |