[ox/std] Fix VectorMemMap sizeOf

This commit is contained in:
Gary Talent 2022-12-03 01:42:23 -06:00
parent a946f8dae1
commit 86c33019fe

View File

@ -34,8 +34,10 @@ constexpr auto sizeOf(const VectorMemMap<PlatSpec> *t) noexcept {
std::size_t size = 0; std::size_t size = 0;
if (t->smallVecSize) { if (t->smallVecSize) {
size += t->smallVecSize; size += t->smallVecSize;
size += padding(size, PlatSpec::alignOf(t->size)); size += padding(size, PlatSpec::alignOf(t->allocator));
} }
size += sizeof(t->allocator);
size += padding(size, PlatSpec::alignOf(t->size));
size += sizeof(t->size); size += sizeof(t->size);
size += padding(size, PlatSpec::alignOf(t->cap)); size += padding(size, PlatSpec::alignOf(t->cap));
size += sizeof(t->cap); size += sizeof(t->cap);
@ -51,10 +53,22 @@ constexpr auto alignOf(const VectorMemMap<PlatSpec>&) noexcept {
return PlatSpec::alignOf(i); return PlatSpec::alignOf(i);
} }
template<typename PlatSpec, typename T>
constexpr ox::Error pad(Writer_c auto *w, const T *v) noexcept {
const auto a = PlatSpec::alignOf(*v);
const auto excess = w->tellp() % a;
if (excess) {
return w->write(nullptr, a - excess);
} else {
return {};
}
}
template<typename PlatSpec> template<typename PlatSpec>
constexpr ox::Error serialize(Writer_c auto *buff, const VectorMemMap<PlatSpec> &vm) noexcept { constexpr ox::Error serialize(Writer_c auto *buff, const VectorMemMap<PlatSpec> &vm) noexcept {
oxReturnError(buff->write(nullptr, vm.smallVecSize)); oxReturnError(buff->write(nullptr, vm.smallVecSize));
oxReturnError(serialize(buff, PlatSpec::correctEndianness(vm.allocator))); oxReturnError(serialize(buff, PlatSpec::correctEndianness(vm.allocator)));
oxReturnError(pad<PlatSpec>(buff, &vm.size));
oxReturnError(serialize(buff, PlatSpec::correctEndianness(vm.size))); oxReturnError(serialize(buff, PlatSpec::correctEndianness(vm.size)));
oxReturnError(serialize(buff, PlatSpec::correctEndianness(vm.cap))); oxReturnError(serialize(buff, PlatSpec::correctEndianness(vm.cap)));
oxReturnError(serialize(buff, PlatSpec::correctEndianness(vm.items))); oxReturnError(serialize(buff, PlatSpec::correctEndianness(vm.items)));