• 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
SeiryuEnder

Container Types with Static Memory Constraints

7 posts in this topic

I am implementing a set of containers for use with a static memory allocator.
The allocator does not support deletion of individual elements/arrays.

Dynamic Array
Many people like dynamic arrays (std::vector<>), but these containers
are extremely memory inefficient when used with a static memory allocator as
all of the previously used memory cannot be freed until the level ends. I've
alleviated some of the cost by making the main array a list of references and
individually allocating each object with the allocator, though I'm still not a fan of
throwing away memory even if it is on a much smaller scale.

Linked List
A linked list is very memory efficient with a static memory allocator, but the loss
of constant access times is unacceptable for potentially large lists.

Linked Dynamic Array
Right now I'm considering a hybridization of the two, a linked dynamic array
which maintains it's previous memchunk but can still grow by creating a new
node which should maintain fast access times without too much memory.


However, before I create the LDA I want to make sure there is not another
container type which is more appropriate for this scenario. Any ideas?
0

Share this post


Link to post
Share on other sites
[quote name='SeiryuEnder' timestamp='1328113594' post='4908404']
Linked Dynamic Array
Right now I'm considering a hybridization of the two, a linked dynamic array
which maintains it's previous memchunk but can still grow by creating a new
node which should maintain fast access times without too much memory.[/quote]
So in effect, the typical implementation of [url="http://www.cplusplus.com/reference/stl/deque/"]std::deque[/url]?

I don't think the standard mandates a particular implementation, but the typical approach seems to be to allocate fixed size chunks of items, and have an indirection table to find the chunk containing the given index. A fringe benefit is that it allows you to cheaply push/pop elements from both ends of the deque.
1

Share this post


Link to post
Share on other sites
[quote name='SeiryuEnder' timestamp='1328113594' post='4908404']

Dynamic Array
Many people like dynamic arrays (std::vector<>), but these containers
are extremely memory inefficient when used with a static memory allocator as
all of the previously used memory cannot be freed until the level ends. I've
alleviated some of the cost by making the main array a list of references and
individually allocating each object with the allocator, though I'm still not a fan of
throwing away memory even if it is on a much smaller scale.[/quote]

vector can release claimed memory.
The overhead is between 1/2 and 1/3 of total allocation. If using some sort of paging (multiple vectors), then this overhead becomes n/2 of a single page. Even on embedded devices this isn't an issue. IIRC, such allocators are default on iPhones since the start.

[quote]A linked list is very memory efficient with a static memory allocator, but the loss
of constant access times is unacceptable for potentially large lists.[/quote]
Linked list is terribly inefficient. For small objects it can have up to 800% overhead and the overhead is constant. In addition, each node brings additional hidden overhead inside malloc/new.

For allocators, linked lists are used, but they are backed by paged array structure as mentioned above.

[quote]However, before I create the LDA I want to make sure there is not another
container type which is more appropriate for this scenario. Any ideas?
[/quote]
[code]struct Allocator {
Allocator(size_t max_size) : data(max_size) {}

void * alloc(size_t count) {
if (count + offset < data.size()) return NULL;
void * ptr = (void*) &data[offset];
offset + count;
return ptr;
}
private:
vector<char> data;
size_t offset;
};[/code]And you're done. Really, don't overthink it.
0

Share this post


Link to post
Share on other sites
That LDA sounds very similar to a [url="http://www.cplusplus.com/reference/stl/deque/"]std::deque[/url]
0

Share this post


Link to post
[quote name='swiftcoder' timestamp='1328115751' post='4908410']
So in effect, the typical implementation of [url="http://www.cplusplus.com/reference/stl/deque/"]std::deque[/url]?

I don't think the standard mandates a particular implementation, but the typical approach seems to be to allocate fixed size chunks of items, and have an indirection table to find the chunk containing the given index. A fringe benefit is that it allows you to cheaply push/pop elements from both ends of the deque.
[/quote]

Exactly what I was talking about, I wasn't aware of std::deque.
This should make things a lot easier. Thanks for the reference!

