c++ 常用buffer实现

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 <&
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值