Index: src/allocation.cc |
diff --git a/src/allocation.cc b/src/allocation.cc |
index 69edf6906cf2d03601af6bf90a98ae798c836316..a2b2b9a972a255cc9b859b21d318438aa56003b0 100644 |
--- a/src/allocation.cc |
+++ b/src/allocation.cc |
@@ -32,6 +32,16 @@ |
#include "platform.h" |
#include "utils.h" |
+#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
|
+#include <malloc.h> // NOLINT |
+#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
|
+#endif |
+ |
+#if V8_OS_POSIX |
+#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.
|
+#define V8_HAS_POSIX_MEMALIGN 1 |
+#endif |
+ |
namespace v8 { |
namespace internal { |
@@ -100,4 +110,48 @@ char* StrNDup(const char* str, int n) { |
return result; |
} |
+ |
+#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
|
+static void* AlignPointer(void* ptr, size_t alignment) { |
+ uintptr_t mask = uintptr_t(alignment) - 1; |
+ uintptr_t aligned = (reinterpret_cast<uintptr_t>(ptr) + mask) & ~mask; |
+ return reinterpret_cast<void*>(aligned); |
+} |
+ |
+ |
+// We store a pointer to the real start of the buffer just in front of the |
+// aligned buffer our clients see. |
+void*& BufferStart(void* ptr) { |
+ return reinterpret_cast<void**>(ptr)[-1]; |
+} |
+#endif |
+ |
+ |
+void* AlignedMalloc(size_t size, size_t alignment) { |
+ ASSERT(IsPowerOf2(alignment) && alignment >= V8_ALIGNOF(void*)); // NOLINT |
+ void* ptr; |
+#if V8_HAS__ALIGNED_MALLOC |
+ ptr = _aligned_malloc(size, alignment); |
+#elif V8_HAS_POSIX_MEMALIGN |
+ if (posix_memalign(&ptr, alignment, size)) ptr = NULL; |
+#else |
+ void* buffer = malloc(size + sizeof(void*) + alignment - 1); // NOLINT |
+ ptr = AlignPointer(reinterpret_cast<void**>(buffer) + 1, alignment); |
+ if (ptr != NULL) BufferStart(ptr) = buffer; |
+#endif |
+ if (ptr == NULL) FatalProcessOutOfMemory("AlignedMalloc"); |
+ return ptr; |
+} |
+ |
+ |
+void AlignedFree(void *ptr) { |
+#if V8_HAS__ALIGNED_MALLOC |
+ _aligned_free(ptr); |
+#elif V8_HAS_POSIX_MEMALIGN |
+ free(ptr); |
+#else |
+ if (ptr != NULL) free(BufferStart(ptr)); |
+#endif |
+} |
+ |
} } // namespace v8::internal |