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


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)

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


// 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.


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


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...

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