Commit 049e3d99 authored by Nicholas Ormrod's avatar Nicholas Ormrod Committed by Facebook Github Bot

Remove unpackHack from smallVector

Summary:
This change removes the unpackHack function from small_vector, which was blocking ##-Waddress-of-packed-member## from being enabled. The fix is split the pointer-getting non-const ##getCapacity## into a normal getter and setter.

(lithium is flakey, according to continuous, and is push-blocking)

Reviewed By: yfeldblum

Differential Revision: D4918188

fbshipit-source-id: 435e030ad659f5dc9c42d90e9bfee9ca564a120a
parent c6cf2b08
...@@ -487,7 +487,9 @@ class small_vector ...@@ -487,7 +487,9 @@ class small_vector
auto thisCapacity = this->capacity(); auto thisCapacity = this->capacity();
auto oCapacity = o.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); this->setCapacity(oCapacity);
o.setCapacity(thisCapacity); o.setCapacity(thisCapacity);
...@@ -620,7 +622,7 @@ class small_vector ...@@ -620,7 +622,7 @@ class small_vector
size_type capacity() const { size_type capacity() const {
if (this->isExtern()) { if (this->isExtern()) {
if (u.hasCapacity()) { if (u.hasCapacity()) {
return *u.getCapacity(); return u.getCapacity();
} }
return malloc_usable_size(u.pdata_.heap_) / sizeof(value_type); return malloc_usable_size(u.pdata_.heap_) / sizeof(value_type);
} }
...@@ -816,15 +818,6 @@ private: ...@@ -816,15 +818,6 @@ private:
return const_cast<iterator>(it); 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 // The std::false_type argument is part of disambiguating the
// iterator insert functions from integral types (see insert().) // iterator insert functions from integral types (see insert().)
template<class It> template<class It>
...@@ -1015,7 +1008,7 @@ private: ...@@ -1015,7 +1008,7 @@ private:
assert(this->isExtern()); assert(this->isExtern());
if (u.hasCapacity()) { if (u.hasCapacity()) {
assert(newCapacity < std::numeric_limits<InternalSizeType>::max()); assert(newCapacity < std::numeric_limits<InternalSizeType>::max());
*u.getCapacity() = InternalSizeType(newCapacity); u.setCapacity(newCapacity);
} }
} }
...@@ -1024,8 +1017,11 @@ private: ...@@ -1024,8 +1017,11 @@ private:
void* heap_; void* heap_;
InternalSizeType capacity_; InternalSizeType capacity_;
InternalSizeType* getCapacity() { InternalSizeType getCapacity() const {
return &capacity_; return capacity_;
}
void setCapacity(InternalSizeType c) {
capacity_ = c;
} }
} FOLLY_PACK_ATTR; } FOLLY_PACK_ATTR;
...@@ -1034,10 +1030,12 @@ private: ...@@ -1034,10 +1030,12 @@ private:
// stored at the front of the heap allocation. // stored at the front of the heap allocation.
void* heap_; void* heap_;
InternalSizeType* getCapacity() { InternalSizeType getCapacity() const {
assert(detail::pointerFlagGet(heap_)); assert(detail::pointerFlagGet(heap_));
return static_cast<InternalSizeType*>( return *static_cast<InternalSizeType*>(detail::pointerFlagClear(heap_));
detail::pointerFlagClear(heap_)); }
void setCapacity(InternalSizeType c) {
*static_cast<InternalSizeType*>(detail::pointerFlagClear(heap_)) = c;
} }
} FOLLY_PACK_ATTR; } FOLLY_PACK_ATTR;
...@@ -1103,11 +1101,11 @@ private: ...@@ -1103,11 +1101,11 @@ private:
bool hasCapacity() const { bool hasCapacity() const {
return kHasInlineCapacity || detail::pointerFlagGet(pdata_.heap_); return kHasInlineCapacity || detail::pointerFlagGet(pdata_.heap_);
} }
InternalSizeType* getCapacity() { InternalSizeType getCapacity() const {
return pdata_.getCapacity(); return pdata_.getCapacity();
} }
InternalSizeType* getCapacity() const { void setCapacity(InternalSizeType c) {
return const_cast<Data*>(this)->getCapacity(); pdata_.setCapacity(c);
} }
void freeHeap() { void freeHeap() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment