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

Reasons for wanting custom malloc or allocators?

10 posts in this topic

Hello,

 

In C, users of a library or sometimes not happy when malloc, realloc and free are used to allocate the output.

 

In C++, the same applies I guess (needing custom allocators for the STL).

 

I'm basically wondering why?

 

If for mobile development or other small devices, doesn't the compiler for those devices use a proper malloc/realloc/free for that device?

 

If for a memory pool: why is it useful to also use your memory pool for the allocations another library you're using is doing?

 

Apart from tiny devices and memory pools, are there other possible reasons I missed why you'd want something else than malloc, on a desktop computer? If so, which reasons are this?

Knowing why will make me take these usages into account better when designing an API.

 

Thanks!

Edited by Lode
0

Share this post


Link to post
Share on other sites

Maybe this article will answer your questions: http://www.gamedev.net/page/resources/_/technical/general-programming/c-custom-memory-allocation-r3010 But generally you want a custom allocator because then you have control over what it does, and you can have different allocations in the back end, think heap allocator and slot allocator. A slot allocator is far faster then a heap allocator as all the slots will have the same size, so there generally is no searching for free slots big enough in this allocator.

Edited by NightCreature83
0

Share this post


Link to post
Share on other sites

The most important reason for me is debugging and profiling. If my code (and the libraries that I use) all allocate memory through an interface that I control, then I can generate all sorts of information during development. I can track down any memory leaks, visualize fragmentation, track memory usage per system to find waste, spot bits of code that use a lot of memory allocations so I can go and optimize them to use their own pool, etc, etc...

1

Share this post


Link to post
Share on other sites

Which debugging tools do you use?

For example, Valgrind is able to output all kinds of useful information while you use just malloc. What is the difference?

Thanks :)

0

Share this post


Link to post
Share on other sites

FWIW:

it is also possible to gain additional features as well.

 

for example:

being able to tag the types for memory objects (actually fairly useful);

being able to fetch the base or size of a memory object if given a pointer (can also be useful);

being able to reduce the cost of, say, small or fixed-size allocations (many malloc implementations actually deal pretty poorly with small allocations, and oddly also with many larger allocations as well, ...);

potentially also, features to aid with things like detecting leaks and array overruns;

ability to fine-tune performance;

