prevector: Avoid unnamed struct, which is a GNU extension

pull/764/head
DesWurstes 6 years ago committed by fanquake
parent afed2e98b0
commit 5d4999951e
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1

@ -152,7 +152,7 @@ private:
struct { struct {
char* indirect; char* indirect;
size_type capacity; size_type capacity;
}; } indirect_contents;
}; };
#pragma pack(pop) #pragma pack(pop)
alignas(char*) direct_or_indirect _union = {}; alignas(char*) direct_or_indirect _union = {};
@ -163,8 +163,8 @@ private:
T* direct_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.direct) + pos; } T* direct_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.direct) + pos; }
const T* direct_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.direct) + pos; } const T* direct_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.direct) + pos; }
T* indirect_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.indirect) + pos; } T* indirect_ptr(difference_type pos) { return reinterpret_cast<T*>(_union.indirect_contents.indirect) + pos; }
const T* indirect_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.indirect) + pos; } const T* indirect_ptr(difference_type pos) const { return reinterpret_cast<const T*>(_union.indirect_contents.indirect) + pos; }
bool is_direct() const { return _size <= N; } bool is_direct() const { return _size <= N; }
void change_capacity(size_type new_capacity) { void change_capacity(size_type new_capacity) {
@ -182,17 +182,17 @@ private:
/* FIXME: Because malloc/realloc here won't call new_handler if allocation fails, assert /* FIXME: Because malloc/realloc here won't call new_handler if allocation fails, assert
success. These should instead use an allocator or new/delete so that handlers success. These should instead use an allocator or new/delete so that handlers
are called as necessary, but performance would be slightly degraded by doing so. */ are called as necessary, but performance would be slightly degraded by doing so. */
_union.indirect = static_cast<char*>(realloc(_union.indirect, ((size_t)sizeof(T)) * new_capacity)); _union.indirect_contents.indirect = static_cast<char*>(realloc(_union.indirect_contents.indirect, ((size_t)sizeof(T)) * new_capacity));
assert(_union.indirect); assert(_union.indirect_contents.indirect);
_union.capacity = new_capacity; _union.indirect_contents.capacity = new_capacity;
} else { } else {
char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity)); char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
assert(new_indirect); assert(new_indirect);
T* src = direct_ptr(0); T* src = direct_ptr(0);
T* dst = reinterpret_cast<T*>(new_indirect); T* dst = reinterpret_cast<T*>(new_indirect);
memcpy(dst, src, size() * sizeof(T)); memcpy(dst, src, size() * sizeof(T));
_union.indirect = new_indirect; _union.indirect_contents.indirect = new_indirect;
_union.capacity = new_capacity; _union.indirect_contents.capacity = new_capacity;
_size += N + 1; _size += N + 1;
} }
} }
@ -301,7 +301,7 @@ public:
if (is_direct()) { if (is_direct()) {
return N; return N;
} else { } else {
return _union.capacity; return _union.indirect_contents.capacity;
} }
} }
@ -468,8 +468,8 @@ public:
clear(); clear();
} }
if (!is_direct()) { if (!is_direct()) {
free(_union.indirect); free(_union.indirect_contents.indirect);
_union.indirect = nullptr; _union.indirect_contents.indirect = nullptr;
} }
} }
@ -521,7 +521,7 @@ public:
if (is_direct()) { if (is_direct()) {
return 0; return 0;
} else { } else {
return ((size_t)(sizeof(T))) * _union.capacity; return ((size_t)(sizeof(T))) * _union.indirect_contents.capacity;
} }
} }

Loading…
Cancel
Save