Page MenuHome

C++ allocator concept class based on guardedalloc, for use with STL containers.
AcceptedPublic

Authored by Lukas Tönne (lukastoenne) on Dec 22 2015, 5:57 PM.

Details

Summary

A custom 'allocator' template argument can be used with STL containers to control
their internal memory allocation. By default this uses the standard new/delete via
the std::allocator implementation. By providing an implementation in guardedalloc
we can ensure proper accounting for allocated memory using standard Blender mechanisms.

Note that the definition of a template class in MEM_guardedalloc.h means that any C++
code using this header now must do the #include "MEM_guardedalloc.h" statement outside
of the typical extern "C" block! While the code is only compiled for C++ linkage,
the #ifdef __cplusplus conditional still is true even when using an extern "C"
block, which means the compiler will attempt C linkage on C++ code and fail.

By using the extern "C++" declaration we can avoid linker errors even when the new C++ code is included inside a extern "C" block.

Diff Detail

Repository
rB Blender
Branch
guardedalloc_cpp_allocator_concept

Event Timeline

Lukas Tönne (lukastoenne) retitled this revision from to C++ allocator concept class based on guardedalloc, for use with STL containers..
Lukas Tönne (lukastoenne) updated this object.

Better handling of C++ linking issues by use of extern "C++".

How does this work when one C++ file includes MEM_guardedalloc.h and another C++ file doesn't... which deletes memory allocated from the file that file?

Also, should this use WITH_CXX_GUARDEDALLOC define? (already used in many places)

intern/guardedalloc/MEM_guardedalloc.h
266

Would it be possible to pass the type as a string? (one of the main benefits with guarded-alloc is to ID allocations when guarded-allocs enabled)

The allocator concept can be used optionally on a per-type basis when you define a STL container:

typedef std::vector<int, GuardedAlloc<int> > MyIntList;

So whenever an instance of MyIntList is deleted it uses the GuardedAllocator without the deleter having to worry about the correct deallocator.

WITH_CXX_GUARDEDALLOC could be used, yes. Although i would then expect it to also toggle the MEM_CXX_CLASS_ALLOC_FUNCS overloads.

Sergey Sharybin (sergey) edited edge metadata.

While it's not fully guarded allocator, it's already much better than nothing to track down issues with unfreed elements in a list and/or track down memory peaks when using STL collections.

I wouldn't move it inside WITH_CXX_GUARDEDALLOC. This define will enable MEM_CXX_CLASS_ALLOC_FUNCS which are causing issues with certain video driver (due to some allocation happening before our guarded allocator is enabled, causing freeing unknown blocks at exit). Having this guarded class enabled for all builds is also something we have to aim for.

Don't have time to check compilation, but code-wise seems all correct to me, also is rather handy thing.

intern/guardedalloc/MEM_guardedalloc.h
266

Not really, you can only specify a type which you want to be used for the allocations and you can not use string literals for the template arguments because they aren't guaranteed to be a const expression.

You can kind of hack this by using global-scope strings, but that's ugly and unfun.

This revision is now accepted and ready to land.Dec 24 2015, 2:50 PM