[quote name='Antheus' timestamp='1328116100' post='4908412']
[code]struct Allocator {
Allocator(size_t max_size) : data(max_size) {}

void * alloc(size_t count) {
if (count + offset < data.size()) return NULL;
void * ptr = (void*) &data[offset];
offset + count;
return ptr;
}
private:
vector<char> data;
size_t offset;
};[/code]And you're done. Really, don't overthink it.
[/quote]

I think that you're misunderstanding the goal.
This is using a vector to manage data inside of an allocator.
What I'm talking about are containers that use data distributed by an allocator.

The allocator itself is finished, right now I'm just making several container implementations
and designing them to work efficiently with the restrictions that a static memory allocator impose.

The static memory allocator can't release memory.
Therefore, the vector cannot release memory and becomes increasingly inefficient as it grows.
A deque (what I was referring to as an LDA) is able to grow without bleeding memory.

Does this make more sense? Sorry for not explaining properly. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
Perhaps you might want to clarify just what you mean by a "static allocator". Do you mean an allocator object that exists in static storage? Or an allocator that returns memory form a pool of static storage? Or a linear allocator? Or something else?

Incidentally, I'd say that there's little or no problem in not freeing memory until the level ends. Typically you're going to know in advance how much memory a level and all its entities require before the level starts. So allocate that much up front and you're done.
0

Share this post


Link to post
Share on other sites
[quote name='edd²' timestamp='1328117124' post='4908416']
Perhaps you might want to clarify just what you mean by a "static allocator". Do you mean an allocator object that exists in static storage? Or an allocator that returns memory form a pool of static storage? Or a linear allocator? Or something else?

Incidentally, I'd say that there's little or no problem in not freeing memory until the level ends. Typically you're going to know in advance how much memory a level and all its entities require before the level starts. So allocate that much up front and you're done.
[/quote]

Sure, what I mean by a static allocator is an object that allocates a chunk of memory and distributes that memory upon request.

I can see how static in this context could be ambiguous, in this context I mean that the memory is allocated once at the beginning
of the game and wiped at the end of each level.
0

Share this post


Link to post
Share on other sites
[quote name='SeiryuEnder' timestamp='1328118865' post='4908424']

I can see how static in this context could be ambiguous, in this context I mean that the memory is allocated once at the beginning
of the game and wiped at the end of each level.
[/quote]

Which is exactly what my allocator does. It's std::vector, but it never reallocates the memory.

There is no need to release 'data' between levels. Why? You'll just allocate it again.

But if [url="http://stackoverflow.com/questions/3172571/force-a-stdvector-to-free-its-memory"]you insist[/url].
0

Share this post


Link to post
Share on other sites
[quote name='Antheus' timestamp='1328119158' post='4908425']
Which is exactly what my allocator does. It's std::vector, but it never reallocates the memory.

There is no need to release 'data' between levels. Why? You'll just allocate it again.

But if [url="http://stackoverflow.com/questions/3172571/force-a-stdvector-to-free-its-memory"]you insist[/url].
[/quote]

Pseudo-code:
[CODE]
class DynamicArray
{
private:
MemHeap& m_Heap; // Memory allocator

public:
...

void reserve( u32 _capacity )
{
...
// Allocate a new memchunk
m_Data = m_Heap.AllocArray<T*>( m_Capacity );

// Allocate referenced objects
for( u32 i = 0; i < m_Capacity; ++i )
m_Data[i] = m_Heap.Alloc<T>();
...
}

void push_back( const T& _object )
{
if( m_Size == m_Capacity )
reserve( 2 * m_Capacity + 1 );

*m_Data[ m_Size++ ] = _object;
}
};
[/CODE]

[CODE]

void MemHeap::ClearMem()
{
if( m_StaticHeap )
{
CLEARMEM( m_StaticHeap, m_StaticHeapSize );

m_AllocHead = m_StaticHeap;
m_AllocTail = m_StaticHeap + m_StaticHeapSize;
}
}

[/CODE]
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