Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

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

, , 22 Jun 2009
In this article, we will try to make our algorithms work faster using the methods of low-level optimization of memory allocation in C++.
make-it-faster.zip
src
make_it_faster
make_it_faster_80.vcproj.DEV.ligen.user
alloc
win32
make_it_faster_win32
make_it_faster_win32_80.vcproj.DEV.ligen.user
win32_arena_local_sample
arena_local_sample_80.vcproj.DEV.ligen.user
win32_arena_sample
arena_sample_80.vcproj.DEV.ligen.user
win32_arena_tests
arena_tests_80.vcproj.DEV.ligen.user
win32_fast_object_sample
fast_object_sample_80.vcproj.DEV.ligen.user
#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.
Ukraine Ukraine
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

31 members

Follow on   LinkedIn

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