Skip to content

simplify handling for mixed allocator usage #945

@olesenm

Description

@olesenm

Coming back to this (was partly discussed off-line elsewhere). I am looking at supporting the use of umpire for large fields, as well as standard new[] for smaller fields. Now run into the slight problem of needing to handle the deletions differently. This is one idea:

namespace Foam::memoryPool
{

//- Deallocate iff actually managed by Umpire
inline bool deallocate_if(void *ptr)
{
    if (ptr)
    {
        if
        (
            auto& rm = umpire::ResourceManager::getInstance();
            rm.hasAllocator(ptr)
        )
        {
            rm.deallocate(ptr);
            return true;
        }
    }

    return false;
}

} // End namespace Foam::memoryPool

It's not really pretty, but could perhaps be made more efficient if the hasAllocator returned a pointer to the Allocator instead (which could also be tested as bool). Then the code would look like this:

namespace Foam::memoryPool
{

//- Deallocate iff actually managed by Umpire
inline bool deallocate_if(void *ptr)
{
    // Assuming we could have an internal short-circuit for nullptr
    if (auto* handler = umpire::ResourceManager::getInstance().hasAllocator(ptr))
    {
         handler->deallocate(ptr);
         return true;
    }
    return false;
}

} // End namespace Foam::memoryPool

This would avoid calling findAllocatorForPointer twice (not sure how expensive that is).

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions