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

Implementing custom memory allocators

6 posts in this topic

Last year there was a great article on custom general memory allocators: http://www.gamedev.net/page/resources/_/technical/general-programming/c-custom-memory-allocation-r3010

 

I decided to take a hand at modernizing and fully implementing the article for my own use: https://github.com/cgikoray/Nemesis-Memory I am looking to see if anyone has comments or criticisms of my work thus far, as I would love to improve it as much as I can. Thank you anyone in advance, I am open to all opinions. Be as harsh as you wish!

0

Share this post


Link to post
Share on other sites

Just for purposes of education, one of the issues I have found is that my use of lock and unlock inside my concrete allocate() and deallocate() functions, currently can result result in a deadlock due to the fact that I have a return branch right after unlocking a mutex. To solve this I have to follow the principle of RAII and use std::lock_guard from the concurrency library of C++11. I'll probably end up still functionally using my hybrid_lock as a mutex concept for the lock_guard so that I can use spin locking via atomics.

0

Share this post


Link to post
Share on other sites

To solve this I have to follow the principle of RAII and use std::lock_guard from the concurrency library of C++11. I'll probably end up still functionally using my hybrid_lock as a mutex concept for the lock_guard so that I can use spin locking via atomics.


That is the whole point of those RAII wrapper classes. Never take a lock without them.


So far as allocators and threading in general:

High-performance algorithms and locking/synchronization don't play well together. Every time you take a lock you're hitting some overhead (how much depends on the type of lock) and every time a thread is stuck waiting on a lock you're throwing away CPU cycles. When working with multiple threads you really really want to isolate the threads as much as possible; not doing this is one of the reasons people think threading is so hard (it's really not). Use algorithms that allow the threads to work as independently of each other as possible.

Typical modern general-purpose memory allocators make use of per-thread caches. This allows most allocations/deallocations on a thread to operate without every needing to touch a lock or any shared data structure.

A more thorough overview of one such allocator is http://jamesgolick.com/2013/5/19/how-tcmalloc-works.html.

See also the excellent jemalloc

More constrained platforms might not be able to pay the overhead in memory "waste" of thread caches. Heavy threading or memory allocation aren't super ocmmon on such platforms (in games, anyway), though.

Specific allocators for various needs are often single-threaded in games since each instance of an allocator is typically only used by a single system running on a specific thread. Thread safety in these kinds of allocators is mostly just about ensuring only a single thread uses them, not allowing multiple threads to use them.
2

Share this post


Link to post
Share on other sites
Some quick-glance comments:

It might not play nicely with polymorphic arrays, but then again, I don't know if regular new[]/delete[] work correctly in this case either!
class Base { virtual ~Base(){}; }; class Derived : public Base { int foo[42]; };
Base* ptr = create_array<Derived>(1);
destroy_array(ptr);//uh oh, might use the wrong header_size
 
With linear_allocator, it's impossible to destruct any created objects (without asserting inside deallocate).

Should the pool_allocator assert that the requested size is smaller or equal to m_type_size?

IMHO, the constant use of the preprocessor makes the code harder to read:
#ifdef _DEBUG
    assert(condition && "message");
#endif
I would instead make your own assertion macro:
#ifndef NEMESIS_ASSERT
# ifdef _DEBUG
#  define NEMESIS_ASSERT( condition, message ) assert(condition && message)
# else
#  define NEMESIS_ASSERT( condition, message )
# endif
#endif
1

Share this post


Link to post
Share on other sites

 

To solve this I have to follow the principle of RAII and use std::lock_guard from the concurrency library of C++11. I'll probably end up still functionally using my hybrid_lock as a mutex concept for the lock_guard so that I can use spin locking via atomics.


That is the whole point of those RAII wrapper classes. Never take a lock without them.


So far as allocators and threading in general:

High-performance algorithms and locking/synchronization don't play well together. Every time you take a lock you're hitting some overhead (how much depends on the type of lock) and every time a thread is stuck waiting on a lock you're throwing away CPU cycles. When working with multiple threads you really really want to isolate the threads as much as possible; not doing this is one of the reasons people think threading is so hard (it's really not). Use algorithms that allow the threads to work as independently of each other as possible.

Typical modern general-purpose memory allocators make use of per-thread caches. This allows most allocations/deallocations on a thread to operate without every needing to touch a lock or any shared data structure.

A more thorough overview of one such allocator is http://jamesgolick.com/2013/5/19/how-tcmalloc-works.html.

See also the excellent jemalloc

More constrained platforms might not be able to pay the overhead in memory "waste" of thread caches. Heavy threading or memory allocation aren't super ocmmon on such platforms (in games, anyway), though.

Specific allocators for various needs are often single-threaded in games since each instance of an allocator is typically only used by a single system running on a specific thread. Thread safety in these kinds of allocators is mostly just about ensuring only a single thread uses them, not allowing multiple threads to use them.

 

 

Sean,

 

Thank you for the great advice. I have actually been asking myself the question, "Do I want to support threaded allocations/deallocations at all?" The entire purpose of this library is to provide functionality for specific types of allocation patterns, and I think that limiting their use to "same thread context" allocations might be only beneficial to my design choices. I found in performance tests that the introduction of mutex locks to my allocation algorithms basically proved that I should just use malloc and new anyway.

 

Thank you for the links, definitely going to look into them.

0

Share this post


Link to post
Share on other sites

Some quick-glance comments:

It might not play nicely with polymorphic arrays, but then again, I don't know if regular new[]/delete[] work correctly in this case either!

class Base { virtual ~Base(){}; }; class Derived : public Base { int foo[42]; };
Base* ptr = create_array<Derived>(1);
destroy_array(ptr);//uh oh, might use the wrong header_size
 
With linear_allocator, it's impossible to destruct any created objects (without asserting inside deallocate).

Should the pool_allocator assert that the requested size is smaller or equal to m_type_size?

IMHO, the constant use of the preprocessor makes the code harder to read:
#ifdef _DEBUG
    assert(condition && "message");
#endif
I would instead make your own assertion macro:
#ifndef NEMESIS_ASSERT
# ifdef _DEBUG
#  define NEMESIS_ASSERT( condition, message ) assert(condition && message)
# else
#  define NEMESIS_ASSERT( condition, message )
# endif
#endif

 

 

Hodgman, 
 
Thank you for the comments -- regarding the linear allocator, I do in fact believe that this is an oversight on my part. The purpose of the allocator is to have a lot of objects at once all with the same lifetime. I am going to change destroy_all() to fit this, and loop through the allocated objects invoking their destructors.
 
The pool allocator should ABSOLUTELY be asserting that particular oversight as well.
 
And I agree that my assertion code needs more work. I already have enough preprocessor work as it is and should aim to make the code more readable. I certainly would like to isolate preprocessor directives as much as I can, as you have suggested.
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