1、一个buffer缓存简单实现
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <malloc.h>
class Buffer
{
public:
Buffer()
{
m_buf = (char *)malloc(m_incr);
if (m_buf == nullptr)
{
return;
}
m_cap += m_incr;
m_begin = m_end = 0;
}
~Buffer()
{
free(m_buf);
m_buf = nullptr;
}
int GetBegin() { return m_begin; }
int GetEnd() { return m_end; }
int GetCap() { return m_cap; }
int GetLeft() { return (m_cap - m_end); }
int ContentSize() { return (m_end - m_begin); }
void Put(const char *c, int len);
char *Get(int len);
private:
char *m_buf{nullptr};
int m_begin{0}; //buffer 内容开始
int m_end{0}; //buffer 内容结束
int m_cap{0}; //buffer 总量
int m_incr{10};
};
void Buffer::Put(const char *c, int len)
{
if (c == nullptr || len <&