Index: base/allocator/win_allocator.cc |
diff --git a/base/allocator/win_allocator.cc b/base/allocator/win_allocator.cc |
index 8ae653a690ed033b5e708e7021f98998082f0ca1..5367509ba4f3abc855ead1b428231581ca16506d 100644 |
--- a/base/allocator/win_allocator.cc |
+++ b/base/allocator/win_allocator.cc |
@@ -47,4 +47,26 @@ size_t win_heap_msize(void* ptr) { |
return HeapSize(win_heap, 0, ptr); |
} |
+void* win_heap_memalign(size_t alignment, size_t size) { |
+ // Reserve enough space to ensure we can align and set aligned_ptr[-1] to the |
+ // original allocation for use with win_heap_memalign_free() later. |
+ size_t allocation_size = size + (alignment - 1) + sizeof(void*); |
+ |
+ // Check for overflow. Alignment and size are checked in allocator_shim. |
+ DCHECK_LT(size, allocation_size); |
+ DCHECK_LT(alignment, allocation_size); |
+ |
+ void* ptr = win_heap_malloc(allocation_size); |
+ char* aligned_ptr = static_cast<char*>(ptr) + sizeof(void*); |
+ aligned_ptr += |
+ alignment - reinterpret_cast<uintptr_t>(aligned_ptr) & (alignment - 1); |
+ |
+ reinterpret_cast<void**>(aligned_ptr)[-1] = ptr; |
+ return aligned_ptr; |
+} |
+ |
+void win_heap_memalign_free(void* ptr) { |
+ win_heap_free(static_cast<void**>(ptr)[-1]); |
+} |
+ |
} // extern "C" |