Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: base/memory/aligned_memory.h

Issue 9186057: Add ALIGNAS and ALIGNOF macros to ensure proper alignment of StaticMemorySingletonTraits (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: willchan feedback Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/lazy_instance_unittest.cc ('k') | base/memory/aligned_memory_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // AlignedMemory is a POD type that gives you a portable way to specify static
6 // or local stack data of a given alignment and size. For example, if you need
7 // static storage for a class, but you want manual control over when the object
8 // is constructed and destructed (you don't want static initialization and
9 // destruction), use AlignedMemory:
10 //
11 // static AlignedMemory<sizeof(MyClass), ALIGNOF(MyClass)> my_class;
12 //
13 // // ... at runtime:
14 // new(my_class.void_data()) MyClass();
15 //
16 // // ... use it:
17 // MyClass* mc = my_class.data_as<MyClass>();
18 //
19 // // ... later, to destruct my_class:
20 // my_class.data_as<MyClass>()->MyClass::~MyClass();
21
22 #ifndef BASE_MEMORY_ALIGNED_MEMORY_H_
23 #define BASE_MEMORY_ALIGNED_MEMORY_H_
24 #pragma once
25
26 #include "base/basictypes.h"
27 #include "base/compiler_specific.h"
28 #include "base/logging.h"
29
30 namespace base {
31
32 // AlignedMemory is specialized for all supported alignments.
33 // Make sure we get a compiler error if someone uses an unsupported alignment.
34 template <size_t Size, size_t ByteAlignment>
35 struct AlignedMemory {};
36
37 #define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \
38 template <size_t Size> \
39 class AlignedMemory<Size, byte_alignment> { \
40 public: \
41 ALIGNAS(byte_alignment) uint8 data_[Size]; \
42 void* void_data() { return reinterpret_cast<void*>(data_); } \
43 const void* void_data() const { \
44 return reinterpret_cast<const void*>(data_); \
45 } \
46 template<typename Type> \
47 Type* data_as() { return reinterpret_cast<Type*>(void_data()); } \
48 template<typename Type> \
49 const Type* data_as() const { \
50 return reinterpret_cast<const Type*>(void_data()); \
51 } \
52 private: \
53 void* operator new(size_t); \
54 void operator delete(void*); \
55 }
56
57 // Specialization for all alignments is required because MSVC (as of VS 2008)
58 // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param).
59 // Greater than 4096 alignment is not supported by some compilers, so 4096 is
60 // the maximum specified here.
61 BASE_DECL_ALIGNED_MEMORY(1);
62 BASE_DECL_ALIGNED_MEMORY(2);
63 BASE_DECL_ALIGNED_MEMORY(4);
64 BASE_DECL_ALIGNED_MEMORY(8);
65 BASE_DECL_ALIGNED_MEMORY(16);
66 BASE_DECL_ALIGNED_MEMORY(32);
67 BASE_DECL_ALIGNED_MEMORY(64);
68 BASE_DECL_ALIGNED_MEMORY(128);
69 BASE_DECL_ALIGNED_MEMORY(256);
70 BASE_DECL_ALIGNED_MEMORY(512);
71 BASE_DECL_ALIGNED_MEMORY(1024);
72 BASE_DECL_ALIGNED_MEMORY(2048);
73 BASE_DECL_ALIGNED_MEMORY(4096);
74
75 } // base
76
77 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_
OLDNEW
« no previous file with comments | « base/lazy_instance_unittest.cc ('k') | base/memory/aligned_memory_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698