87 auto lock = _use_count->get_lock_guard();
88 _use_count->foreach ([
this](
auto it)
89 { _old_entries.insert(it.first);
return true; });
90 _use_count->insert(
nullptr, get_initial_use_count() + 1);
97 auto lock = _use_count->get_lock_guard();
102 _use_count->foreach ([
this, &done](
auto it)
104 if (_old_entries.count(it.first) == 0)
106 if (--it.second == 0)
108 CBEAM_LOG_DEBUG(
"cbeam::container::container::stable_reference_buffer: Deallocating " +
convert::to_string((
void*)it.first) +
" in context of destruction of class delay_deallocation");
110 _use_count->erase(it.first);
116 _old_entries.insert(it.first);
117 CBEAM_LOG_DEBUG(
"cbeam::container::container::stable_reference_buffer: Removed reference to " +
convert::to_string((
void*)it.first) +
" (" + std::to_string(it.second) +
" left) in context of destruction of class delay_deallocation");
125 _use_count->insert(
nullptr, get_initial_use_count() - 1);
130 std::set<uint8_t*> _old_entries;
131 std::shared_ptr<stable_interprocess_map<uint8_t*, int>> _use_count{get_use_count()};
145 _use_count->update_or_insert(
148 get_initial_use_count());
156 _use_count->update_or_insert(
159 get_initial_use_count());
180 "cbeam::container::container::stable_reference_buffer: memory address " +
convert::to_string((
void*)address) +
" was not created by cbeam::stable_reference_buffer");
190 throw cbeam::error::runtime_error(
"cbeam::container::container::stable_reference_buffer: copy constructor has been passed a default constructed (therefore invalid) instance of Memory");
195 _use_count->update(
_buffer, [
this, &other](
int& count)
202 void append(
const void* bufferToAppend,
const size_t lengthOfBuffer)
override
204 auto lock = _use_count->get_lock_guard();
208 throw cbeam::error::logic_error(
"cbeam::container::container::stable_reference_buffer::append: This instance was created from a raw pointer without knowledge of its buffer length, so appending is not possible.");
219 _use_count->insert(
_buffer, _use_count->at(old_buffer));
220 _use_count->erase(old_buffer);
224 _use_count->insert(
_buffer, get_initial_use_count());
252 CBEAM_LOG(
"Error: Attempt to access the raw pointer via cbeam::container::stable_reference_buffer::safe_get() without adequate reference count. This operation is blocked and returns a nullptr to prevent unsafe memory access, as the buffer could be invalidated upon destruction of this instance.");
266 throw cbeam::error::runtime_error(
"cbeam::container::container::stable_reference_buffer: copy assignment operator has been passed a default constructed (therefore invalid) instance of Memory");
269 auto lock = _use_count->get_lock_guard();
274 _use_count->update(
_buffer, [
this, &other](
auto& count)
288 auto lock = _use_count->get_lock_guard();
298 _use_count->insert(
_buffer, get_initial_use_count());
314 return use_count->count((uint8_t*)address) == 1;
325 return _use_count->at_or_default(
_buffer, 0);
333 auto lock = _use_count->get_lock_guard();
337 auto updated_value = _use_count->update(
_buffer, [](
auto& count)
340 if (updated_value == 0)
350 if (updated_value < 0)
360 catch (
const std::exception& ex)
362 CBEAM_LOG(
"cbeam::container::container::stable_reference_buffer: ");
373 auto lock = _use_count->get_lock_guard();
388 static std::shared_ptr<stable_interprocess_map<uint8_t*, int>> get_use_count()
391 "cbeam::memory::stable_reference_buffer::_use_count",
398 static int get_initial_use_count()
400 return get_use_count()->at_or_default(
nullptr, 1);
418 std::shared_ptr<stable_interprocess_map<uint8_t*, int>> _use_count{get_use_count()};
virtual void * get() const noexcept
return a pointer to the managed memory block
Definition buffer.hpp:141
virtual void swap(buffer &other) noexcept
Swaps the contents of this shared_buffer with another shared_buffer.
Definition buffer.hpp:156
virtual void append(const void *buffer_to_append, const std::size_t length_of_buffer)
append the given buffer to the end of the current buffer. If there is no current buffer yet,...
Definition buffer.hpp:96
buffer()=default
Will not create any memory block. Use append to create one or append bytes to an existing one.
std::size_t _size
Definition buffer.hpp:163
virtual void reset() noexcept
Resets the shared_buffer instance, deallocating the managed memory block.
Definition buffer.hpp:147
uint8_t * _buffer
Definition buffer.hpp:164
virtual buffer & operator=(const buffer &other)
make a deep copy of the other buffer, overwriting the content of this buffer
Definition buffer.hpp:118
virtual std::size_t size() const noexcept
returns the size of the buffer in bytes
Definition buffer.hpp:112
~delay_deallocation() noexcept
Exit the protected scope, potentially leading to deallocation of memory.
Definition stable_reference_buffer.hpp:95
delay_deallocation()
Begin a scope that prevents deallocation of memory created within it.
Definition stable_reference_buffer.hpp:85
stable_reference_buffer & operator=(const buffer &other) override
make a deep copy of the other buffer, overwriting the content of this buffer
Definition stable_reference_buffer.hpp:281
~stable_reference_buffer() noexcept override
deallocate the managed memory block, in case this instance olds the last reference to it....
Definition stable_reference_buffer.hpp:165
stable_reference_buffer()
Definition stable_reference_buffer.hpp:134
stable_reference_buffer(const buffer &base)
Definition stable_reference_buffer.hpp:153
stable_reference_buffer(const void *address)
create a managed memory block from address. If this address is a known address, it only increases the...
Definition stable_reference_buffer.hpp:173
void swap(stable_reference_buffer &other)
Swaps the contents of this shared_buffer with another shared_buffer.
Definition stable_reference_buffer.hpp:371
size_t use_count() const
Get the use count of the managed memory block.
Definition stable_reference_buffer.hpp:319
static bool is_known(const void *address)
return if the given address is one of the managed addresses
Definition stable_reference_buffer.hpp:305
void append(const void *bufferToAppend, const size_t lengthOfBuffer) override
append the given buffer to the end of the current buffer. If there is no current buffer yet,...
Definition stable_reference_buffer.hpp:202
stable_reference_buffer & operator=(const stable_reference_buffer &other)
copy assignment means that the copied instance increases the reference count of the managed memory ad...
Definition stable_reference_buffer.hpp:260
stable_reference_buffer(const stable_reference_buffer &other)
copy construction means that the copied instance increases the reference count of the managed memory ...
Definition stable_reference_buffer.hpp:186
void reset() noexcept override
Resets the shared_buffer instance, potentially deallocating the managed memory block.
Definition stable_reference_buffer.hpp:329
stable_reference_buffer(const std::size_t size, const size_t size_of_type=1)
Create a managed memory block with optional element size.
Definition stable_reference_buffer.hpp:142
void * safe_get() const noexcept
Provides safe access to the raw pointer of the managed memory buffer.
Definition stable_reference_buffer.hpp:248
A Cbeam-specific logic error that also behaves like std::logic_error.
Definition logic_error.hpp:46
A Cbeam-specific runtime error that also acts like std::runtime_error.
Definition runtime_error.hpp:46
Definition singleton.hpp:153
#define CBEAM_LOG(s)
Logs a message using cbeam::logging::log_manager.
Definition log_manager.hpp:124
#define CBEAM_LOG_DEBUG(s)
Logs a debug message if CBEAM_DEBUG_LOGGING is enabled.
Definition log_manager.hpp:138
process_id_type get_current_process_id()
Retrieves the current process's identifier in a platform-independent manner.
Definition process.hpp:57
Offers advanced container types with unique approaches to stability and interprocess sharing....
Definition buffer.hpp:44
std::string to_string(const container::buffer &b)
Creates a std::string from the contents of a container::buffer.
Definition buffer.hpp:42