Shadowrun: Awakened 29 September 2011 - Build 871
Classes | Public Member Functions | Protected Member Functions | Protected Attributes
DataStructures::MemoryPool< MemoryBlockType > Class Template Reference

#include <DS_MemoryPool.h>

Inheritance diagram for DataStructures::MemoryPool< MemoryBlockType >:

List of all members.

Classes

struct  MemoryWithPage
struct  Page

Public Member Functions

MemoryBlockType * Allocate (const char *file, unsigned int line)
void Clear (const char *file, unsigned int line)
int GetAvailablePagesSize (void) const
int GetMemoryPoolPageSize (void) const
int GetUnavailablePagesSize (void) const
 MemoryPool ()
void Release (MemoryBlockType *m, const char *file, unsigned int line)
void SetPageSize (int size)
 ~MemoryPool ()

Protected Member Functions

void AllocateFirst (void)
int BlocksPerPage (void) const
bool InitPage (Page *page, Page *prev, const char *file, unsigned int line)

Protected Attributes

PageavailablePages
int availablePagesSize
int memoryPoolPageSize
PageunavailablePages
int unavailablePagesSize

Detailed Description

template<class MemoryBlockType>
class DataStructures::MemoryPool< MemoryBlockType >

Very fast memory pool for allocating and deallocating structures that don't have constructors or destructors. Contains a list of pages, each of which has an array of the user structures

Definition at line 29 of file DS_MemoryPool.h.


Constructor & Destructor Documentation

template<class MemoryBlockType >
DataStructures::MemoryPool< MemoryBlockType >::MemoryPool ( )

Definition at line 70 of file DS_MemoryPool.h.

    {
#ifndef _DISABLE_MEMORY_POOL
        //AllocateFirst();
        availablePagesSize=0;
        unavailablePagesSize=0;
        memoryPoolPageSize=16384;
#endif
    }
template<class MemoryBlockType >
DataStructures::MemoryPool< MemoryBlockType >::~MemoryPool ( )

Definition at line 80 of file DS_MemoryPool.h.

References _FILE_AND_LINE_.

    {
#ifndef _DISABLE_MEMORY_POOL
        Clear(_FILE_AND_LINE_);
#endif
    }

Member Function Documentation

template<class MemoryBlockType >
MemoryBlockType * DataStructures::MemoryPool< MemoryBlockType >::Allocate ( const char *  file,
unsigned int  line 
)

Definition at line 94 of file DS_MemoryPool.h.

References DataStructures::MemoryPool< MemoryBlockType >::Page::availableStack, DataStructures::MemoryPool< MemoryBlockType >::Page::availableStackSize, DataStructures::Page< KeyType, DataType, order >::next, DataStructures::MemoryPool< MemoryBlockType >::Page::next, DataStructures::MemoryPool< MemoryBlockType >::Page::prev, RakAssert, and rakMalloc_Ex.

    {
#ifdef _DISABLE_MEMORY_POOL
        return (MemoryBlockType*) rakMalloc_Ex(sizeof(MemoryBlockType), file, line);
#else

        if (availablePagesSize>0)
        {
            MemoryBlockType *retVal;
            Page *curPage;
            curPage=availablePages;
            retVal = (MemoryBlockType*) curPage->availableStack[--(curPage->availableStackSize)];
            if (curPage->availableStackSize==0)
            {
                --availablePagesSize;
                availablePages=curPage->next;
                RakAssert(availablePagesSize==0 || availablePages->availableStackSize>0);
                curPage->next->prev=curPage->prev;
                curPage->prev->next=curPage->next;

                if (unavailablePagesSize++==0)
                {
                    unavailablePages=curPage;
                    curPage->next=curPage;
                    curPage->prev=curPage;  
                }
                else
                {
                    curPage->next=unavailablePages;
                    curPage->prev=unavailablePages->prev;
                    unavailablePages->prev->next=curPage;
                    unavailablePages->prev=curPage;
                }           
            }

            RakAssert(availablePagesSize==0 || availablePages->availableStackSize>0);
            return retVal;
        }

        availablePages = (Page *) rakMalloc_Ex(sizeof(Page), file, line);
        if (availablePages==0)
            return 0;
        availablePagesSize=1;
        if (InitPage(availablePages, availablePages, file, line)==false)
            return 0;
        // If this assert hits, we couldn't allocate even 1 block per page. Increase the page size
        RakAssert(availablePages->availableStackSize>1);

        return (MemoryBlockType *) availablePages->availableStack[--availablePages->availableStackSize];
#endif
    }
template<class MemoryBlockType>
void DataStructures::MemoryPool< MemoryBlockType >::AllocateFirst ( void  ) [protected]
template<class MemoryBlockType >
int DataStructures::MemoryPool< MemoryBlockType >::BlocksPerPage ( void  ) const [protected]

Definition at line 261 of file DS_MemoryPool.h.

    {
        return memoryPoolPageSize / sizeof(MemoryWithPage);
    }
template<class MemoryBlockType >
void DataStructures::MemoryPool< MemoryBlockType >::Clear ( const char *  file,
unsigned int  line 
)

Definition at line 208 of file DS_MemoryPool.h.

References DataStructures::MemoryPool< MemoryBlockType >::Page::availableStack, DataStructures::MemoryPool< MemoryBlockType >::Page::block, DataStructures::MemoryPool< MemoryBlockType >::Page::next, and rakFree_Ex.

    {
#ifdef _DISABLE_MEMORY_POOL
        return;
#else
        Page *cur, *freed;

        if (availablePagesSize>0)
        {
            cur = availablePages;
#ifdef _MSC_VER
#pragma warning(disable:4127)   // conditional expression is constant
#endif
            while (true) 
            // do
            {
                rakFree_Ex(cur->availableStack, file, line );
                rakFree_Ex(cur->block, file, line );
                freed=cur;
                cur=cur->next;
                if (cur==availablePages)
                {
                    rakFree_Ex(freed, file, line );
                    break;
                }
                rakFree_Ex(freed, file, line );
            }// while(cur!=availablePages);
        }
        
        if (unavailablePagesSize>0)
        {
            cur = unavailablePages;
            while (1)
            //do 
            {
                rakFree_Ex(cur->availableStack, file, line );
                rakFree_Ex(cur->block, file, line );
                freed=cur;
                cur=cur->next;
                if (cur==unavailablePages)
                {
                    rakFree_Ex(freed, file, line );
                    break;
                }
                rakFree_Ex(freed, file, line );
            } // while(cur!=unavailablePages);
        }

        availablePagesSize=0;
        unavailablePagesSize=0;
#endif
    }
template<class MemoryBlockType>
int DataStructures::MemoryPool< MemoryBlockType >::GetAvailablePagesSize ( void  ) const [inline]

Definition at line 53 of file DS_MemoryPool.h.

template<class MemoryBlockType>
int DataStructures::MemoryPool< MemoryBlockType >::GetMemoryPoolPageSize ( void  ) const [inline]

Definition at line 55 of file DS_MemoryPool.h.

template<class MemoryBlockType>
int DataStructures::MemoryPool< MemoryBlockType >::GetUnavailablePagesSize ( void  ) const [inline]

Definition at line 54 of file DS_MemoryPool.h.

template<class MemoryBlockType >
bool DataStructures::MemoryPool< MemoryBlockType >::InitPage ( Page page,
Page prev,
const char *  file,
unsigned int  line 
) [protected]

Definition at line 266 of file DS_MemoryPool.h.

References DataStructures::MemoryPool< MemoryBlockType >::Page::availableStack, DataStructures::MemoryPool< MemoryBlockType >::Page::availableStackSize, DataStructures::MemoryPool< MemoryBlockType >::Page::block, DataStructures::MemoryPool< MemoryBlockType >::Page::next, DataStructures::MemoryPool< MemoryBlockType >::MemoryWithPage::parentPage, DataStructures::MemoryPool< MemoryBlockType >::Page::prev, rakFree_Ex, and rakMalloc_Ex.

    {
        int i=0;
        const int bpp = BlocksPerPage();
        page->block=(MemoryWithPage*) rakMalloc_Ex(memoryPoolPageSize, file, line);
        if (page->block==0)
            return false;
        page->availableStack=(MemoryWithPage**)rakMalloc_Ex(sizeof(MemoryWithPage*)*bpp, file, line);
        if (page->availableStack==0)
        {
            rakFree_Ex(page->block, file, line );
            return false;
        }
        MemoryWithPage *curBlock = page->block;
        MemoryWithPage **curStack = page->availableStack;
        while (i < bpp)
        {
            curBlock->parentPage=page;
            curStack[i]=curBlock++;
            i++;
        }
        page->availableStackSize=bpp;
        page->next=availablePages;
        page->prev=prev;
        return true;
    }
template<class MemoryBlockType>
void DataStructures::MemoryPool< MemoryBlockType >::Release ( MemoryBlockType *  m,
const char *  file,
unsigned int  line 
)

Definition at line 146 of file DS_MemoryPool.h.

References DataStructures::MemoryPool< MemoryBlockType >::Page::availableStack, DataStructures::MemoryPool< MemoryBlockType >::Page::availableStackSize, DataStructures::MemoryPool< MemoryBlockType >::Page::block, DS_MEMORY_POOL_MAX_FREE_PAGES, DataStructures::Page< KeyType, DataType, order >::next, DataStructures::MemoryPool< MemoryBlockType >::Page::next, DataStructures::MemoryPool< MemoryBlockType >::MemoryWithPage::parentPage, DataStructures::MemoryPool< MemoryBlockType >::Page::prev, RakAssert, and rakFree_Ex.

    {
#ifdef _DISABLE_MEMORY_POOL
        rakFree_Ex(m, file, line);
        return;
#else
        // Find the page this block is in and return it.
        Page *curPage;
        MemoryWithPage *memoryWithPage = (MemoryWithPage*)m;
        curPage=memoryWithPage->parentPage;

        if (curPage->availableStackSize==0)
        {
            // The page is in the unavailable list so move it to the available list
            curPage->availableStack[curPage->availableStackSize++]=memoryWithPage;
            unavailablePagesSize--;

            // As this page is no longer totally empty, move it to the end of available pages
            curPage->next->prev=curPage->prev;
            curPage->prev->next=curPage->next;
            
            if (unavailablePagesSize>0 && curPage==unavailablePages)
                unavailablePages=unavailablePages->next;
            
            if (availablePagesSize++==0)
            {
                availablePages=curPage;
                curPage->next=curPage;
                curPage->prev=curPage;
            }
            else
            {
                curPage->next=availablePages;
                curPage->prev=availablePages->prev;
                availablePages->prev->next=curPage;
                availablePages->prev=curPage;   
            }
        }
        else
        {
            curPage->availableStack[curPage->availableStackSize++]=memoryWithPage;

            if (curPage->availableStackSize==BlocksPerPage() &&
                availablePagesSize>=DS_MEMORY_POOL_MAX_FREE_PAGES)
            {
                // After a certain point, just deallocate empty pages rather than keep them around
                if (curPage==availablePages)
                {
                    availablePages=curPage->next;
                    RakAssert(availablePages->availableStackSize>0);
                }
                curPage->prev->next=curPage->next;
                curPage->next->prev=curPage->prev;
                availablePagesSize--;
                rakFree_Ex(curPage->availableStack, file, line );
                rakFree_Ex(curPage->block, file, line );
                rakFree_Ex(curPage, file, line );
            }
        }
#endif
    }
template<class MemoryBlockType >
void DataStructures::MemoryPool< MemoryBlockType >::SetPageSize ( int  size)

Definition at line 88 of file DS_MemoryPool.h.

    {
        memoryPoolPageSize=size;
    }

Member Data Documentation

template<class MemoryBlockType>
Page* DataStructures::MemoryPool< MemoryBlockType >::availablePages [protected]

Definition at line 64 of file DS_MemoryPool.h.

template<class MemoryBlockType>
int DataStructures::MemoryPool< MemoryBlockType >::availablePagesSize [protected]

Definition at line 65 of file DS_MemoryPool.h.

template<class MemoryBlockType>
int DataStructures::MemoryPool< MemoryBlockType >::memoryPoolPageSize [protected]

Definition at line 66 of file DS_MemoryPool.h.

template<class MemoryBlockType>
Page * DataStructures::MemoryPool< MemoryBlockType >::unavailablePages [protected]

Definition at line 64 of file DS_MemoryPool.h.

template<class MemoryBlockType>
int DataStructures::MemoryPool< MemoryBlockType >::unavailablePagesSize [protected]

Definition at line 65 of file DS_MemoryPool.h.


The documentation for this class was generated from the following file:

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net