Index: base/memory/aligned_memory.h |
diff --git a/base/memory/aligned_memory.h b/base/memory/aligned_memory.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..812a108cec62fcc9446808f2d354d0fac84fef60 |
--- /dev/null |
+++ b/base/memory/aligned_memory.h |
@@ -0,0 +1,79 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// AlignedMemory and RawAlignedMemory give you portable ways to specify aligned |
+// static or local stack data. For example, if you need static storage for a |
+// class, but you want manual control over when the object is constructed and |
+// destructed (you don't want static initialization and destruction), use |
+// AlignedMemory: |
+// |
+// static AlignedMemory<MyClass> my_class; |
+// |
+// // ... at runtime: |
+// new(my_class.data()) MyClass(); |
+// |
+// // ... later, to destruct my_class: |
+// my_class.data()->MyClass::~MyClass(); |
+// |
+// RawAlignedMemory just contains a uint8 buffer with a specified size and |
+// memory alignment. |
+ |
+ |
+#ifndef BASE_MEMORY_ALIGNED_MEMORY_H_ |
+#define BASE_MEMORY_ALIGNED_MEMORY_H_ |
+#pragma once |
+ |
+#include "base/basictypes.h" |
+#include "base/compiler_specific.h" |
+#include "base/logging.h" |
+ |
+namespace base { |
+ |
+// RawAlignedMemory is specialized for all supported alignments. |
+// Make sure we get a compiler error if someone uses an unsupported alignment. |
+template <size_t Size, size_t ByteAlignment> |
+struct RawAlignedMemory {}; |
+ |
+#define BASE_DECL_RAW_ALIGNED_MEMORY(byte_alignment) \ |
+ template <size_t Size> \ |
+ struct RawAlignedMemory<Size, byte_alignment> { \ |
+ ALIGNAS(byte_alignment) uint8 data_[Size]; \ |
+ void* operator new(size_t size) { \ |
Jeffrey Yasskin (google)
2012/02/22 18:38:42
I'd make these private too, to try to cause a comp
jbates
2012/02/22 19:37:15
Done.
|
+ NOTREACHED() << "new/delete not supported"; \ |
+ return NULL; \ |
+ } \ |
+ void operator delete(void *ptr) { } \ |
+ } |
+ |
+// Specialization for all alignments is required because MSVC does not |
Jeffrey Yasskin (google)
2012/02/22 18:38:42
Consider including an MSVC version number here, so
jbates
2012/02/22 19:37:15
Done.
|
+// understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param). |
+BASE_DECL_RAW_ALIGNED_MEMORY(1); |
+BASE_DECL_RAW_ALIGNED_MEMORY(2); |
+BASE_DECL_RAW_ALIGNED_MEMORY(4); |
+BASE_DECL_RAW_ALIGNED_MEMORY(8); |
+BASE_DECL_RAW_ALIGNED_MEMORY(16); |
+BASE_DECL_RAW_ALIGNED_MEMORY(32); |
+BASE_DECL_RAW_ALIGNED_MEMORY(64); |
+BASE_DECL_RAW_ALIGNED_MEMORY(128); |
+BASE_DECL_RAW_ALIGNED_MEMORY(256); |
+BASE_DECL_RAW_ALIGNED_MEMORY(512); |
+BASE_DECL_RAW_ALIGNED_MEMORY(1024); |
+BASE_DECL_RAW_ALIGNED_MEMORY(2048); |
+BASE_DECL_RAW_ALIGNED_MEMORY(4096); |
+ |
+template <typename Type> |
+struct AlignedMemory : RawAlignedMemory<sizeof(Type), ALIGNOF(Type)> { |
+ void* void_data() { |
+ return reinterpret_cast<void*>( |
+ RawAlignedMemory<sizeof(Type), ALIGNOF(Type)>::data_); |
Jeffrey Yasskin (google)
2012/02/22 18:38:42
"this->data_" should also work.
jbates
2012/02/22 19:37:15
Done.
|
+ } |
+ |
+ Type* data() { |
+ return reinterpret_cast<Type*>(void_data()); |
+ } |
+}; |
+ |
+} // base |
+ |
+#endif // BASE_MEMORY_ALIGNED_MEMORY_H_ |