[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 { rabbit::UserPointer rabbit::Object::getUserDataValue() const {
return (rabbit::UserPointer)sq_aligning(_unVal.pUserData + 1); 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::Object getRealObject() const;
rabbit::UserPointer getUserDataValue() 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) inline void _Swap(rabbit::Object &a,rabbit::Object &b)
{ {
rabbit::ObjectType tOldType = a._type; rabbit::ObjectType tOldType = a._type;

View File

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

View File

@ -52,6 +52,7 @@ namespace rabbit {
ObjectPtr& operator=(const ObjectPtr& _obj); ObjectPtr& operator=(const ObjectPtr& _obj);
ObjectPtr& operator=(const Object& _obj); ObjectPtr& operator=(const Object& _obj);
void Null(); void Null();
void swap(rabbit::ObjectPtr& _obj);
private: private:
ObjectPtr(const char * _obj){} //safety 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) { if(po->isRefCounted() == false) {
return; return;
} }
__addRef(po->_type,po->_unVal); po->addRef();
} }
uint64_t rabbit::sq_getrefcount(rabbit::VirtualMachine* v,rabbit::Object *po) 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; return SQTrue;
} }
bool ret = (po->_unVal.pRefCounted->refCountget() <= 1) ? SQTrue : SQFalse; 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) return ret; //the ret val doesn't work(and cannot be fixed)
} }