36#ifndef _UTILS_DATABUFFER_HXX_
37#define _UTILS_DATABUFFER_HXX_
49#ifdef DEBUG_DATA_BUFFER_FREE
86 return static_cast<uint8_t *
>(&
data_[0]);
103 unsigned *tail_size =
nullptr)
110 if (total_size > curr->
size())
112 total_size -= curr->
size();
122 *tail_size = total_size;
127 }
while (total_size > 0);
142 if (total_size > curr->
size())
145 total_size -= curr->
size();
166 unsigned skip, uint8_t **ptr,
unsigned *available)
169 while (curr->
size() <= skip)
171 skip -= curr->
size();
175 *ptr = curr->
data() + skip;
176 *available = curr->
size() - skip;
180#ifdef DEBUG_DATA_BUFFER_FREE
185 check_db_ownership(
this);
200using DataBufferPtr = std::unique_ptr<DataBuffer, BufferDelete<uint8_t[]>>;
206#ifdef DEBUG_DATA_BUFFER_FREE
274 unsigned tail_size = 0;
486 size_t bytes_left =
size_ - len;
493 size_t bytes_transferred_from_tail_buffer =
tail_->
size() - bytes_left;
499 skip_ = bytes_transferred_from_tail_buffer;
502 ret.
free_ = -bytes_transferred_from_tail_buffer;
514 recvd->reserve(recvd->size() + len);
524 recvd->append((
char *)ptr, available);
571 else if (add_link &&
tail_->
next() ==
nullptr)
638#ifdef DEBUG_DATA_BUFFER_FREE
645 ssize_t total = l->skip() + l->size();
646 for (
DataBuffer *curr = l->head(); total > 0;)
649 total -= curr->size();
670 uint16_t *payload_size_override()
696#ifdef DEBUG_BUFFER_MEMORY
725 DIE(
"DataBufferPool does not support this type of allocation.");
735 item->
next =
nullptr;
DynamicPool * mainBufferPool
main buffer pool instance
void * g_current_alloc
This pointer will be saved for debugging the current allocation source.
See OSMutexLock in os/OS.hxx.
Abstract base class for all Buffers.
uint16_t size_
size of data in bytes
Base class for all QMember types that hold data in an expandable format.
friend class DataBuffer
Allow DataBuffer access to our constructor.
Buffer< T > * ref()
Add another reference to the buffer.
void unref()
Decrement count.
Proxy Pool that can allocate DataBuffer objects of a certain size.
uint16_t payloadSize_
Number of bytes that need to be stored in each buffer.
size_t free_items(size_t size) override
Number of free items in the pool for a given allocation size.
size_t free_items() override
Number of free items in the pool.
void alloc(DataBuffer **result)
Get a free item out of the pool with untyped data of the size specified in the constructor.
void alloc(DataBufferPtr *result)
Get a free item out of the pool with untyped data of the size specified in the constructor.
void free(BufferBase *item) override
Function called when a buffer refcount reaches zero.
BufferBase * alloc_untyped(size_t size, Executable *flow) override
Internal helper funciton used by the default Buffer allocimplementation.
Specialization of the Buffer class that is designed for storing untyped data arrays.
void unref_all(unsigned total_size)
Releases one reference to all blocks of this buffer.
void set_size(uint16_t s)
Overrides the size of the data buffer.
DataBuffer * ref_all(unsigned total_size, DataBuffer **tail=nullptr, unsigned *tail_size=nullptr)
Acquires one reference to all blocks of this buffer.
DataBuffer * get_read_pointer(unsigned skip, uint8_t **ptr, unsigned *available)
Helper function to read out data from a linked data buffer.
void set_next(DataBuffer *n)
Sets the linking pointer of the DataBuffer to a target buffer.
An object that can be scheduled on an executor to run.
A class that keeps ownership of a chain of linked DataBuffer references.
bool try_append_from(const LinkedDataBufferPtr &o, bool add_link=false)
Attempt to combine *this with o into a single LinkedDataBufferPtr this.
void reset()
Deallocates the current content (by releasing the references).
uint8_t * data_write_pointer()
int16_t free_
If >= 0: How many free bytes are there in the tail buffer.
void data_read_advance(size_t len)
Advances the head pointer.
void reset(DataBuffer *buf)
Clears the current contents and replaces it with the empty buf.
DataBuffer * tail() const
size_t size_
How many bytes we have filled in (counting starts at head.data() + skip_).
LinkedDataBufferPtr(LinkedDataBufferPtr &&o)
Move constructor. Takes the ownership that o has. Leaves o as empty.
void reset(const LinkedDataBufferPtr &o, ssize_t size=-1)
Takes a reference of o, taking a prefix of len size (or all the data).
DataBuffer * head_
First buffer in the chain. This is the root of the ownership.
LinkedDataBufferPtr transfer_head(size_t len)
Transfers the ownership of the prefix of this buffer.
void append_empty_buffer(DataBuffer *buf)
Adds an empty buffer to the end of this buffer chain.
void reset(DataBuffer *buf, unsigned skip, unsigned size)
Set to a single data buffer.
void append_to(std::string *recvd) const
Appends all content in this buffer to an std::string.
DataBuffer * head() const
void data_write_advance(size_t len)
Advances the tail pointer after a write occurred into the tail.
void operator=(LinkedDataBufferPtr &&o)
Move assignment operator.
DataBuffer * tail_
Last buffer in the chain. This is where we can extend the owned bytes.
void clear()
Internal helper function of constructors and reset functions.
uint16_t skip_
How many bytes to skip in the head buffer.
const uint8_t * data_read_pointer(size_t *len)
Retrieves a pointer where data can be read out of the buffer.
LinkedDataBufferPtr(const LinkedDataBufferPtr &)=delete
We do not permit default copy operation.
Using this class as a base class will cause the given class to have all its instances linked up in a ...
Pool of previously allocated, but currently unused, items.
virtual void free(BufferBase *item)=0
Release an item back to the free pool.
virtual BufferBase * alloc_untyped(size_t size, Executable *flow)=0
Untyped buffer allocation method, used be descendants.
virtual size_t free_items()=0
Number of free items in the pool.
friend class DataBufferPool
DataBufferPool proxies to a base Pool.
friend class BufferBase
Allow BufferBase to access this class.
QMember * next
pointer to the next member in the queue
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
#define DIE(MSG)
Unconditionally terminates the current process with a message.