Click here to Skip to main content
13,299,252 members (45,215 online)
Click here to Skip to main content

Stats

27.6K views
222 downloads
43 bookmarked
Posted 16 Jun 2009

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

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 "cmnTlsBasedArena.h"
#include "new"

namespace utils
{

CTlsArenaContext::CTlsArenaContext(CTlsArenaProvider * pProvider)
        : m_pProvider(pProvider)
{
}
// arena api
IArena * CTlsArenaContext::GetArena()const
{
    return m_pProvider->GetArena();
}
void CTlsArenaContext::SetArena(IArena * pArena)
{
    m_pProvider->SetArena(pArena);
}


// provider
CTlsArenaProvider::CTlsArenaProvider()
    : m_dwTLS_index(0)
{
    m_dwTLS_index = TlsAlloc();
    if (TLS_OUT_OF_INDEXES == m_dwTLS_index)
        throw std::runtime_error("TLSAlloc failed");
}

CTlsArenaProvider::~CTlsArenaProvider()
{
    TlsFree(m_dwTLS_index);
}

// item api
void CTlsArenaProvider::QueryThreadAssociatedItem(ArenaContext_type * pContext)
{
    pContext->CreateByCopy(utils::manageable<CTlsArenaContext, utils::allow_copy>(this));
}

// arena api
IArena * CTlsArenaProvider::GetArena()const
{
    if (m_dwTLS_index == TLS_OUT_OF_INDEXES)
        throw std::runtime_error("Arena library not initialized");

    IArena * pArena = (IArena * )TlsGetValue(m_dwTLS_index);
    if (!pArena && GetLastError() != ERROR_SUCCESS)
        throw std::runtime_error("Cannot get arena");

    return pArena;
}

void CTlsArenaProvider::SetArena(IArena * pArena)
{
    if (m_dwTLS_index == TLS_OUT_OF_INDEXES)
        throw std::runtime_error("Arena library not initialized");

    if (!TlsSetValue(m_dwTLS_index, pArena))
        throw std::runtime_error("Cannot register arena");
}

} // 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
This member doesn't quite have enough reputation to be able to display their biography and homepage.
Group type: Organisation

32 members


You may also be interested in...

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