/* * Copyright (C) 2014 Cloudius Systems, Ltd. */ #ifndef VLA_HH_ #define VLA_HH_ #include #include #include #include // Some C APIs have a structure with a variable length array at the end. // This is a helper function to help allocate it. // // for a structure // // struct xx { int a; float b[0]; }; // // use // // make_struct_with_vla(&xx::b, number_of_bs); // // to allocate it. // template inline std::unique_ptr make_struct_with_vla(E S::*last, size_t nr) { auto fake = reinterpret_cast(0); size_t offset = reinterpret_cast(&(fake->*last)); size_t element_size = sizeof((fake->*last)[0]); assert(offset == sizeof(S)); auto p = ::operator new(offset + element_size * nr); return std::unique_ptr(new (p) S()); } #endif /* VLA_HH_ */