Click here to Skip to main content
12,291,271 members (61,849 online)
Click here to Skip to main content

Stats

24K views
200 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++.
#ifndef CMN_GROWING_ARENA_H
#define CMN_GROWING_ARENA_H

// To include #include "cmnGrowingArena.h"

#include "cmnArena.h"

namespace utils
{


template<size_t chunkSize>
class CPoolBuffer
{
    typedef utils::crt_vector<char>::result Buffer_type;
    typedef utils::crt_list<Buffer_type>::result BufferList_type;
    BufferList_type m_bufferList;
public:
    CPoolBuffer()
    {
        NewPage();
    }
    char * GetPtr() 
    { 
        return &m_bufferList.back().front(); 
    }
    size_t GetTotalSize() 
    { 
        return m_bufferList.back().size(); 
    }
    size_t CalcTotalSize()
    {
        return (m_bufferList.size()-1)*chunkSize + GetTotalSize();
    }
    void NewPage() 
    { 
        m_bufferList.push_back(Buffer_type());
        m_bufferList.back().resize(chunkSize);
    }  
};


template<size_t chunkSize>
class CGrowingArena:public CArena<CPoolBuffer<chunkSize> >
{

public:
    CGrowingArena()
    {
    }
    void * Allocate(size_t size)
    {
        const size_t bufSize = GetBuffer()->GetTotalSize();
        size_t index = GetIndex();
        if (bufSize < index)
            throw std::bad_alloc("bad_alloc");

        if (bufSize - index < size)
        {
            if (size > chunkSize)
                throw std::bad_alloc("bad_alloc");

            GetBuffer()->NewPage();
            SetIndex(0);
            index = 0;
        }

        void * pResult = GetBuffer()->GetPtr() + index;
        DbgRegisterAllocate(pResult);
        SetIndex(index + size);
        return pResult;
    }
    virtual void * AllocateNoThrow(size_t size) 
    {
        try
        {
            return Allocate(size);
        }
        catch(std::exception&)
        {
            return 0;
        }
    }

};

}

#endif

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.160518.1 | Last Updated 23 Jun 2009
Article Copyright 2009 by Victor A. Milokum, Apriorit Inc
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid