Click here to Skip to main content
12,396,230 members (66,656 online)
Click here to Skip to main content

Stats

24.3K views
205 downloads
43 bookmarked
Posted

Session of low-level optimization of memory usage in C++ programs with total exposure

, , 22 Jun 2009 CPOL
In this article, we will try to make our algorithms work faster using the methods of low-level optimization of memory allocation in C++.
#include "windows.h"
#include "exception"
#include "cmnArena.h"
#include "new"

namespace utils
{

static IArenaProvider * g_arenaProvider = 0;
static IArenaGlobalCounter * g_pCounter = 0;

void Arena_InitLib(IArenaProvider * pProvider, IArenaGlobalCounter * pCounter)
{
    g_arenaProvider = pProvider;
    g_pCounter = pCounter;
}
void Arena_UnregisterLib()
{
    g_arenaProvider = 0;
    g_pCounter = 0;
}
void Arena_ChangeGlobalCount(int iValue)
{
    if (g_pCounter)
		g_pCounter->ChangeGlobalCount(iValue);
}
long Arena_GetGlobalCount()
{
    if (g_pCounter)
	    return g_pCounter->GetGlobalCount();
	return 0;
}

void RegisterArena(IArena * pArena)
{
    ArenaContext_type context;
    g_arenaProvider->QueryThreadAssociatedItem( &context );

    if (context->GetArena())
        throw std::exception("RegisterArena.CannotRegisterArena:Duplicate");
    
    context->SetArena(pArena);
}
IArena * ExchangeArena(IArena * pArena)
{
    ArenaContext_type context;
    g_arenaProvider->QueryThreadAssociatedItem( &context );
    
    IArena * pOld = context->GetArena();
    context->SetArena( pArena );
    return pOld;
}

void UnregisterArena()
{
    ArenaContext_type context;
    g_arenaProvider->QueryThreadAssociatedItem( &context );
    context->SetArena( 0 );
}

void *Arena_Alloc(std::size_t size)
{
    if (g_arenaProvider)
    {
		ArenaContext_type context;
		g_arenaProvider->QueryThreadAssociatedItem( &context );
	    
		if (IArena * pArena = context->GetArena())
		{
			return pArena->Allocate(size);
		}
    }

    void * pResult = malloc(size);
    if (!pResult)
        throw std::bad_alloc();
    return pResult;
}

void *Arena_AllocNoThrow(std::size_t size)
{
    if (g_arenaProvider)
    {
		ArenaContext_type context;
		g_arenaProvider->QueryThreadAssociatedItem( &context );
	    
		if (IArena * pArena = context->GetArena())
		{
			return pArena->AllocateNoThrow(size);
		}
	}

    void * pResult = malloc(size);
    return pResult;
}

void Arena_Free(void * p)
{
    if (!p)
        return;

    if (g_arenaProvider)
    {
		ArenaContext_type context;
		g_arenaProvider->QueryThreadAssociatedItem( &context );
	    
		if (IArena * pArena = context->GetArena())
		{
			return pArena->Deallocate(p);
		}
    }
    free(p);
}

} // utils

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Authors

No Biography provided

Apriorit Inc
Apriorit Inc.
Hungary Hungary
ApriorIT is a Software Research and Development company that works in advanced knowledge-intensive scopes.

Company offers integrated research&development services for the software projects in such directions as Corporate Security, Remote Control, Mobile Development, Embedded Systems, Virtualization, Drivers and others.

Official site http://www.apriorit.com
Group type: Organisation

32 members


You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 23 Jun 2009
Article Copyright 2009 by Victor A. Milokum, Apriorit Inc
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid