Add a cellIsPlausible assertion

Cleans up a few assertions in tv_comparisons and I want to
use it elsewhere.
Esse commit está contido em:
Jordan DeLong
2013-06-24 23:58:32 -07:00
commit de Sara Golemon
commit 90e73bdf6c
3 arquivos alterados com 76 adições e 53 exclusões
+13 -18
Ver Arquivo
@@ -51,8 +51,7 @@ bool cellRelOp(Op op, const Cell* cell, bool val) {
template<class Op>
bool cellRelOp(Op op, const Cell* cell, int64_t val) {
assert(tvIsPlausible(cell));
assert(cell->m_type != KindOfRef);
assert(cellIsPlausible(cell));
switch (cell->m_type) {
case KindOfUninit:
@@ -88,8 +87,7 @@ bool cellRelOp(Op op, const Cell* cell, int64_t val) {
template<class Op>
bool cellRelOp(Op op, const Cell* cell, double val) {
assert(tvIsPlausible(cell));
assert(cell->m_type != KindOfRef);
assert(cellIsPlausible(cell));
switch (cell->m_type) {
case KindOfUninit:
@@ -116,8 +114,7 @@ bool cellRelOp(Op op, const Cell* cell, double val) {
template<class Op>
bool cellRelOp(Op op, const Cell* cell, const StringData* val) {
assert(tvIsPlausible(cell));
assert(cell->m_type != KindOfRef);
assert(cellIsPlausible(cell));
switch (cell->m_type) {
case KindOfUninit:
@@ -160,8 +157,7 @@ bool cellRelOp(Op op, const Cell* cell, const StringData* val) {
template<class Op>
bool cellRelOp(Op op, const Cell* cell, const ArrayData* ad) {
assert(tvIsPlausible(cell));
assert(cell->m_type != KindOfRef);
assert(cellIsPlausible(cell));
switch (cell->m_type) {
case KindOfUninit:
@@ -186,8 +182,7 @@ bool cellRelOp(Op op, const Cell* cell, const ArrayData* ad) {
template<class Op>
bool cellRelOp(Op op, const Cell* cell, const ObjectData* od) {
assert(tvIsPlausible(cell));
assert(cell->m_type != KindOfRef);
assert(cellIsPlausible(cell));
switch (cell->m_type) {
case KindOfUninit:
@@ -226,8 +221,8 @@ bool cellRelOp(Op op, const Cell* cell, const ObjectData* od) {
template<class Op>
bool cellRelOp(Op op, const Cell* c1, const Cell* c2) {
assert(c1->m_type != KindOfRef);
assert(c2->m_type != KindOfRef);
assert(cellIsPlausible(c1));
assert(cellIsPlausible(c2));
switch (c2->m_type) {
case KindOfUninit:
@@ -371,8 +366,8 @@ struct Gt {
}
bool cellSame(const Cell* c1, const Cell* c2) {
assert(c1->m_type != KindOfRef);
assert(c2->m_type != KindOfRef);
assert(cellIsPlausible(c1));
assert(cellIsPlausible(c2));
bool const null1 = IS_NULL_TYPE(c1->m_type);
bool const null2 = IS_NULL_TYPE(c2->m_type);
@@ -523,8 +518,8 @@ bool tvGreater(const TypedValue* tv1, const TypedValue* tv2) {
//////////////////////////////////////////////////////////////////////
bool cellLessOrEqual(const Cell* c1, const Cell* c2) {
assert(c1->m_type != KindOfRef);
assert(c2->m_type != KindOfRef);
assert(cellIsPlausible(c1));
assert(cellIsPlausible(c2));
if ((c1->m_type == KindOfArray && c2->m_type == KindOfArray) ||
(c1->m_type == KindOfObject && c2->m_type == KindOfObject)) {
@@ -534,8 +529,8 @@ bool cellLessOrEqual(const Cell* c1, const Cell* c2) {
}
bool cellGreaterOrEqual(const Cell* c1, const Cell* c2) {
assert(c1->m_type != KindOfRef);
assert(c2->m_type != KindOfRef);
assert(cellIsPlausible(c1));
assert(cellIsPlausible(c2));
if ((c1->m_type == KindOfArray && c2->m_type == KindOfArray) ||
(c1->m_type == KindOfObject && c2->m_type == KindOfObject)) {
+56 -33
Ver Arquivo
@@ -21,7 +21,62 @@
#include "hphp/system/systemlib.h"
namespace HPHP {
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
bool cellIsPlausible(const Cell* cell) {
assert(cell);
assert(cell->m_type != KindOfRef);
auto assertPtr = [](void* ptr) {
assert(ptr && (uintptr_t(ptr) % sizeof(ptr) == 0));
};
switch (cell->m_type) {
case KindOfUninit:
case KindOfNull:
break;
case KindOfBoolean:
assert(cell->m_data.num == 0 || cell->m_data.num == 1);
break;
case KindOfInt64:
case KindOfDouble:
break;
case KindOfStaticString:
assertPtr(cell->m_data.pstr);
assert(cell->m_data.pstr->isStatic());
break;
case KindOfString:
assertPtr(cell->m_data.pstr);
assert(is_refcount_realistic(cell->m_data.pstr->getCount()));
break;
case KindOfArray:
assertPtr(cell->m_data.parr);
assert(is_refcount_realistic(cell->m_data.parr->getCount()));
break;
case KindOfObject:
assertPtr(cell->m_data.pobj);
assert(!cell->m_data.pobj->isStatic());
break;
case KindOfRef:
assert(!"KindOfRef found in a Cell");
break;
default:
not_reached();
}
return true;
}
bool tvIsPlausible(const TypedValue* tv) {
assert(tv);
if (tv->m_type == KindOfRef) {
assert(tv->m_data.pref);
assert(uintptr_t(tv->m_data.pref) % sizeof(void*) == 0);
assert(is_refcount_realistic(tv->m_data.pref->getCount()));
tv = tv->m_data.pref->tv();
}
return cellIsPlausible(tv);
}
inline void tvUnboxIfNeeded(TypedValue *tv) {
if (tv->m_type == KindOfRef) {
@@ -349,37 +404,5 @@ bool tvCoerceParamToObjectInPlace(TypedValue* tv) {
}
bool tvIsPlausible(const TypedValue* tv) {
if (!tv) return false;
auto okPtr = [](void* ptr) {
return ptr && (uintptr_t(ptr) % sizeof(ptr) == 0);
};
switch (tv->m_type) {
case KindOfUninit:
case KindOfNull:
return true;
case KindOfBoolean:
return tv->m_data.num == 0 || tv->m_data.num == 1;
case KindOfInt64:
case KindOfDouble:
return true;
case KindOfStaticString:
return okPtr(tv->m_data.pstr) && tv->m_data.pstr->isStatic();
case KindOfString:
return okPtr(tv->m_data.pstr) &&
is_refcount_realistic(tv->m_data.pstr->getCount());
case KindOfArray:
return okPtr(tv->m_data.parr) &&
is_refcount_realistic(tv->m_data.parr->getCount());
case KindOfObject:
return okPtr(tv->m_data.pobj) && !tv->m_data.pobj->isStatic();
case KindOfRef:
return okPtr(tv->m_data.pref) &&
tv->m_data.pref->tv()->m_type != KindOfRef;
default:
return false;
}
}
///////////////////////////////////////////////////////////////////////////////
}
+7 -2
Ver Arquivo
@@ -29,12 +29,17 @@ namespace HPHP {
class Variant;
/*
* Assertions on Cells and TypedValues. Should usually only happen
* inside an assert().
*/
bool tvIsPlausible(const TypedValue*);
bool cellIsPlausible(const Cell*);
// Assumes 'data' is live
// Assumes 'IS_REFCOUNTED_TYPE(type)'
void tvDecRefHelper(DataType type, uint64_t datum);
bool tvIsPlausible(const TypedValue* tv);
inline bool tvWillBeReleased(TypedValue* tv) {
return IS_REFCOUNTED_TYPE(tv->m_type) &&
tv->m_data.pstr->getCount() <= 1;