Update memory.h

Update common.h
This commit is contained in:
blueloveTH 2023-02-20 04:58:02 +08:00
parent 410fbc89cf
commit 368e963cb5
2 changed files with 16 additions and 28 deletions

View File

@ -36,7 +36,7 @@
#define PK_VERSION "0.8.9"
#if defined(__EMSCRIPTEN__) || defined(__arm__) || defined(PK_32_BIT)
#if defined(__EMSCRIPTEN__) || defined(__arm__) || defined(__i386__)
typedef int32_t i64;
typedef float f64;
const i64 kMinSafeInt = -((i64)1 << 30);

View File

@ -22,28 +22,9 @@ namespace pkpy{
class shared_ptr {
int* counter;
inline T* _t() const {
if constexpr(std::is_same_v<T, PyObject>){
if(is_tagged()) UNREACHABLE();
}
return (T*)(counter + 1);
}
inline void _inc_counter() const {
if constexpr(std::is_same_v<T, PyObject>){
if(is_tagged()) return;
}
if(counter) ++(*counter);
}
inline void _dec_counter() const {
if constexpr(std::is_same_v<T, PyObject>){
if(is_tagged()) return;
}
if(counter && --(*counter) == 0){
SpAllocator<T>::dealloc(counter);
}
}
#define _t() (T*)(counter + 1)
#define _inc_counter() if(!is_tagged() && counter) ++(*counter)
#define _dec_counter() if(!is_tagged() && counter && --(*counter) == 0) SpAllocator<T>::dealloc(counter)
public:
shared_ptr() : counter(nullptr) {}
@ -99,13 +80,20 @@ namespace pkpy{
return reinterpret_cast<__VAL>(counter);
}
inline bool is_tagged() const { return (cast<i64>() & 0b11) != 0b00; }
inline bool is_tag_00() const { return (cast<i64>() & 0b11) == 0b00; }
inline bool is_tag_01() const { return (cast<i64>() & 0b11) == 0b01; }
inline bool is_tag_10() const { return (cast<i64>() & 0b11) == 0b10; }
inline bool is_tag_11() const { return (cast<i64>() & 0b11) == 0b11; }
inline bool is_tagged() const {
if constexpr(!std::is_same_v<T, PyObject>) return false;
return (reinterpret_cast<i64>(counter) & 0b11) != 0b00;
}
inline bool is_tag_00() const { return (reinterpret_cast<i64>(counter) & 0b11) == 0b00; }
inline bool is_tag_01() const { return (reinterpret_cast<i64>(counter) & 0b11) == 0b01; }
inline bool is_tag_10() const { return (reinterpret_cast<i64>(counter) & 0b11) == 0b10; }
inline bool is_tag_11() const { return (reinterpret_cast<i64>(counter) & 0b11) == 0b11; }
};
#undef _t
#undef _inc_counter
#undef _dec_counter
template <typename T, typename U, typename... Args>
shared_ptr<T> make_shared(Args&&... args) {
static_assert(std::is_base_of_v<T, U>, "U must be derived from T");