Beispiel #1
0
// Converts from Ascii or Hex to Decimal integer
int Strtoi(const char * szStr)
{
    UINT retVal = 0;
	bool fNegative=false;

	if( szStr && *szStr)
	{
		char ch;

		if(*szStr=='-')
		{
			fNegative=true;
			szStr++;
		}
		else if(*szStr == '+')
			szStr++;

		// Check if it is a hex number
		if( ('0' == szStr[0]) && (('X' == szStr[1]) || ('x' == szStr[1])) )
		{
			szStr+=2;
			while( ch = *szStr++)
			{
				if(ch >= '0' && ch <='9' )
					ch-='0';
				else if( ch >= 'A' && ch<='F' )
					ch=ch-'A' + 10;
				else if( ch >= 'a' && ch<='f')
					ch=ch-'a'+10;
				else
					return 0;	//invalid string

				if (FAILED (UIntMult(retVal, 16, &retVal)))
					return 0;
				if (FAILED (UIntAdd(retVal, (UINT)ch, &retVal)))
					return 0;
			}
		}
		// Otherwise its an int
		else
		{
			while( ch = *szStr++)
			{
				if(ch >= '0' && ch <='9' )
					ch-='0';
				else
					return 0;	//invalid string
					
				if (FAILED (UIntMult(retVal, 10, &retVal)))
					return 0;
				if (FAILED (UIntAdd(retVal, (UINT)ch, &retVal)))
					return 0;
			}
		}
	}

	Assert (INT_MAX > retVal);
    return ( fNegative ? -((int)retVal) : (int)retVal );
}
Beispiel #2
0
void* CDataBlockStore::Allocate(UINT bufferSize)
{
    void *pRetValue = NULL;

#if _DEBUG
    m_cAllocations++;
#endif

    if (!m_pFirst)
    {
        m_pFirst = NEW CDataBlock();
        if (!m_pFirst)
            return NULL;

        if (m_IsAligned)
        {
            m_pFirst->EnableAlignment();
        }
        m_pLast = m_pFirst;
    }

    if (FAILED(UIntAdd(m_Size, bufferSize, &m_Size)))
        return NULL;

    pRetValue = m_pLast->Allocate(bufferSize, &m_pLast);
    if (!pRetValue)
        return NULL;

    return pRetValue;
}
Beispiel #3
0
void* CDataBlockStore::Allocate(_In_ uint32_t bufferSize)
{
    void *pRetValue = nullptr;

#if _DEBUG
    m_cAllocations++;
#endif

    if (!m_pFirst)
    {
        m_pFirst = new CDataBlock();
        if (!m_pFirst)
            return nullptr;

        if (m_IsAligned)
        {
            m_pFirst->EnableAlignment();
        }
        m_pLast = m_pFirst;
    }

    if (FAILED(UIntAdd(m_Size, bufferSize, &m_Size)))
        return nullptr;

    pRetValue = m_pLast->Allocate(bufferSize, &m_pLast);
    if (!pRetValue)
        return nullptr;

    return pRetValue;
}
Beispiel #4
0
void test3()
 {
  Random random;
  
  for(ulen cnt=Count; cnt ;cnt--)
    {
     UInt a=random.next_uint<UInt>();
     UInt b=random.next_uint<UInt>();
     UInt s=random.next_uint<UInt>();
     UInt mod=random.next_uint<UInt>();
     
     if( mod==0 ) mod=1;
     
     a%=mod;
     b%=mod;
     s%=mod;
     
     typename UIntFunc<UInt>::Mul mul(a,b);
     
     if( UIntMod(mul.hi,mul.lo,mod)!=UIntModMul(a,b,mod) )
       {
        Printf(Exception,"Fail 5");
       }
     
     mul.hi+=UIntAdd(mul.lo,s);
     
     if( UIntMod(mul.hi,mul.lo,mod)!=UIntModMac(s,a,b,mod) )
       {
        Printf(Exception,"Fail 6");
       }
    }
 }
Beispiel #5
0
ulen Sem::begin_give_many(ulen dcount)
 {
  FastMutex::Lock lock(mutex);
   
  if( take_count<dcount )
    {
     dcount-=take_count;
      
     if( UIntAdd(count,dcount) ) Abort("Fatal error : CCore::Sem counter add overflow");
     
     return Replace_null(take_count);
    }
  else
    {
     take_count-=dcount;
     
     return dcount;
    }  
 }
Beispiel #6
0
void test2()
 {
  Random random;
  
  for(ulen cnt=Count; cnt ;cnt--)
    {
     UInt hi=random.next_uint<UInt>();
     UInt lo=random.next_uint<UInt>();
     UInt den=random.next_uint<UInt>();
     
     if( den==0 ) den=1;
     
     hi%=den;
     
     typename UIntFunc<UInt>::DivMod res(hi,lo,den);
     
     if( res.div!=UIntDiv(hi,lo,den) )
       {
        Printf(Exception,"Fail 2");
       }
     
     if( res.mod!=UIntMod(hi,lo,den) )
       {
        Printf(Exception,"Fail 3");
       }
     
     typename UIntFunc<UInt>::Mul mul(den,res.div);
     
     mul.hi+=UIntAdd(mul.lo,res.mod);
     
     if( mul.hi!=hi || mul.lo!=lo )
       {
        Printf(Exception,"Fail 4");
       }
    }
 }
Beispiel #7
0
void AntiSem::add(ulen dcount)
 {
  FastMutex::Lock lock(mutex);
  
  if( UIntAdd(count,dcount) ) Abort("Fatal error: CCore::AntiSem counter overflow");
 }
Beispiel #8
0
void AntiSem::add_count(ulen dcount)
 {
  if( UIntAdd(count,dcount) ) Abort("Fatal error: CCore::AntiSem counter overflow");
 }