diff options
Diffstat (limited to 'libtransport/includes/hicn/transport/utils/membuf.h')
-rw-r--r-- | libtransport/includes/hicn/transport/utils/membuf.h | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/libtransport/includes/hicn/transport/utils/membuf.h b/libtransport/includes/hicn/transport/utils/membuf.h index 9fc37dd25..4b442dda3 100644 --- a/libtransport/includes/hicn/transport/utils/membuf.h +++ b/libtransport/includes/hicn/transport/utils/membuf.h @@ -23,20 +23,20 @@ #include <hicn/transport/portability/portability.h> #include <hicn/transport/utils/branch_prediction.h> +#include <stdlib.h> #include <atomic> #include <cassert> #include <cinttypes> #include <cstddef> #include <cstring> +#include <iostream> #include <iterator> #include <limits> #include <memory> #include <type_traits> #include <vector> -#include <stdlib.h> - #ifndef _WIN32 TRANSPORT_GNU_DISABLE_WARNING("-Wshadow") #endif @@ -50,6 +50,8 @@ class MemBuf { enum TakeOwnershipOp { TAKE_OWNERSHIP }; enum CopyBufferOp { COPY_BUFFER }; + using Ptr = std::shared_ptr<MemBuf>; + typedef void (*FreeFunction)(void* buf, void* userData); static std::unique_ptr<MemBuf> create(std::size_t capacity); @@ -106,13 +108,14 @@ class MemBuf { FreeFunction freeFn = nullptr, void* userData = nullptr, bool freeOnError = true); - static std::unique_ptr<MemBuf> wrapBuffer(const void* buf, + static std::unique_ptr<MemBuf> wrapBuffer(const void* buf, std::size_t length, std::size_t capacity); - static MemBuf wrapBufferAsValue(const void* buf, + static MemBuf wrapBufferAsValue(const void* buf, std::size_t length, std::size_t capacity) noexcept; - MemBuf(WrapBufferOp op, const void* buf, std::size_t capacity) noexcept; + MemBuf(WrapBufferOp op, const void* buf, std::size_t length, + std::size_t capacity) noexcept; /** * Convenience function to create a new MemBuf object that copies data from a @@ -147,6 +150,8 @@ class MemBuf { std::size_t length() const { return length_; } + void setLength(std::size_t length) { length_ = length; } + std::size_t headroom() const { return std::size_t(data_ - buffer()); } std::size_t tailroom() const { return std::size_t(bufferEnd() - tail()); } @@ -689,6 +694,18 @@ class MemBuf { void* userData = nullptr, bool freeOnError = true); + /** + * Ensure the current MemBuf can hold at least capacity bytes and its + * memory is contiguous + */ + bool ensureCapacity(std::size_t capacity); + + /** + * Ensure packet buffer can hold at least 1500 bytes in contiguous memory and + * fill unused memory with placeholder + */ + bool ensureCapacityAndFillUnused(std::size_t capacity, uint8_t placeholder); + /* * Overridden operator new and delete. * These perform specialized memory management to help support @@ -700,6 +717,12 @@ class MemBuf { void operator delete(void* ptr); void operator delete(void* ptr, void* placement); + /** + * Override operator == and != + */ + bool operator==(const MemBuf& other); + bool operator!=(const MemBuf& other); + // /** // * Iteration support: a chain of MemBufs may be iterated through using // * STL-style iterators over const ByteRanges. Iterators are only |