[DEV] remove __release() and __addRef()

This commit is contained in:
Edouard DUPIN 2018-07-05 21:12:57 +02:00
parent ac1bd10654
commit e62f3074cb
5 changed files with 57 additions and 55 deletions

View File

@ -31,3 +31,29 @@ rabbit::Object rabbit::Object::getRealObject() const {
rabbit::UserPointer rabbit::Object::getUserDataValue() const {
return (rabbit::UserPointer)sq_aligning(_unVal.pUserData + 1);
}
void rabbit::Object::addRef() {
if ((_type&SQOBJECT_REF_COUNTED) != 0) {
_unVal.pRefCounted->refCountIncrement();
}
}
void rabbit::Object::releaseRef() {
if ( (_type&SQOBJECT_REF_COUNTED) != 0
&& (_unVal.pRefCounted->refCountDecrement()==0)) {
_unVal.pRefCounted->release();
}
_type = rabbit::OT_NULL;
_unVal.raw = 0;
}
void rabbit::Object::swap(rabbit::Object& _obj) {
rabbit::ObjectType tOldType = _type;
rabbit::ObjectValue unOldVal = _unVal;
_type = _obj._type;
_unVal = _obj._unVal;
_obj._type = tOldType;
_obj._unVal = unOldVal;
}

View File

@ -223,20 +223,12 @@ namespace rabbit {
}
rabbit::Object getRealObject() const;
rabbit::UserPointer getUserDataValue() const;
void addRef();
void releaseRef();
void swap(rabbit::Object& _obj);
};
#define ISREFCOUNTED(t) (t&SQOBJECT_REF_COUNTED)
#define __addRef(type,unval) if(ISREFCOUNTED(type)) \
{ \
unval.pRefCounted->refCountIncrement(); \
}
#define __release(type,unval) if(ISREFCOUNTED(type) && (unval.pRefCounted->refCountDecrement()==0)) \
{ \
unval.pRefCounted->release(); \
}
inline void _Swap(rabbit::Object &a,rabbit::Object &b)
{
rabbit::ObjectType tOldType = a._type;

View File

@ -9,26 +9,19 @@
#include <rabbit/ObjectPtr.hpp>
#include <rabbit/RefCounted.hpp>
#define RABBIT_OBJ_REF_TYPE_INSTANCIATE(type,_class,sym) \
#define RABBIT_OBJ_REF_TYPE_INSTANCIATE(type, _class, sym) \
rabbit::ObjectPtr::ObjectPtr(_class * x) \
{ \
_unVal.raw = 0; \
_type=type; \
_unVal.sym = x; \
assert(_unVal.pTable); \
_unVal.pRefCounted->refCountIncrement(); \
addRef(); \
} \
rabbit::ObjectPtr& rabbit::ObjectPtr::operator=(_class *x) \
{ \
rabbit::ObjectType tOldType; \
rabbit::ObjectValue unOldVal; \
tOldType=_type; \
unOldVal=_unVal; \
_type = type; \
_unVal.raw = 0; \
_unVal.sym = x; \
_unVal.pRefCounted->refCountIncrement(); \
__release(tOldType,unOldVal); \
rabbit::ObjectPtr tmp{x}; \
swap(tmp); \
return *this; \
}
@ -41,10 +34,8 @@
} \
rabbit::ObjectPtr& rabbit::ObjectPtr::operator=(_class x) \
{ \
__release(_type,_unVal); \
_type = type; \
_unVal.raw = 0; \
_unVal.sym = x; \
rabbit::ObjectPtr tmp{x}; \
swap(tmp); \
return *this; \
}
@ -78,13 +69,13 @@ rabbit::ObjectPtr::ObjectPtr() {
rabbit::ObjectPtr::ObjectPtr(const rabbit::ObjectPtr& _obj) {
_type = _obj._type;
_unVal = _obj._unVal;
__addRef(_type,_unVal);
addRef();
}
rabbit::ObjectPtr::ObjectPtr(const rabbit::Object& _obj) {
_type = _obj._type;
_unVal = _obj._unVal;
__addRef(_type,_unVal);
addRef();
}
rabbit::ObjectPtr::ObjectPtr(bool _value) {
@ -94,51 +85,43 @@ rabbit::ObjectPtr::ObjectPtr(bool _value) {
}
rabbit::ObjectPtr& rabbit::ObjectPtr::operator=(bool _value) {
__release(_type,_unVal);
releaseRef();
_unVal.raw = 0;
_type = rabbit::OT_BOOL;
_unVal.nInteger = _value?1:0;
return *this;
}
void rabbit::ObjectPtr::swap(rabbit::ObjectPtr& _obj) {
rabbit::ObjectType tOldType = _type;
rabbit::ObjectValue unOldVal = _unVal;
_type = _obj._type;
_unVal = _obj._unVal;
_obj._type = tOldType;
_obj._unVal = unOldVal;
}
rabbit::ObjectPtr::~ObjectPtr() {
__release(_type,_unVal);
releaseRef();
}
rabbit::ObjectPtr& rabbit::ObjectPtr::operator=(const rabbit::ObjectPtr& _obj) {
rabbit::ObjectType tOldType;
rabbit::ObjectValue unOldVal;
tOldType=_type;
unOldVal=_unVal;
_unVal = _obj._unVal;
_type = _obj._type;
__addRef(_type,_unVal);
__release(tOldType,unOldVal);
rabbit::ObjectPtr tmp{_obj};
swap(tmp);
return *this;
}
rabbit::ObjectPtr& rabbit::ObjectPtr::operator=(const rabbit::Object& _obj) {
rabbit::ObjectType tOldType;
rabbit::ObjectValue unOldVal;
tOldType=_type;
unOldVal=_unVal;
_unVal = _obj._unVal;
_type = _obj._type;
__addRef(_type,_unVal);
__release(tOldType,unOldVal);
rabbit::ObjectPtr tmp{_obj};
swap(tmp);
return *this;
}
void rabbit::ObjectPtr::Null() {
rabbit::ObjectType tOldType = _type;
rabbit::ObjectValue unOldVal = _unVal;
_type = rabbit::OT_NULL;
_unVal.raw = 0;
__release(tOldType ,unOldVal);
releaseRef();
}
uint64_t rabbit::translateIndex(const rabbit::ObjectPtr &idx) {
switch(idx.getType()){
case rabbit::OT_NULL:

View File

@ -52,6 +52,7 @@ namespace rabbit {
ObjectPtr& operator=(const ObjectPtr& _obj);
ObjectPtr& operator=(const Object& _obj);
void Null();
void swap(rabbit::ObjectPtr& _obj);
private:
ObjectPtr(const char * _obj){} //safety
};

View File

@ -177,7 +177,7 @@ void rabbit::sq_addref(rabbit::VirtualMachine* v,rabbit::Object *po)
if(po->isRefCounted() == false) {
return;
}
__addRef(po->_type,po->_unVal);
po->addRef();
}
uint64_t rabbit::sq_getrefcount(rabbit::VirtualMachine* v,rabbit::Object *po)
@ -194,7 +194,7 @@ rabbit::Bool rabbit::sq_release(rabbit::VirtualMachine* v,rabbit::Object *po)
return SQTrue;
}
bool ret = (po->_unVal.pRefCounted->refCountget() <= 1) ? SQTrue : SQFalse;
__release(po->_type,po->_unVal);
po->releaseRef();
return ret; //the ret val doesn't work(and cannot be fixed)
}