• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
EngineProgrammer

Memory Management GameEngine

2 posts in this topic

Hi everyone!!

I'm learning memory management for about 2 days now and I'm trying to create my own memory management for my game engine.

First I start with heap allocation.
If someone has some time to check if I'm doing OK please have a look at the code below.

But now that I have a Heap Allocator. How should I continue?
I heard something about pools and I've read a little bit about them but still don't have a clue what they should do.
Also, how can I make my HeapAllocator multi-thread safe?
I assume some of my heaps will give errors when I use multithreads?


Thanks in advance!
Kind regards,
Jonathan

This is the code I have at the moment:
[CODE]
///////////////////////////////////////////////////////////////////////////
// HeapAllocator.h
// Date: 21/09/2012
// Copyrights Bollaert Jonathan
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
#ifndef HEAP_ALLOCATOR_H
#define HEAP_ALLOCATOR_H

#pragma once

#include "Types.h"
#include "Assert.h"
#include <iostream>
using namespace std;

template<typename T> CINLINE
T Align(T nData, size_t nAlign)
{
// More information: http://en.wikipedia.org/wiki/Data_structure_alignment
ASSERT((nAlign & (nAlign - 1)) == 0);
size_t size = ((size_t)nData + (nAlign - 1)) & ~(nAlign - 1);
return T(size);
}

