Git Product home page Git Product logo

Comments (12)

foonathan avatar foonathan commented on May 25, 2024

Oh, so you want to have a default constructor for allocator_deleter?

from memory.

gabyx avatar gabyx commented on May 25, 2024

Basically yes :), such that I can set the m_data member to nullptr? Maybe its the wrong approach ?

from memory.

foonathan avatar foonathan commented on May 25, 2024

I'm not quite sure why you need it, the constructor you've shown takes an allocator anyway.

from memory.

gabyx avatar gabyx commented on May 25, 2024

Jeah, exactly, but I want to get rid of this shared_ptr -> I want a CTOR BinaryBuffer() == empty buffer which makes m_data = nullptr but its not that easy because of the deleter.

from memory.

foonathan avatar foonathan commented on May 25, 2024

I see. But is there a way to modify the deleter of a smart pointer?

from memory.

gabyx avatar gabyx commented on May 25, 2024

What I need is

std::unique_ptr<uint8_t[], Deleter> p(nullptr, Deleter{});
p = foo(); // move assigns to p,  p has now a valid deleter, copied from the return value

This is essentially a very related question on SO: https://stackoverflow.com/questions/35317344/unique-ptrs-assignment-operator-copies-a-deleter-stored-by-a-reference-is-it-a
Hm... might std::reference_wrapper help?

from memory.

gabyx avatar gabyx commented on May 25, 2024

If I see correctly: allocator_storage<reference_storage<RawAllocator>, Mutex> is the allocator_reference. If get_allocator in allocator_storage would return a pointer, then we could have a default ctor. The allocator_storage::storage_policy::get_allocator returns a reference, what about returning a pointer?
If that pointer is nullptr, the deleter does nothing

void allocator_storage::deallocate_node(void* ptr, std::size_t size,std::size_t alignment) FOONATHAN_NOEXCEPT
{
    std::lock_guard<actual_mutex> lock(*this);
    auto*  alloc = get_allocator();
    if(alloc)
    {
                traits::deallocate_node(alloc, ptr, size, alignment);
    }
}

If that still fits the design of wrapping a deleter around a RawAllocator concept, i dont know?

from memory.

gabyx avatar gabyx commented on May 25, 2024

Some thoughts: The standart says: http://eel.is/c++draft/unique.ptr#single.dtor

Effects: If get() == nullptr there are no effects. Otherwise get_­deleter()(get()).

So the deleter is not called if it is nullptr, so I suppose, we can store an empty deleter in it.

from memory.

gabyx avatar gabyx commented on May 25, 2024

sounds awesome, thanks!

from memory.

gabyx avatar gabyx commented on May 25, 2024

I have a related question: I store my allocator in the BinaryBuffer as a smart pointer (to make sure it always exists)
Is there already a way of having an allocator_storage which stores a shared_ptr of a RawAllocator. Meaning, the deleter will store not a reference but a std::shared_ptr inside, removing the need to keeping the allocator next to my m_data member.

//! Constructor for a meaningful buffer!
    BinaryBuffer(std::size_t bytes, std::shared_ptr<RawAllocator> allocator)
        : /*m_allocator(allocator) not needed anymore*/ 
        , m_data(foonathan::memory::allocate_unique<uint8_t[]>(allocator, bytes))
// ------------------------------------------------------------->*
//---------------------------------------------------------std::shared_ptr
        , m_bytes(bytes)
    {
    }

from memory.

foonathan avatar foonathan commented on May 25, 2024

Yes, you can write your own StoragePolicy (see the documentation for requirements). However, I won't add one using shared_ptr to the library, as I don't think it's an ideal solution. Usually you just have global variables or allocators in a top level function.

from memory.

gabyx avatar gabyx commented on May 25, 2024

ok thats true, also better, to be sure where its central location is, instead of wrapping it inside a deleter, hiding it

from memory.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.