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_CC_MSVC | |
Benedikt Meurer
2014/01/09 10:42:52
What about Cygwin? Does Cygwin have posix_memalign
Sven Panne
2014/01/09 12:04:19
Cygwin seems to have posix_memalign, and hopefully
| |
36 #include <malloc.h> // NOLINT | |
37 #define V8_HAS__ALIGNED_MALLOC 1 | |
Benedikt Meurer
2014/01/09 10:42:52
Shouldn't these V8_HAS_* macros live in v8config.h
Sven Panne
2014/01/09 12:04:19
I don't have a strong opinion about that, up to no
| |
38 #endif | |
39 | |
40 #if V8_OS_POSIX | |
41 #include <stdlib.h> // NOLINT | |
Benedikt Meurer
2014/01/09 10:42:52
stdlib.h is already included above.
Sven Panne
2014/01/09 12:04:19
Done.
| |
42 #define V8_HAS_POSIX_MEMALIGN 1 | |
43 #endif | |
44 | |
35 namespace v8 { | 45 namespace v8 { |
36 namespace internal { | 46 namespace internal { |
37 | 47 |
38 void* Malloced::New(size_t size) { | 48 void* Malloced::New(size_t size) { |
39 void* result = malloc(size); | 49 void* result = malloc(size); |
40 if (result == NULL) { | 50 if (result == NULL) { |
41 v8::internal::FatalProcessOutOfMemory("Malloced operator new"); | 51 v8::internal::FatalProcessOutOfMemory("Malloced operator new"); |
42 } | 52 } |
43 return result; | 53 return result; |
44 } | 54 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 | 103 |
94 char* StrNDup(const char* str, int n) { | 104 char* StrNDup(const char* str, int n) { |
95 int length = StrLength(str); | 105 int length = StrLength(str); |
96 if (n < length) length = n; | 106 if (n < length) length = n; |
97 char* result = NewArray<char>(length + 1); | 107 char* result = NewArray<char>(length + 1); |
98 OS::MemCopy(result, str, length); | 108 OS::MemCopy(result, str, length); |
99 result[length] = '\0'; | 109 result[length] = '\0'; |
100 return result; | 110 return result; |
101 } | 111 } |
102 | 112 |
113 | |
114 #if !V8_HAS__ALIGNED_MALLOC && !V8_HAS_POSIX_MEMALIGN | |
Benedikt Meurer
2014/01/09 10:42:52
This looks weird. Can't we inline these trivial fu
Sven Panne
2014/01/09 12:04:19
I explicitly outlined them, and I actually think i
| |
115 static void* AlignPointer(void* ptr, size_t alignment) { | |
116 uintptr_t mask = uintptr_t(alignment) - 1; | |
117 uintptr_t aligned = (reinterpret_cast<uintptr_t>(ptr) + mask) & ~mask; | |
118 return reinterpret_cast<void*>(aligned); | |
119 } | |
120 | |
121 | |
122 // We store a pointer to the real start of the buffer just in front of the | |
123 // aligned buffer our clients see. | |
124 void*& BufferStart(void* ptr) { | |
125 return reinterpret_cast<void**>(ptr)[-1]; | |
126 } | |
127 #endif | |
128 | |
129 | |
130 void* AlignedMalloc(size_t size, size_t alignment) { | |
131 ASSERT(IsPowerOf2(alignment) && alignment >= V8_ALIGNOF(void*)); // NOLINT | |
132 void* ptr; | |
133 #if V8_HAS__ALIGNED_MALLOC | |
134 ptr = _aligned_malloc(size, alignment); | |
135 #elif V8_HAS_POSIX_MEMALIGN | |
136 if (posix_memalign(&ptr, alignment, size)) ptr = NULL; | |
137 #else | |
138 void* buffer = malloc(size + sizeof(void*) + alignment - 1); // NOLINT | |
139 ptr = AlignPointer(reinterpret_cast<void**>(buffer) + 1, alignment); | |
140 if (ptr != NULL) BufferStart(ptr) = buffer; | |
141 #endif | |
142 if (ptr == NULL) FatalProcessOutOfMemory("AlignedMalloc"); | |
143 return ptr; | |
144 } | |
145 | |
146 | |
147 void AlignedFree(void *ptr) { | |
148 #if V8_HAS__ALIGNED_MALLOC | |
149 _aligned_free(ptr); | |
150 #elif V8_HAS_POSIX_MEMALIGN | |
151 free(ptr); | |
152 #else | |
153 if (ptr != NULL) free(BufferStart(ptr)); | |
154 #endif | |
155 } | |
156 | |
103 } } // namespace v8::internal | 157 } } // namespace v8::internal |
OLD | NEW |