namespace C2D1HeapAlloc
{
///////////////////////////////////////////////
// Heap System Allocator
///////////////////////////////////////////////
class HeapSysAllocator
{
public:
static CVOID* SysAlloc(size_t size) {
cout << "HeapSysAllocator has allocated a heap with size: " << size << " bytes." << endl;
return malloc(size);
}
static CVOID SysDealloc(void* ptr) {
cout << "HeapSysAllocator has deallocated a heap." << endl;
free(ptr);
}
};

///////////////////////////////////////////////
// Memory Usage
///////////////////////////////////////////////
struct MemoryUsage
{
size_t m_MemAllocated, m_MemUsed;
MemoryUsage(size_t nAlloc = 0, size_t nUsed = 0)
:m_MemAllocated(nAlloc), m_MemUsed(nUsed)
{
Validate();
}
CINLINE CVOID Validate() {
ASSERT(m_MemUsed <= m_MemAllocated);
}
CINLINE CVOID ClearMemory(){
m_MemAllocated = m_MemUsed = 0;
}
CINLINE const size_t GetMemoryFree() const {
return m_MemAllocated - m_MemUsed;
}
CINLINE const size_t GetMemoryAllocated() const {
return m_MemAllocated;
}
CINLINE const size_t GetMemoryUsed() const {
return m_MemUsed;
}
};

///////////////////////////////////////////////
// Round up to next multiple of nAlign.
///////////////////////////////////////////////
CINLINE const size_t RoundUpTo(size_t nSize, size_t nAlign)
{
ASSERT(nAlign > 0);
nSize += nAlign - 1;
return nSize - nSize % nAlign;
}

///////////////////////////////////////////////
// Heap Allocator
///////////////////////////////////////////////
template<CBOOL bMultiPage = true, typename SysAl = HeapSysAllocator>
class HeapAllocator: private SysAl
{
public:
enum {DefaultAlignment = sizeof(void*)}; // 4 bytes
enum {DefaultPageSize = 0x1000}; // 4 Kb
HeapAllocator(size_t nPageSize = DefaultPageSize)
:m_PageSize(nPageSize ? nPageSize : DefaultPageSize)
,m_PageList(nullptr)
{
cout << "HeapAllocator is being constructed on the stack with size: " << sizeof(*this) << " bytes." << endl;
}
~HeapAllocator()
{
cout << "HeapAllocator is being deallocated." << endl;
Clear();
}

////////////////////////////
// Raw memory allocation
////////////////////////////
void* Allocate(size_t nSize, size_t nAlign = DefaultAlignment)
{
for(;;)
{
cout << "Attempting to find a heap..." << endl;
// Try allocating from head page first
if(m_PageList)
{
cout << "Heap has been found." << endl;
cout << "Attempting to allocate: " << nSize << " bytes." << endl;
if(void* ptr = m_PageList->Allocate(nSize, nAlign)) {
m_Memory.m_MemUsed += nSize;
return ptr;
}
if(m_PageList->m_pNext && m_PageList->m_pNext->GetMemoryFree() > m_PageList->GetMemoryFree())
{
SortPage(m_PageList);
Validate();
if(void* ptr = m_PageList->Allocate(nSize, nAlign)) {
m_Memory.m_MemUsed += nSize;
return ptr;
}
}
cout << "Allocating requested memory FAILED." << endl;
if(!bMultiPage)
return 0;
cout << "Heap can not store the required size." << endl;
}
else
{
cout << "No heap has been found!" << endl;
}
// Allocate the new page of the required size.
int nAllocSize = sizeof(PageNode) + nAlign - 1 + RoundUpTo(nSize, m_PageSize);
cout << "Attempting to allocate a heap..." << endl;
void* pAlloc = this->SysAlloc(nAllocSize);
PageNode* pPageNode = new(pAlloc) PageNode(nAllocSize);
// Insert at head of list
pPageNode->m_pNext = m_PageList;
m_PageList = pPageNode;
m_Memory.m_MemAllocated += nAllocSize;
Validate();
}
}
CVOID Deallocate(void* ptr, size_t nSize)
{
cout << "Attempting to deallocate memory..." << endl;
ASSERT(CheckPtr(ptr));
ASSERT(m_Memory.m_MemUsed >= nSize);
m_Memory.m_MemUsed -= nSize;
cout << "A memory with size: " << nSize << " bytes has been deallocated." << endl;
}

////////////////////////////
// Maintenance
////////////////////////////
CINLINE const MemoryUsage GetTotalMemory() const {
m_Memory;
}
struct PageNodeHandle {};
PageNodeHandle* RemovePages() {
// Remove the pages from the object.
PageNodeHandle* pPageNode;
Validate();
pPageNode = m_PageList;
m_PageList = 0;
m_Memory.ClearMemory();
return pPageNode;
}
CVOID FreeMemory(PageNodeHandle* handle)
{
PageNode* pPageNode = static_cast<PageNode*>(handle);
// Loop through all the pages, freeing the memory.
while(pPageNode != nullptr)
{
// Read the "next" pointer before deleting.
PageNode* pNext = pPageNode->m_pNext;
// Delete the current page
this->SysDealloc(pPageNode);
// Mode to the next page in the list
pPageNode = pNext;
}
}
CVOID Clear()
{
PageNodeHandle* pPageNode;
Validate();

// Remove the pages from the object.
pPageNode = RemovePages();

// Loop through all the pages, freeing the memory.
FreeMemory(pPageNode);
}
CVOID Reset()
{
Validate();
size_t nPrevSize = ~0;
for(PageNode** ppPage = &m_PageList; *ppPage; )
{
(*ppPage)->Reset();
if((*ppPage)->GetMemoryAlloc() > nPrevSize) {
// Move page to sorted location near beginning.
SortPage(*ppPage);
// ppPage is now next page, so continue loop.
continue;
}
nPrevSize = (*ppPage)->GetMemoryAlloc();
ppPage = &(*ppPage)->pNext;
}
m_Memory.m_MemUsed = 0;
Validate();
}
CBOOL CheckPtr(void* ptr) const
{
if(!ptr)
return true;
for(PageNode* pNode = m_PageList; pNode; pNode = pNode->m_pNext) {
if(pNode->CheckPtr(ptr))
return true;
}
return false;
}
CVOID Validate()
{
MemoryUsage MemCheck;
for(PageNode* pPage = m_PageList; pPage; pPage = pPage->m_pNext) {
pPage->Validate();
if(pPage != m_PageList && pPage->m_pNext)
ASSERT(pPage->GetMemoryFree() >= pPage->m_pNext->GetMemoryFree());
MemCheck.m_MemAllocated += pPage->GetMemoryAllocated();
MemCheck.m_MemUsed += pPage->GetMemoryUsed();
}
ASSERT(MemCheck.m_MemAllocated == m_Memory.m_MemAllocated);
ASSERT(MemCheck.m_MemUsed >= m_Memory.m_MemUsed);
}

protected:
struct PageNode : PageNodeHandle
{
PageNode* m_pNext;
char* m_pEndAlloc;
char* m_pEndUsed;
CINLINE char* StartUsed() const {
return (char*)(this+1);
}
PageNode(size_t nAlloc) {
m_pNext = nullptr;
m_pEndAlloc = (char*)this + nAlloc;
m_pEndUsed = StartUsed();
}
void* Allocate(size_t nSize, size_t nAlign) {
// Align current mem
char* pNew = Align(m_pEndUsed, nAlign);
if(pNew + nSize > m_pEndAlloc)
return 0;
m_pEndUsed = pNew + nSize;
cout << "Memory has been allocated with size: " << nSize << " bytes." << endl;
return pNew;
}

CINLINE CVOID Reset() {
m_pEndUsed = StartUsed();
}
CINLINE const size_t GetMemoryAllocated() const {
return m_pEndAlloc - (char*)this;
}
CINLINE const size_t GetMemoryUsed() const {
return m_pEndUsed - StartUsed();
}
CINLINE const size_t GetMemoryFree() const {
return m_pEndAlloc - m_pEndUsed;
}
CINLINE CVOID Validate() const {
ASSERT(m_pEndAlloc >= (char*)this);
ASSERT(m_pEndUsed >= StartUsed() && m_pEndUsed <= m_pEndAlloc);
}
CINLINE CBOOL CheckPtr(void* ptr) const {
return (char*)ptr >= StartUsed() && (char*)ptr < m_pEndUsed;
}
};

CVOID SortPage(PageNode* rpPage) {
// Unlink rpPage.
PageNode* pPage = rpPage;
rpPage = pPage->m_pNext;
// Insert into list based on free memory.
PageNode** ppBefore = &m_PageList;
while (*ppBefore && (*ppBefore)->GetMemoryFree() > pPage->GetMemoryFree())
ppBefore = &(*ppBefore)->m_pNext;
// Link before rpList.
pPage->m_pNext = *ppBefore;
*ppBefore = pPage;
}

private:
MemoryUsage m_Memory; // Track memory allocated and used
const size_t m_PageSize; // Pages allocated at this size
PageNode* m_PageList; // All allocated pages
};
}

#endif // HEAP_ALLOCATOR_H
[/CODE]

Some of you may think "I have seen this kind of code before...."
Well got all the code from all of these:
- CryEngine open source SDK
- Standard Library C++ ~by Sutter
- Internet: [url="http://www.cantrip.org/wave12.html"]http://www.cantrip.org/wave12.html[/url]
[url="http://www.bogotobogo.com/cplusplus/memoryallocation.php"] http://www.bogotobogo.com/cplusplus/memoryallocation.php[/url]
[url="http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4079/Allocators-STL.htm"] http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c4079/Allocators-STL.htm[/url]
0

Share this post


Link to post
Share on other sites
[quote name='EngineProgrammer' timestamp='1348241264' post='4982406']
I heard something about pools and I've read a little bit about them but still don't have a clue what they should do.
[/quote]
This is a technique used for special circumstances, it is not always the best solution.

If your application frequently allocates and frees a memory chunk of the same size, then you can arrange to have a list of free memory blocks of this size, possibly in a linked list. This can be very efficient (in time).
0

Share this post


Link to post
Share on other sites
[url="http://molecularmusings.wordpress.com/"]Molecular Musings[/url] has several blogs on memory allocation strategies, including pool allocator and linear allocator.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0