| 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..dc9db903b3b1b5a065a7b1657946b5c4f9fb6268
|
| --- /dev/null
|
| +++ b/base/memory/aligned_memory.h
|
| @@ -0,0 +1,77 @@
|
| +// 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 is a POD type that gives you a portable way to specify static
|
| +// or local stack data of a given alignment and size. 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<sizeof(MyClass), ALIGNOF(MyClass)> my_class;
|
| +//
|
| +// // ... at runtime:
|
| +// new(my_class.void_data()) MyClass();
|
| +//
|
| +// // ... use it:
|
| +// MyClass* mc = my_class.data_as<MyClass>();
|
| +//
|
| +// // ... later, to destruct my_class:
|
| +// my_class.data_as<MyClass>()->MyClass::~MyClass();
|
| +
|
| +#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 {
|
| +
|
| +// AlignedMemory 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 AlignedMemory {};
|
| +
|
| +#define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \
|
| + template <size_t Size> \
|
| + class AlignedMemory<Size, byte_alignment> { \
|
| + public: \
|
| + ALIGNAS(byte_alignment) uint8 data_[Size]; \
|
| + void* void_data() { return reinterpret_cast<void*>(data_); } \
|
| + const void* void_data() const { \
|
| + return reinterpret_cast<const void*>(data_); \
|
| + } \
|
| + template<typename Type> \
|
| + Type* data_as() { return reinterpret_cast<Type*>(void_data()); } \
|
| + template<typename Type> \
|
| + const Type* data_as() const { \
|
| + return reinterpret_cast<const Type*>(void_data()); \
|
| + } \
|
| + private: \
|
| + void* operator new(size_t); \
|
| + void operator delete(void*); \
|
| + }
|
| +
|
| +// Specialization for all alignments is required because MSVC (as of VS 2008)
|
| +// does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param).
|
| +// Greater than 4096 alignment is not supported by some compilers, so 4096 is
|
| +// the maximum specified here.
|
| +BASE_DECL_ALIGNED_MEMORY(1);
|
| +BASE_DECL_ALIGNED_MEMORY(2);
|
| +BASE_DECL_ALIGNED_MEMORY(4);
|
| +BASE_DECL_ALIGNED_MEMORY(8);
|
| +BASE_DECL_ALIGNED_MEMORY(16);
|
| +BASE_DECL_ALIGNED_MEMORY(32);
|
| +BASE_DECL_ALIGNED_MEMORY(64);
|
| +BASE_DECL_ALIGNED_MEMORY(128);
|
| +BASE_DECL_ALIGNED_MEMORY(256);
|
| +BASE_DECL_ALIGNED_MEMORY(512);
|
| +BASE_DECL_ALIGNED_MEMORY(1024);
|
| +BASE_DECL_ALIGNED_MEMORY(2048);
|
| +BASE_DECL_ALIGNED_MEMORY(4096);
|
| +
|
| +} // base
|
| +
|
| +#endif // BASE_MEMORY_ALIGNED_MEMORY_H_
|
|
|