From bbe5829935ca67689c183840390c10460acb00a4 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Mon, 20 Nov 2000 03:44:25 +0000 Subject: [PATCH] Make POD reference-counting absolutely portable [SVN r8264] --- newtypes.h | 106 ++++++++++++++++++++++------------------------------- 1 file changed, 44 insertions(+), 62 deletions(-) diff --git a/newtypes.h b/newtypes.h index 80f4dade..7503f05b 100644 --- a/newtypes.h +++ b/newtypes.h @@ -299,74 +299,56 @@ PyObject* Reprable::instance_repr(PyObject* instance) const namespace detail { - class shared_pod_manager - { - struct counted_pod - { - union max_align { - short dummy0; - long dummy1; - double dummy2; - long double dummy3; - void* dummy4; - }; - - int m_ref_count; - max_align m_data; - }; + class shared_pod_manager + { + typedef std::pair Holder; + typedef std::vector Storage; - typedef std::pair Holder; - typedef std::vector Storage; + public: + static shared_pod_manager& instance(); + ~shared_pod_manager(); + + template + void replace_if_equal(T*& t) + { + t = reinterpret_cast(replace_if_equal(t, sizeof(T))); + } + + template + void make_unique_copy(T*& t) + { + t = reinterpret_cast(make_unique_copy(t, sizeof(T))); + } + + template + void create(T*& t) + { + t = reinterpret_cast(create(sizeof(T))); + } + + template + void dispose(T* t) + { + dec_ref(t, sizeof(T)); + } + + private: + void* replace_if_equal(void* pod, std::size_t size); + void* make_unique_copy(void* pod, std::size_t size); + void* create(std::size_t size); + void dec_ref(void* pod, std::size_t size); - public: - enum { offset = PY_OFFSETOF(counted_pod, m_data) }; - - static shared_pod_manager& instance(); - ~shared_pod_manager(); + struct Compare; + struct identical; - template - void replace_if_equal(T*& t) - { - t = reinterpret_cast(replace_if_equal(t, sizeof(T))); - } - - template - void make_unique_copy(T*& t) - { - t = reinterpret_cast(make_unique_copy(t, sizeof(T))); - } - - template - void create(T*& t) - { - t = reinterpret_cast(create(sizeof(T))); - } - - template - void dispose(T* t) - { - dec_ref(t); - } - - private: - void* replace_if_equal(void* pod, std::size_t size); - void* make_unique_copy(void* pod, std::size_t size); - void* create(std::size_t size); - void dec_ref(void * pod); - - struct Compare; - struct identical; - - private: - shared_pod_manager() {} // singleton + private: + shared_pod_manager() {} // singleton #ifdef TYPE_OBJECT_BASE_STANDALONE_TEST - public: + public: #endif - Storage m_storage; - }; - - + Storage m_storage; + }; void add_capability(TypeObjectBase::Capability capability,