potentially, having features like garbage-collection (this being a bit more controversial, but having a GC doesn't necessarily mean adopting a Java-like memory use model, and the GC can also partly double as a leak-detector, like "hey, if I am reclaiming these objects, you probably leaked them, here is where they came from!", though granted a person could just have it as a leak detector, but the "hard part" between both of them are basically pretty similar);

...

 

now vs Valgrind / etc:

Valgrind is Linux specific IIRC (so is less useful for Windows developers);

AFAICT, the available leak detectors for Windows tend to cost money;

...

0

Share this post


Link to post
Share on other sites
Another reason is pure performance.

For example, if you know that you will allocate 1000 objects in a row and then delete them all at once, you can use a one-way allocator which creates space for all 1000 objects and then just constructs/destructs them as needed and blows away the entire allocation in one shot. This can be far faster than doing 1000 general-purpose allocations and helps alleviate things like fragmentation to boot.

Stack allocators can also be useful (where you can only free the most recently allocated object) and so on.
1

Share this post


Link to post
Share on other sites

Another reason is pure performance.

For example, if you know that you will allocate 1000 objects in a row and then delete them all at once, you can use a one-way allocator which creates space for all 1000 objects and then just constructs/destructs them as needed and blows away the entire allocation in one shot. This can be far faster than doing 1000 general-purpose allocations and helps alleviate things like fragmentation to boot.

Stack allocators can also be useful (where you can only free the most recently allocated object) and so on.

 

When I worked on console games, we would allocate all the memory upfront from the system and use our own allocator to divvy it out. It was a lot faster than going to the system for every allocation and allowed us to place more strict restrictions on memory usage, i.e. this level can only use 200MB, and if it goes above that then crash dump and trace where all the memory is going.

0

Share this post


Link to post
Share on other sites

Stack allocators can also be useful (where you can only free the most recently allocated object) and so on.

Yeah, stack allocators are way more useful than they first seem. In the engine I'm working on at the moment, malloc/new are treated like the global variable that they are, which means their use is avoided as much as possible. If a function needs to allocate memory, it will generally have an allocator passed in as an argument. The vast, vast majority of the time, a scope/stack allocator is used instead of a malloc-esque heap allocator.

Ranked by usage in the engine, the most used is probably Scope/Stack, then Pool, then Stack (without the scope layer), then Malloc. I can literally count malloc usage on one hand.

I actually find scope/stack allocation easier to use/maintain than shared_ptr/new allocation. Leaks are impossible because the scope is a parameter to the allocation call (I use a macro eiNew(scope, Type)(parameters)), there's no clean-up code because the scopes use the RAII pattern, and reasoning about the scope works exactly the same way as reasoning about built-in language scopes, like local variables, etc... It just extends this familiar concept to dynamic allocations.

 

For me, replacing malloc with a different version (dlmalloc, tcmalloc, etc) is nice to be able to do, but isn't that much of a big deal -- when I see "custom memory management", I don't think of "custom written malloc", I think of completely different paradigms for allocation, like pools and stacks wink.png

 

When I worked on console games, we would allocate all the memory upfront from the system and use our own allocator to divvy it out. It was a lot faster than going to the system for every allocation and allowed us to place more strict restrictions on memory usage, i.e. this level can only use 200MB, and if it goes above that then crash dump and trace where all the memory is going.

Yeah this is very common. When I worked on a adventure/platforming game, from the main allocation, we'd allocate three large contiguous chunks for the level to use. Two would be in use at once, and a 3rd would be streaming in the background. Each geographical 'chunk' of the level had to fit within this hard memory limit, but in return, managing the streaming of chunks was dead simple. When a chunk was no longer required, we'd just let it leak (remove all pointers to it's member structures), and then start streaming the next chunk over the top of it. There was no real memory allocation going on.
1

Share this post


Link to post
Share on other sites

It depends on the system. For iOS I think the system memory allocation probably does a better job than anything that you can write yourself.

0

Share this post


Link to post
Share on other sites

I would strongly encourage anyone on this thread to read the paper I wrote on this topic over a decade ago. It just won a Most Influential Paper award but its influence has clearly not spread to this domain...yet.

 

TL;DR - a good malloc is often as fast / faster than your custom allocator because it does the same tricks; "region" allocators can be faster but can leak tons of memory.

 

Title: Reconsidering Custom Memory Allocation (ACM linkdirect PDF linkPowerpoint talk slides), OOPSLA 2002. I've attached the slides in PPT and PDF formats; I highly recommend looking at the PPT version, since it has animations that do not translate well to PDF.

 

Abstract:

 

Programmers hoping to achieve performance improvements often use custom memory allocators. This in-depth study examines eight applications that use custom allocators. Surprisingly, for six of these applications, a state-of-the-art general-purpose allocator (the Lea allocator) performs as well as or better than the custom allocators. The two exceptions use regions, which deliver higher performance (improvements of up to 44%). Regions also reduce programmer burden and eliminate a source of memory leaks. However, we show that the inability of programmers to free individual objects within regions can lead to a substantial increase in memory consumption. Worse, this limitation precludes the use of regions for common programming idioms, reducing their usefulness.We present a generalization of general-purpose and region-based allocators that we call reaps. Reaps are a combination of regions and heaps, providing a full range of region semantics with the addition of individual object deletion. We show that our implementation of reaps provides high performance, outperforming other allocators with region-like semantics. We then use a case study to demonstrate the space advantages and software engineering benefits of reaps in practice. Our results indicate that programmers needing fast regions should use reaps, and that most programmers considering custom allocators should instead use the Lea allocator.

 

StackOverflow discussion here.

 

2

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