Comments (12)
Oh, so you want to have a default constructor for allocator_deleter
?
from memory.
Basically yes :), such that I can set the m_data
member to nullptr? Maybe its the wrong approach ?
from memory.
I'm not quite sure why you need it, the constructor you've shown takes an allocator anyway.
from memory.
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.
I see. But is there a way to modify the deleter of a smart pointer?
from memory.
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.
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.
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.
sounds awesome, thanks!
from memory.
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.
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.
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)
- mac build failure - Unable to determine alignment of C++ type long long - no error text HOT 3
- cross-compiling for qnx 32 bit HOT 2
- Wrapping Stateful to Stateless allocators HOT 2
- Compile error when set FOONATHAN_MEMORY_TEMPORARY_STACK_MODE to 1 HOT 1
- Making `container node sizes` optional? HOT 3
- Strange exception from unordered_set with memory_stack, on MSVC HOT 9
- Library triggers bug in MSVC++ v19 (VS 2022) under modern C++ standards, preventing compilation HOT 2
- `memory_pool_collection` carves too little memory from block if there are many buckets HOT 4
- [docs/examples] associative containers HOT 4
- memory_block_stack::owns() seems wrong? HOT 1
- Time for a new release? HOT 1
- problem with std::is_abstract for unique_ptr with allocator inside struct definition HOT 1
- give allocator_polymorphic_deleter a default constructor HOT 1
- Thread safety
- Build issues on MacOS while building tools with static flag HOT 3
- MinGW build fails due to "#warning" HOT 1
- Can a vector use a memory pool with a small_node_pool ?
- Unable to determine node size of C++ container HOT 4
- ARM64 crash when using static block allocator with small types HOT 2
- memory_pool_collection fails to allocate memory HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from memory.