#ifndef SQUID_IPC_MEM_FLEXIBLE_ARRAY_H
#define SQUID_IPC_MEM_FLEXIBLE_ARRAY_H
-// sometimes required for placement-new operator to be declared
+#include <cstddef>
#include <new>
namespace Ipc
{
public:
explicit FlexibleArray(const int capacity) {
- if (capacity > 1) // the first item is initialized automatically
- new (raw()+1) Item[capacity-1];
+ new (raw()) Item[capacity];
}
- Item &operator [](const int idx) { return items[idx]; }
- const Item &operator [](const int idx) const { return items[idx]; }
+ Item &operator [](const int idx) { return *(raw() + idx); }
- //const Item *operator ()() const { return items; }
- //Item *operator ()() { return items; }
-
- Item *raw() { return items; }
+ Item *raw() { return reinterpret_cast<Item*>(&start_); }
private:
- Item items[1]; // ensures proper alignment of array elements
+ alignas(Item) std::byte start_; ///< the first byte of the first array item
};
} // namespace Mem