From 76f7912420c66ed9603064e5dfac87cbb57fc80e Mon Sep 17 00:00:00 2001 From: Andrzej Kotulski Date: Thu, 9 Jun 2016 07:54:55 -0700 Subject: [PATCH] Make sizeof(std::shared_ptr) = 16 Summary: Reuse data fields of model's superclass (which is actual implementation). Not very pretty, but makes sizeof shared_ptr same as in actual libraries Reviewed By: jvillard Differential Revision: D3398628 fbshipit-source-id: bdb9418 --- .../infer_model/begin_name_override.inc | 1 + .../include/infer_model/end_name_override.inc | 1 + .../cpp/include/infer_model/shared_ptr.h | 55 +++++++++++-------- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/infer/models/cpp/include/infer_model/begin_name_override.inc b/infer/models/cpp/include/infer_model/begin_name_override.inc index 9c48c48de..e8b3e1af2 100644 --- a/infer/models/cpp/include/infer_model/begin_name_override.inc +++ b/infer/models/cpp/include/infer_model/begin_name_override.inc @@ -2,3 +2,4 @@ #define shared_ptr std__shared_ptr #define unique_ptr std__unique_ptr #define make_unique std__make_unique +#define private public diff --git a/infer/models/cpp/include/infer_model/end_name_override.inc b/infer/models/cpp/include/infer_model/end_name_override.inc index f91c269d6..44c9b826a 100644 --- a/infer/models/cpp/include/infer_model/end_name_override.inc +++ b/infer/models/cpp/include/infer_model/end_name_override.inc @@ -2,3 +2,4 @@ #undef shared_ptr #undef unique_ptr #undef make_unique +#undef private diff --git a/infer/models/cpp/include/infer_model/shared_ptr.h b/infer/models/cpp/include/infer_model/shared_ptr.h index 64c634088..ceb7d4c9d 100644 --- a/infer/models/cpp/include/infer_model/shared_ptr.h +++ b/infer/models/cpp/include/infer_model/shared_ptr.h @@ -22,6 +22,13 @@ template class shared_ptr : public std__shared_ptr { public: +#if INFER_USE_LIBCPP + using std__shared_ptr::__ptr_; +#define __data __ptr_ +#else + using __shared_ptr::_M_ptr; +#define __data _M_ptr +#endif // Conversion constructors to allow implicit conversions. // it's here purely to avoid compilation errors template { template shared_ptr(const std__shared_ptr& r, T* p) noexcept {} - T* data; - // constructors: - constexpr shared_ptr() noexcept : data(nullptr) {} + constexpr shared_ptr() noexcept { __data = nullptr; } shared_ptr(nullptr_t) : shared_ptr() {} @@ -47,7 +52,7 @@ class shared_ptr : public std__shared_ptr { template ::value>::type> explicit shared_ptr(Y* p) { - data = p; + __data = p; } template { shared_ptr(nullptr_t p, D d, A a) : shared_ptr(p) {} template - shared_ptr(const shared_ptr& r, T* p) noexcept : data(nullptr) { /* TODO */ + shared_ptr(const shared_ptr& r, T* p) noexcept { + __data = nullptr; /* TODO */ } shared_ptr(const shared_ptr& r) noexcept - : shared_ptr(r.data) { /* TODO - increase refcount*/ + : shared_ptr(r.__data) { /* TODO - increase refcount*/ } template ::value>::type> shared_ptr(const shared_ptr& r) noexcept - : shared_ptr(r.data) { /* TODO - increase refcount*/ + : shared_ptr(r.__data) { /* TODO - increase refcount*/ } - shared_ptr(shared_ptr&& r) noexcept : shared_ptr(r.data) { - r.data = nullptr; + shared_ptr(shared_ptr&& r) noexcept : shared_ptr(r.__data) { + r.__data = nullptr; } template ::value>::type> - shared_ptr(shared_ptr&& r) noexcept : shared_ptr(r.data) { - r.data = nullptr; + shared_ptr(shared_ptr&& r) noexcept : shared_ptr(r.__data) { + r.__data = nullptr; } template { // assignment: shared_ptr& operator=(const shared_ptr& r) noexcept { // shared_ptr(r).swap(*this); - data = r.data; + __data = r.__data; return *this; } @@ -124,13 +130,13 @@ class shared_ptr : public std__shared_ptr { typename = typename enable_if::value>::type> shared_ptr& operator=(const shared_ptr& r) noexcept { // shared_ptr(r).swap(*this); - data = r.data; + __data = r.__data; return *this; } shared_ptr& operator=(shared_ptr&& r) noexcept { // shared_ptr(std::move(r)).swap(*this); - data = r.data; + __data = r.__data; return *this; } @@ -138,7 +144,7 @@ class shared_ptr : public std__shared_ptr { typename = typename enable_if::value>::type> shared_ptr& operator=(shared_ptr&& r) { // shared_ptr(std::move(r)).swap(*this); - data = r.data; + __data = r.__data; return *this; } @@ -156,9 +162,9 @@ class shared_ptr : public std__shared_ptr { // modifiers: void swap(shared_ptr& r) noexcept { - T* tmp = r.data; - r.data = data; - data = tmp; + T* tmp = r.__data; + r.__data = __data; + __data = tmp; } void reset() noexcept { reset((T*)nullptr); } @@ -168,10 +174,10 @@ class shared_ptr : public std__shared_ptr { void reset(Y* p) { /* if (unique()) { - delete data; + delete __data; } */ - data = p; + __data = p; // TODO adjust refcounts } @@ -191,14 +197,14 @@ class shared_ptr : public std__shared_ptr { } // observers: - T* get() const noexcept { return data; } + T* get() const noexcept { return __data; } typename add_lvalue_reference::type operator*() const noexcept { - return *data; + return *__data; } - T* operator->() const noexcept { return data; } + T* operator->() const noexcept { return __data; } long use_count() const noexcept { return 2; /* FIXME */ } bool unique() const noexcept { return use_count() == 1; /* FIXME */ } - explicit operator bool() const noexcept { return (bool)data; } + explicit operator bool() const noexcept { return (bool)__data; } template bool owner_before(shared_ptr const& b) const { return true; /* FIXME - use non-det*/ @@ -232,4 +238,5 @@ shared_ptr make_shared(Args&&... args) { return shared_ptr(new T(std::forward(args)...)); } +#undef __data INFER_NAMESPACE_STD_END