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

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

Issue 1039823002: Add base::LazyInstance (Closed) Base URL: https://chromium.googlesource.com/chromium/mini_chromium@master
Patch Set: Created 5 years, 9 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
« no previous file with comments | « base/lazy_instance.cc ('k') | base/memory/aligned_memory.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 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 // Alternatively, a runtime sized aligned allocation can be created:
23 //
24 // float* my_array = static_cast<float*>(AlignedAlloc(size, alignment));
25 //
26 // // ... later, to release the memory:
27 // AlignedFree(my_array);
28 //
29 // Or using scoped_ptr:
30 //
31 // scoped_ptr<float, AlignedFreeDeleter> my_array(
32 // static_cast<float*>(AlignedAlloc(size, alignment)));
33
34 #ifndef BASE_MEMORY_ALIGNED_MEMORY_H_
35 #define BASE_MEMORY_ALIGNED_MEMORY_H_
36
37 #include "base/basictypes.h"
38 #include "base/compiler_specific.h"
39
40 #if defined(COMPILER_MSVC)
41 #include <malloc.h>
42 #else
43 #include <stdlib.h>
44 #endif
45
46 namespace base {
47
48 // AlignedMemory is specialized for all supported alignments.
49 // Make sure we get a compiler error if someone uses an unsupported alignment.
50 template <size_t Size, size_t ByteAlignment>
51 struct AlignedMemory {};
52
53 #define BASE_DECL_ALIGNED_MEMORY(byte_alignment) \
54 template <size_t Size> \
55 class AlignedMemory<Size, byte_alignment> { \
56 public: \
57 ALIGNAS(byte_alignment) uint8_t data_[Size]; \
58 void* void_data() { return static_cast<void*>(data_); } \
59 const void* void_data() const { \
60 return static_cast<const void*>(data_); \
61 } \
62 template<typename Type> \
63 Type* data_as() { return static_cast<Type*>(void_data()); } \
64 template<typename Type> \
65 const Type* data_as() const { \
66 return static_cast<const Type*>(void_data()); \
67 } \
68 private: \
69 void* operator new(size_t); \
70 void operator delete(void*); \
71 }
72
73 // Specialization for all alignments is required because MSVC (as of VS 2008)
74 // does not understand ALIGNAS(ALIGNOF(Type)) or ALIGNAS(template_param).
75 // Greater than 4096 alignment is not supported by some compilers, so 4096 is
76 // the maximum specified here.
77 BASE_DECL_ALIGNED_MEMORY(1);
78 BASE_DECL_ALIGNED_MEMORY(2);
79 BASE_DECL_ALIGNED_MEMORY(4);
80 BASE_DECL_ALIGNED_MEMORY(8);
81 BASE_DECL_ALIGNED_MEMORY(16);
82 BASE_DECL_ALIGNED_MEMORY(32);
83 BASE_DECL_ALIGNED_MEMORY(64);
84 BASE_DECL_ALIGNED_MEMORY(128);
85 BASE_DECL_ALIGNED_MEMORY(256);
86 BASE_DECL_ALIGNED_MEMORY(512);
87 BASE_DECL_ALIGNED_MEMORY(1024);
88 BASE_DECL_ALIGNED_MEMORY(2048);
89 BASE_DECL_ALIGNED_MEMORY(4096);
90
91 #undef BASE_DECL_ALIGNED_MEMORY
92
93 void* AlignedAlloc(size_t size, size_t alignment);
94
95 inline void AlignedFree(void* ptr) {
96 #if defined(COMPILER_MSVC)
97 _aligned_free(ptr);
98 #else
99 free(ptr);
100 #endif
101 }
102
103 // Deleter for use with scoped_ptr. E.g., use as
104 // scoped_ptr<Foo, base::AlignedFreeDeleter> foo;
105 struct AlignedFreeDeleter {
106 inline void operator()(void* ptr) const {
107 AlignedFree(ptr);
108 }
109 };
110
111 } // namespace base
112
113 #endif // BASE_MEMORY_ALIGNED_MEMORY_H_
OLDNEW
« no previous file with comments | « base/lazy_instance.cc ('k') | base/memory/aligned_memory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698