utils.cpp 4.5 KB
Newer Older
E
Entong Shen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
//---------------------------------------------------------------------------
//	Greenplum Database
//	Copyright (C) 2008 Greenplum, Inc.
//
//	@filename:
//		utils.cpp
//
//	@doc:
//		Various utilities which are not necessarily gpos specific
//---------------------------------------------------------------------------
#include "gpos/types.h"
#include "gpos/utils.h"

// using 16 addresses a line fits exactly into 80 characters
#define GPOS_MEM_BPL		16


// number of stack frames to search for addresses
#define GPOS_SEARCH_STACK_FRAMES	16


using namespace gpos;

//---------------------------------------------------------------------------
//	GPOS versions of standard streams
//	Do not reference std::(w)cerr/(w)cout directly;
//---------------------------------------------------------------------------
COstreamBasic gpos::oswcerr(&std::wcerr);
COstreamBasic gpos::oswcout(&std::wcout);


//---------------------------------------------------------------------------
//	@function:
//		gpos::Print
//
//	@doc:
//		Print wide-character string
//
//---------------------------------------------------------------------------
void
gpos::Print(WCHAR *wsz)
{
	std::wcout << wsz;
}


//---------------------------------------------------------------------------
//	@function:
//		gpos::HexDump
//
//	@doc:
//		Generic memory dumper; produces regular hex dump
//
//---------------------------------------------------------------------------
IOstream&
gpos::HexDump
	(
	IOstream &os,
	const void *pv,
	ULLONG ullSize
	)
{
	for(ULONG i = 0; i < 1 + (ullSize / GPOS_MEM_BPL); i++)
	{
		// starting address of line
		BYTE *pBuf = ((BYTE*)pv) + (GPOS_MEM_BPL * i);
		os << (void*)pBuf << "  ";
		os << COstream::EsmHex;

        // individual bytes
		for(ULONG j = 0; j < GPOS_MEM_BPL; j++)
		{
			if (pBuf[j] < 16) 
			{
				os << "0";
			}

			os << (ULONG)pBuf[j] << " ";

			// separator in middle of line
			if (j + 1 == GPOS_MEM_BPL / 2)
			{
				os << "- ";
			}
		}

		// blank between hex and text dump
		os << " ";

		// text representation
		for(ULONG j = 0; j < GPOS_MEM_BPL; j++)
		{
			// print only 'visible' characters
			if(pBuf[j] >= 0x20 && pBuf[j] <= 0x7f)
			{
				// cast to CHAR to avoid stream from (mis-)interpreting BYTE
				os << (CHAR)pBuf[j];
			}
			else
			{
				os << ".";
			}
		}
		os << COstream::EsmDec << std::endl;
	}
	return os;
}


//---------------------------------------------------------------------------
//	@function:
//		gpos::UlHashByteArray
//
//	@doc:
//		Generic hash function for an array of BYTEs
//		Taken from D. E. Knuth;
//
//---------------------------------------------------------------------------
ULONG
gpos::UlHashByteArray
	( 
	const BYTE *pb,
	ULONG ulSize
	)
{
	ULONG ulHash = ulSize;
		
	for(ULONG i = 0; i < ulSize; ++i)
	{
		BYTE b = pb[i];
		ulHash = ((ulHash << 5) ^ (ulHash >> 27)) ^ b;
	}
	
	return ulHash;
}


//---------------------------------------------------------------------------
//	@function:
//		gpos::UlCombineHashes
//
//	@doc:
//		Combine ULONG-based hash values
//
//---------------------------------------------------------------------------
ULONG
gpos::UlCombineHashes
	(
	ULONG ul0,
	ULONG ul1
	)
{
	ULONG rgul[2];
	rgul[0] = ul0;
	rgul[1] = ul1;

	return UlHashByteArray((BYTE*)rgul, GPOS_ARRAY_SIZE(rgul) * sizeof(rgul[0]));
}


//---------------------------------------------------------------------------
//	@function:
//		gpos::UllAdd
//
//	@doc:
//		Add two unsigned long long values, throw an exception if overflow occurs,
//
//---------------------------------------------------------------------------
ULLONG
gpos::UllAdd
	(
	ULLONG ullFst,
	ULLONG ullSnd
	)
{
	if (ullFst > ULLONG_MAX - ullSnd)
	{
		// if addition result overflows, we have (a + b > ULLONG_MAX),
		// then we need to check for  (a > ULLONG_MAX - b)
		GPOS_RAISE(CException::ExmaSystem, CException::ExmiOverflow);
	}

	ULLONG ullRes = ullFst + ullSnd;

	return ullRes;
}


//---------------------------------------------------------------------------
//	@function:
//		gpos::UllMultiply
//
//	@doc:
//		Multiply two unsigned long long values, throw an exception if overflow occurs,
//
//---------------------------------------------------------------------------
ULLONG
gpos::UllMultiply
	(
	ULLONG ullFst,
	ULLONG ullSnd
	)
{
	if (0 < ullSnd &&
		ullFst > ULLONG_MAX / ullSnd)
	{
		// if multiplication result overflows, we have (a * b > ULLONG_MAX),
		// then we need to check for  (a > ULLONG_MAX / b)
		GPOS_RAISE(CException::ExmaSystem, CException::ExmiOverflow);

	}
	ULLONG ullRes = ullFst * ullSnd;

	return ullRes;
}

// EOF