20 #ifndef _SANDESH_TRANSPORT_TBUFFERTRANSPORTS_H_
21 #define _SANDESH_TRANSPORT_TBUFFERTRANSPORTS_H_ 1
24 #include "boost/scoped_array.hpp"
26 #include <sandesh/transport/TTransport.h>
27 #include <sandesh/transport/TVirtualTransport.h>
30 #define TDB_LIKELY(val) (__builtin_expect((val), 1))
31 #define TDB_UNLIKELY(val) (__builtin_expect((val), 0))
33 #define TDB_LIKELY(val) (val)
34 #define TDB_UNLIKELY(val) (val)
37 namespace contrail {
namespace sandesh {
namespace transport {
62 int32_t
read(uint8_t* buf, uint32_t len) {
63 uint8_t* new_rBase =
rBase_ + len;
65 std::memcpy(buf,
rBase_, len);
75 int32_t
readAll(uint8_t* buf, uint32_t len) {
76 uint8_t* new_rBase =
rBase_ + len;
78 std::memcpy(buf,
rBase_, len);
94 int write(
const uint8_t* buf, uint32_t len) {
95 uint8_t* new_wBase =
wBase_ + len;
97 std::memcpy(
wBase_, buf, len);
107 const uint8_t*
borrow(uint8_t* buf, uint32_t* len) {
124 LOG(ERROR, __func__ <<
": Consume did not follow a borrow.");
144 virtual uint32_t
readSlow(uint8_t* buf, uint32_t len) = 0;
147 virtual int writeSlow(
const uint8_t* buf, uint32_t len) = 0;
154 virtual const uint8_t*
borrowSlow(uint8_t* buf, uint32_t* len) = 0;
196 void initCommon(uint8_t* buf, uint32_t size,
bool owner, uint32_t wPos) {
197 if (buf == NULL && size != 0) {
199 buf = (uint8_t*)std::malloc(size);
201 LOG(ERROR, __func__ <<
": Allocation of " << size <<
" bytes FAILED");
279 if (buf == NULL && sz != 0) {
280 LOG(ERROR, __func__ <<
": TMemoryBuffer given null buffer with "
291 this->
write(buf, sz);
294 LOG(ERROR, __func__ <<
": Invalid MemoryPolicy for TMemoryBuffer");
331 return std::string((
char*)buf, (std::string::size_type)sz);
341 str.append((
char*)buf, sz);
372 this->
swap(new_buffer);
381 this->
swap(new_buffer);
434 uint32_t
readAll(uint8_t* buf, uint32_t len) {
456 void computeRead(uint32_t len, uint8_t** out_start, uint32_t* out_give);
458 uint32_t
readSlow(uint8_t* buf, uint32_t len);
460 int writeSlow(
const uint8_t* buf, uint32_t len);
462 const uint8_t*
borrowSlow(uint8_t* buf, uint32_t* len);
479 #endif // #ifndef _SANDESH_TRANSPORT_TBUFFERTRANSPORTS_H_
void swap(TMemoryBuffer &that)
void getBuffer(uint8_t **bufPtr, uint32_t *sz)
void resetBuffer(uint32_t sz)
See constructor documentation.
void appendBufferToString(std::string &str)
uint8_t * wBound_
Writes may extend to just before here.
uint8_t * rBound_
Reads may extend to just before here.
uint8_t * getWritePtr(uint32_t len)
void initCommon(uint8_t *buf, uint32_t size, bool owner, uint32_t wPos)
TMemoryBuffer(uint32_t sz)
void wroteBytes(uint32_t len)
static const uint32_t defaultSize
uint32_t readAll(uint8_t *buf, uint32_t len)
int writeSlow(const uint8_t *buf, uint32_t len)
Slow path write.
void setWriteBuffer(uint8_t *buf, uint32_t len)
Convenience mutator for setting the write buffer.
int32_t readAll(uint8_t *buf, uint32_t len)
void setReadBuffer(uint8_t *buf, uint32_t len)
Convenience mutator for setting the read buffer.
std::string readAsString(uint32_t len)
uint32_t readSlow(uint8_t *buf, uint32_t len)
Slow path read.
uint32_t available_read() const
const uint8_t * borrowSlow(uint8_t *buf, uint32_t *len)
virtual uint32_t readSlow(uint8_t *buf, uint32_t len)=0
Slow path read.
const uint8_t * borrow(uint8_t *buf, uint32_t *len)
TMemoryBuffer(uint8_t *buf, uint32_t sz, MemoryPolicy policy=OBSERVE)
virtual int writeSlow(const uint8_t *buf, uint32_t len)=0
Slow path write.
int32_t read(uint8_t *buf, uint32_t len)
int ensureCanWrite(uint32_t len)
uint8_t * rBase_
Reads begin here.
uint32_t readAppendToString(std::string &str, uint32_t len)
void resetBuffer(uint8_t *buf, uint32_t sz, MemoryPolicy policy=OBSERVE)
See constructor documentation.
#define LOG(_Level, _Msg)
void consume(uint32_t len)
uint32_t available_write() const
void computeRead(uint32_t len, uint8_t **out_start, uint32_t *out_give)
int write(const uint8_t *buf, uint32_t len)
virtual const uint8_t * borrowSlow(uint8_t *buf, uint32_t *len)=0
int32_t readAll(Transport_ &trans, uint8_t *buf, uint32_t len)
std::string getBufferAsString()
uint8_t * wBase_
Writes begin here.