auto thisCapacity = this->capacity();
auto oCapacity = o.capacity();
- std::swap(unpackHack(&u.pdata_.heap_), unpackHack(&o.u.pdata_.heap_));
+ auto* tmp = u.pdata_.heap_;
+ u.pdata_.heap_ = o.u.pdata_.heap_;
+ o.u.pdata_.heap_ = tmp;
this->setCapacity(oCapacity);
o.setCapacity(thisCapacity);
size_type capacity() const {
if (this->isExtern()) {
if (u.hasCapacity()) {
- return *u.getCapacity();
+ return u.getCapacity();
}
return malloc_usable_size(u.pdata_.heap_) / sizeof(value_type);
}
return const_cast<iterator>(it);
}
- /*
- * g++ doesn't allow you to bind a non-const reference to a member
- * of a packed structure, presumably because it would make it too
- * easy to accidentally make an unaligned memory access?
- */
- template<class T> static T& unpackHack(T* p) {
- return *p;
- }
-
// The std::false_type argument is part of disambiguating the
// iterator insert functions from integral types (see insert().)
template<class It>
assert(this->isExtern());
if (u.hasCapacity()) {
assert(newCapacity < std::numeric_limits<InternalSizeType>::max());
- *u.getCapacity() = InternalSizeType(newCapacity);
+ u.setCapacity(newCapacity);
}
}
void* heap_;
InternalSizeType capacity_;
- InternalSizeType* getCapacity() {
- return &capacity_;
+ InternalSizeType getCapacity() const {
+ return capacity_;
+ }
+ void setCapacity(InternalSizeType c) {
+ capacity_ = c;
}
} FOLLY_PACK_ATTR;
// stored at the front of the heap allocation.
void* heap_;
- InternalSizeType* getCapacity() {
+ InternalSizeType getCapacity() const {
assert(detail::pointerFlagGet(heap_));
- return static_cast<InternalSizeType*>(
- detail::pointerFlagClear(heap_));
+ return *static_cast<InternalSizeType*>(detail::pointerFlagClear(heap_));
+ }
+ void setCapacity(InternalSizeType c) {
+ *static_cast<InternalSizeType*>(detail::pointerFlagClear(heap_)) = c;
}
} FOLLY_PACK_ATTR;
bool hasCapacity() const {
return kHasInlineCapacity || detail::pointerFlagGet(pdata_.heap_);
}
- InternalSizeType* getCapacity() {
+ InternalSizeType getCapacity() const {
return pdata_.getCapacity();
}
- InternalSizeType* getCapacity() const {
- return const_cast<Data*>(this)->getCapacity();
+ void setCapacity(InternalSizeType c) {
+ pdata_.setCapacity(c);
}
void freeHeap() {