示例#1
0
Buffer* BufferPool::Acquire(unsigned size)
{
    Buffer *it;
    
    for (it = available.First(); it != NULL; it = available.Next(it))
    {
        if (it->GetSize() >= size)
        {
            available.Remove(it);
            ASSERT(availableSize - it->GetSize() <= availableSize);
            availableSize -= it->GetSize();
            return it;  
        }
    }
    
    if (available.First() != NULL)
    {
        it = available.First();
        available.Remove(it);
        ASSERT(availableSize - it->GetSize() <= availableSize);
        availableSize -= it->GetSize();
    }
    else
        it = new Buffer();

    it->Allocate(size);

    return it;
}
示例#2
0
const bool ConsoleUI::Separator(LPCTSTR pszText, int nLeftMargin, int nRightMargin) const
{
	CONSOLE_SCREEN_BUFFER_INFO	conInfo;		// Console information
	Buffer<TCHAR>				lineBuffer;		// Console output buffer
	int							nIndex = 0;		// Offset into line buffer
	int							nLength = 0;	// Length of the string
	int							nTextPos;		// Position of separator text

	if(pszText) nLength = static_cast<int>(_tcslen(pszText));	// Determine length

	// Attempt to retrieve the screen buffer information for the console,
	// and allocate a line buffer large enough for the maximum output

	if(!GetConsoleScreenBufferInfo(m_hout, &conInfo)) return false;
	if(!lineBuffer.Allocate(conInfo.dwSize.X + 1)) return false;

	// Calculate both the length of the separator bar, and the position that
	// the inserted text will appear in the line buffer
	
	conInfo.dwSize.X -= (nLeftMargin + nRightMargin);
	nTextPos = (conInfo.dwSize.X - nLength) / 2;

	// Construct the separator bar line buffer by first adding the hyphens, then
	// the centered text, and the trailing hyphens ...
	
	while(nIndex < nTextPos) lineBuffer[nIndex++] = CONSOLE_SEPARATOR;
	if(nLength > 0) { while(*pszText) { lineBuffer[nIndex++] = *pszText; pszText++; } }
	while(nIndex < conInfo.dwSize.X) lineBuffer[nIndex++] = CONSOLE_SEPARATOR;

	return Write(lineBuffer, nLeftMargin);		// Display the separator bar
}
示例#3
0
DWORD ConsoleUI::Activate(LPCTSTR pszTitle)
{
	CONSOLE_SCREEN_BUFFER_INFO	conInfo;			// Console screen buffer info
	Buffer<TCHAR>				strConTitle;		// Console title string buffer
	
	// Attempt to lock the console handles.  The only way this can fail is if
	// the underlying kernel object(s) were not properly created
	
	if(!LockConsole(true)) return ERROR_INVALID_HANDLE;
	
	// Check to see if the input/output handles have already been initialized

	if(m_hin != m_hout) { UnlockConsole(); return ERROR_ALREADY_INITIALIZED; }
	
	// Retrieve the STDIN/STDOUT handles for this process.  If they are both set
	// to INVALID_HANDLE_VALUE, this process is not attached to a console
	
	m_hin = GetStdHandle(STD_INPUT_HANDLE);
	m_hout = GetStdHandle(STD_OUTPUT_HANDLE);
	
	if(m_hin == m_hout) { UnlockConsole(); return ERROR_INVALID_HANDLE; }

	// There are some assumptions made about the width of the console,
	// so ensure that the screen buffer is at least CONSOLE_MIN_WIDTH wide 
	
	if(GetConsoleScreenBufferInfo(m_hout, &conInfo)) {

		if(conInfo.dwSize.X < CONSOLE_MIN_WIDTH) {

			m_uSavedWidth = conInfo.dwSize.X;
			conInfo.dwSize.X = CONSOLE_MIN_WIDTH;
			SetConsoleScreenBufferSize(m_hout, conInfo.dwSize);
		}
	}

	// Change the console window title to reflect the application name

	if((pszTitle) && (strConTitle.Allocate(1025))) {

		if(GetConsoleTitle(strConTitle, 1024) > 0) {

			m_strSavedTitle = strConTitle;		// Save the original title
			SetConsoleTitle(pszTitle);			// Set the new title
		}
	}

	// Attempt to create the CTRL+C handler event object, and register the control
	// handler function to be used for this process

	s_hevtCtrlC = CreateEvent(NULL, FALSE, FALSE, NULL);
	SetConsoleCtrlHandler(ConsoleControlHandler, TRUE);

	BlankLines();							// Start out with a blank line
	UnlockConsole();						// Release the console lock

	return ERROR_SUCCESS;
}
示例#4
0
文件: sspictxt.cpp 项目: tomasr/wsspi
/**
  Call this once you are done with Context::Authenticate()
  Sending the client the buffer returned by this call,
  you ensure that the client knows the real result of
  the authentication process (e.g. logon denied, etc).

  The returned buffer is allocated by the function, and
  freed automatically on the buffer's destruction.
*/
void ServerContext::ConfirmAuthentication ( Buffer & buf )
{
  // here we build the confirmation 
  // message to the client that confirms the
  // login
  buf.Free ( );
  buf.Allocate ( sizeof(auth_state), bt_confirmation );
  buf.SetContents ( (BYTE*)&m_state, sizeof(m_state) );
} 
示例#5
0
Buffer* BufferPool::Allocate(unsigned size)
{
    Buffer* buffer;
    
    buffer = new Buffer();
    buffer->Allocate(size);
    
    return buffer;
}
示例#6
0
void StorageBloomPage::Write(Buffer& buffer)
{
    uint32_t    checksum;

    buffer.Allocate(size);
    buffer.Zero();

    checksum = bloomFilter.GetBuffer().GetChecksum();

    buffer.AppendLittle32(size);
    buffer.AppendLittle32(checksum);
    buffer.Append(bloomFilter.GetBuffer());
}
示例#7
0
文件: Buffer.cpp 项目: crnt/scaliendb
void Buffer::ToHexadecimal()
{
    unsigned        i;
    unsigned char   x;
    Buffer          printable;
    const char      digits[] = "0123456789ABCDEF";
    
    printable.Allocate(length * 3);
    for (i = 0; i < length; i++)
    {
        x = (unsigned char) buffer[i];
        printable.Append(digits[x / 16]);
        printable.Append(digits[x % 16]);
        if (i != length - 1)
            printable.Append(' ');
    }
        
    Write(printable);
}
示例#8
0
void Context :: BufferData(GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage) {

	size_t * currentBuffer = CurrentBufferForTarget(target);

	if (!currentBuffer) {
		return;
	}

	BufferUsage bufferUsage;

	switch (usage) {
	case GL_STATIC_DRAW:
		bufferUsage = BufferUsageStaticDraw;
		break;

	case GL_DYNAMIC_DRAW:
		bufferUsage = BufferUsageDynamicDraw;
		break;

	default:
		RecordError(GL_INVALID_VALUE);
		return;
	}

	if (size < 0) {
		RecordError(GL_INVALID_VALUE);
		return;
	}

	if (*currentBuffer == 0 || !m_Buffers.IsObject(*currentBuffer)) {
		RecordError(GL_INVALID_OPERATION);
		return;
	}

	Buffer * buffer = m_Buffers.GetObject(*currentBuffer);

	if (buffer->Allocate(size, bufferUsage)) {
		memcpy(buffer->GetData(), data, size);
	} else {
		RecordError(GL_OUT_OF_MEMORY);
	}
}
示例#9
0
bool CConnection::SendRequest(XMLMemoryWriter& xml_memory_writer,Buffer &buffer,IEventListener* event_listener)
{
	CHAR   buffer_tmp[1024];
	DWORD  bytes_read;
	const WCHAR* lplpszAcceptTypes[] = { L"*/*", NULL };
	m_request = HttpOpenRequest(m_connection, L"POST", L"/xml-rpc", NULL, 0, lplpszAcceptTypes, 0, 0);
	if (m_request)
	{
		if (HttpSendRequest(m_request, 0, 0, xml_memory_writer.xml_data, xml_memory_writer.xml_data_size))
		{
			DWORD content_len;
			DWORD content_len_size = sizeof(content_len);
			if (HttpQueryInfo(m_request, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &content_len, &content_len_size, 0))
			{
				if (buffer.Allocate(content_len))
				{
					while (InternetReadFile(m_request, buffer_tmp, sizeof(buffer_tmp), &bytes_read) && bytes_read)
					{
						memcpy(buffer.buffer_in + buffer.buffer_in_total, buffer_tmp, bytes_read);
						buffer.buffer_in_total += bytes_read;
					}
					return true;
				}
				else
					event_listener->OnError(L"failed to allocate memory");
			}
			else
				event_listener->OnError(L"failed to query http info");
		}
		else
			event_listener->OnError(L"failed to send http request");
	    InternetCloseHandle(m_request);
	}
	else
		event_listener->OnError(L"failed creating http request");
	return false;
}
示例#10
0
const bool ConsoleUI::WriteText(LPCTSTR pszText, int nLeftMargin, int nRightMargin) const
{
	CONSOLE_SCREEN_BUFFER_INFO	conInfo;		// Console information
	Buffer<TCHAR>				lineBuffer;		// Console line buffer
	int							nIndex = 0;		// Index into the buffer
	int							nBlanks;		// Used to add spaces to the string
	int							nBackup;		// Line break char counter

	if(!pszText) return false;					// NULL string pointer

	// Validate the margin values, and adjust them if necessary
	
	if(nLeftMargin > CONSOLE_MAX_LMARGIN) nLeftMargin = CONSOLE_MAX_LMARGIN;
	if(nRightMargin > CONSOLE_MAX_RMARGIN) nRightMargin = CONSOLE_MAX_RMARGIN;

	// Determine the size of the buffer we need to allocate, and allocate it

	if(!GetConsoleScreenBufferInfo(m_hout, &conInfo)) return false;
	if(!lineBuffer.Allocate(conInfo.dwSize.X + 1)) return false;
	
	nRightMargin = (conInfo.dwSize.X - nRightMargin);	// Calculate right margin
	
	// Loop through this mess until we hit the NULL string terminator ...
	
	while(*pszText) {
		
		nIndex = 0;							// Reset the loop index variable
		lineBuffer.ZeroOut();				// Zero out the buffer contents

		// Apply the left margin to the buffer by adding space characters

		for(nBlanks = 0; (nBlanks < nLeftMargin) && (nIndex < nRightMargin); nBlanks++)
			lineBuffer[nIndex++] = CONSOLE_SPACE;

		// Loop to add the maximum number of characters we can to the buffer,
		// stopping if we run into the end of the string or a line feed char
		
		while((nIndex < nRightMargin) && (*pszText != CONSOLE_NEWLINE) && (*pszText)) {

			if(*pszText != CONSOLE_TAB) lineBuffer[nIndex++] = *pszText++;
			else { lineBuffer[nIndex++] = CONSOLE_SPACE; pszText++; }
		}

		// CONDITION 1 : The line buffer has been filled up.  If the next character
		// in the string is not a SPACE, we need to break up the line neatly

		if((nIndex == nRightMargin) && (*pszText != CONSOLE_SPACE)) {

			pszText--;									// Back off one character
			lineBuffer[--nIndex] = CONSOLE_HYPHEN;		// Assume we need a hyphen

			// Backup up to CONSOLE_HYPHEN_LIMIT characters, looking for a 
			// whitespace character we can break on instead of using the hyphen
			
			nBackup = 0;

			while((nBackup < nIndex) && (nBackup < CONSOLE_HYPHEN_LIMIT)) {

				// If a space is located, break the line and back off the pointers

				if(lineBuffer[nIndex - nBackup] == CONSOLE_SPACE) {
					
					lineBuffer[nIndex - nBackup] = CONSOLE_NEWLINE;
					lineBuffer[nIndex - nBackup + 1] = 0;

					pszText -= nBackup;			// Back off the string pointer
					break;						// We're done
				}

				nBackup++;			// <--- Keep looking for a SPACE character
			}
		}

		// CONDITION 2 : A line feed character has been located.  Just add it
		// to the buffer like any other character

		else if(*pszText == CONSOLE_NEWLINE) lineBuffer[nIndex] = *pszText++;
		
		// CONDITION 3 : The end of the string was reached.  If the entire
		// console line has not been filled, append a line feed character
		
		else if(nIndex < conInfo.dwSize.X) lineBuffer[nIndex] = CONSOLE_NEWLINE;
		
		if(!Write(lineBuffer, 0, false)) return false;	// Display the text
		while(*pszText == CONSOLE_SPACE) pszText++;		// Skip whitespace
	}

	return true;
}