MethodTableメモリ領域割当てにおける加算アルゴリズム

3459 ワード

   MethodTable           ,     MethodTable   ,         。              ,      。 

   MethodTable   ,    methodtable    static  AllocagteNewMT    ,       : 

MethodTable * MethodTable::AllocagteNewMT(EEClass *pClass, 

DWORD dwVtableSlots, 

DWORD dwGCSize, 

DWORD dwNumInterfaces, 

DWORD numGenericArgs, 

DWORD dwNumDicts, 

DWORD cbDict, 

ClassLoader *pClassLoader, 

BaseDomain *pDomain, 

BOOL isInterface, 

BOOL fHasGenericsStaticsInfo, 

BOOL fNeedsRemotableMethodInfo, 

BOOL fNeedsRemotingVtsInfo, 

BOOL fHasThreadOrContextStatics 

, AllocMemTracker *pamTracker 

) 



              ,              : 

   DWORD cbTotalSize = 0; 

  DWORD cbDicts = 0; 

  if (!ClrSafeInt<DWORD>::multiply(dwNumDicts, sizeof(TypeHandle*), cbDicts) || 

   !ClrSafeInt<DWORD>::addition((DWORD)size, cbDicts, cbTotalSize) || 

   !ClrSafeInt<DWORD>::addition(cbTotalSize, dwGCSize, cbTotalSize)) 

   ThrowHR(COR_E_OVERFLOW); 



    addition((DWORD)size, cbDicts, cbTotalSize)   ,       ,             : 

// Returns true if safe, false on overflow 

static inline bool addition(T lhs, T rhs, T &result) 

{ 

//check for T first. 

if(IsSigned()) 

{ 

//test for +/- combo 

if(!IsMixedSign(lhs, rhs)) 

{ 

//either two negatives, or 2 positives, not mixed symbols 

if(rhs < 0) 

{ 

//two negatives 

if(lhs < (T)(MinInt() - rhs)) //remember rhs < 0 

{ 

return false; 

} 

//ok 

} 

else 

{ 

//two positives 

if((T)(MaxInt() - lhs) < rhs) 

{ 

return false; 

} 

//OK 

} 

} 

//else overflow not possible 

result = lhs + rhs; 

return true; 

} 

else //unsigned, and two symbols is mixed 

{ 

if((T)(MaxInt() - lhs) < rhs) 

{ 

return false; 

} 

result = lhs + rhs; 

return true; 

} 

} 



  ,              : 

static bool IsSigned() 

{ 

return( (T)-1 < 0 ); 

} 



//Check if lhs and rhs is mixed Sign symbols 

static bool IsMixedSign(T lhs, T rhs) 

{ 

return ((lhs ^ rhs) < 0); 

} 





//both of the following should optimize away 

static T MinInt() 

{ 

if(IsSigned()) 

{ 

return (T)((T)1 << (BitCount()-1)); 

} 

else 

{ 

return ((T)0); 

} 

} 

static T MaxInt() 

{ 

if(IsSigned()) 

{ 

return (T)~((T)1 << (BitCount()-1)); 

} 

//else 

return (T)(~(T)0); 

} 


       ,       。 

lbq1221110@Cnblogs. 11.5 ; first post at sscli.cnblogs.com