1. 程式人生 > >chrome 中在棧上建立指定位元組對齊的POD型別的static資料的一種可移植的方法

chrome 中在棧上建立指定位元組對齊的POD型別的static資料的一種可移植的方法

// 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();
//
// Alternatively, a runtime sized aligned allocation can be created:
//
//   float* my_array = static_cast<float*>(AlignedAlloc(size, alignment));
//
//   // ... later, to release the memory:
//   AlignedFree(my_array);
//
// Or using scoped_ptr_malloc:
//
//   scoped_ptr_malloc<float, ScopedPtrAlignedFree> my_array(
//       static_cast<float*>(AlignedAlloc(size, alignment)));