1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 6 class cyclebuffer 7 { 8 protected: 9 volatile int32_t m_nReadIndex; 10 volatile int32_t m_nWriteIndex; 11 volatile int32_t m_nDataSize; //有效数据大小 12 volatile int32_t m_nBufSize; //总buf大小 13 uint_8 *m_pBuf; 14 15 public: 16 cyclebuffer(int nBufSize) 17 { 18 Reset(); 19 m_pBuf = (uint8_t *)malloc(nBufSize); 20 m_nbufSize = nBufSize; 21 } 22 23 virtual ~cyclebuffer() 24 { 25 if(m_pBuf != NULL) 26 { 27 free(m_pBuf); 28 m_pBuf = NULL; 29 } 30 } 31 32 //剩余空间数 33 int32_t Space() 34 { 35 return m_nBufSize-m_nDataSize; 36 } 37 38 //获取buf的总大小 39 int32_t Capatity() 40 { 41 return m_nBufSize; 42 } 43 44 //获取有效数据长度 45 int32_t Size() 46 { 47 return m_nDataSize; 48 } 49 50 //清空 51 void Reset() 52 { 53 m_nReadIndex = 0; 54 m_nwriteIndex = 0; 55 m_nDataSize = 0; 56 } 57 58 //增加nSize个字节 59 int32_t Grow(int32_t nSize) 60 { 61 if(nSize<=0) 62 { 63 return 0; 64 } 65 66 uint8_t *pMem = (uint8_t *)malloc(nSize + m_nBufSize); 67 if(pMem == NULL) 68 { 69 return 0; 70 } 71 72 int32_t nWriteIndex = 0; 73 //将旧内存中的数据拷贝到(增大的)新内存中 74 do 75 { 76 int32_t nReadBytes = Read(pMem + nWriteIndex, 4096); 77 if(nReadBytes <= 0) 78 { 79 break; 80 } 81 nWriteIndex += nReadBytes; 82 }while(m_nDataSize > 0); 83 84 if(m_nDataSize > 0) 85 { 86 return -1; 87 } 88 89 //释放旧内存块 90 if(m_pBuf != NULL) 91 { 92 free(m_pBuf); 93 } 94 95 m_pBuf = pMem; 96 m_nReadIndex = 0; 97 m_nWriteIndex = nWriteIndex; 98 m_nDataSize = nWriteIndex; 99 m_nBufSize += nSize; 100 101 return nSize; 102 } 103 104 //写到写列表的头部---针对新数据 105 int32_t WriteToHead(const uint8_t *pBuf, int32_t nDataSize) 106 { 107 if(nDataSize < 0 || pBuf == NULL) 108 { 109 return 0; 110 } 111 112 //空间不够 113 if(m_nDataSize + nDataSize > m_nBufsize) 114 { 115 if(Grow((m_nDataSize+nDataSize) - m_nDataSize) <= 0) 116 { 117 return 0; 118 } 119 } 120 121 if(m_nWriteIndex < m_nReadIndex) 122 { 123 memcpy(&m_pBuf[m_nReadIndex-nDataSize], pBuf, nDataSize); 124 m_nReadIndex -= nDataSize; 125 } 126 else if(m_nWriteIndex == m_nReadIndex) 127 { 128 return Write(pBuf, nDataSize); 129 } 130 else 131 { 132 int32_t nLeftDataSize = (nDataSize-m_nReadIndex<0)?nDataSize:m_nReadIndex; 133 if(nLeftDataSize < nDataSize) //此时nLeftDataSize == m_nReadIndex,m_nReadIndex前面的空间不够 134 { 135 int32_t nTailDataSize = nDataSize - nLeftDataSize; 136 int32_t nWriteIndex = m_nBufSize - nTailDatasize; 137 memcpy(&m_pBuf[nWriteIndex], pBuf, nDataSize-nLeftDataSize); //从内存最后向前写 138 memcpy(&m_pBuf[0], pBuf+nTailDataSize, nLeftDataSize); 139 m_nReadIndex = nWriteIndex; 140 } 141 else 142 { 143 if(m_nReadIndex-nDataSize < 0) 144 { 145 return 0; 146 } 147 memcpy(&m_pBuf[m_nReadIndex-nDataSize], pBuf, nDataSize); 148 m_nReadIndex -= nDataSize; 149 } 150 } 151 152 m_nDataSize += nDataSize; 153 return nDataSize; 154 } 155 156 //写到列表的尾部 157 int32_t Write(const uint8_t *pBuf, int32_t nDataSize) 158 { 159 if(nDataSize <= 0 || NULL == pBuf) 160 { 161 return 0; 162 } 163 164 if(nDataSize + m_nDataSize > m_nBufSize) 165 { 166 if(Grow((nDataSize+nBufSize)-m_nDataSize) <= 0) 167 { 168 return 0; 169 } 170 } 171 172 if(m_nWriteIndex < m_nReadIndex) 173 { 174 memcpy(&m_pBuf[m_nWriteIndex], pBuf, nDataSize); 175 m_nWriteIndex += nDataSize; 176 } 177 else 178 { 179 int32_t nLeftDataSize = m_nBufSize - m_nWriteIndex; 180 if(nLeftDataSize < nDataSize) 181 { 182 memcpy(&m_pBuf[m_nWriteIndex], pBuf, nLeftDataSize); 183 memcpy(&m_pBuf[0], pBuf+nLeftDataSize, nDataSize-nLeftDataSize); 184 m_nwriteIndex = nDataSize - nLeftDataSize; 185 } 186 else 187 { 188 memcpy(&m_pBuf[m_nWriteIndex], pBuf, nDataSize); 189 m_nWriteIndex += nDataSize; 190 } 191 } 192 m_nDataSize += nDataSize; 193 return nDataSize; 194 } 195 196 //读取 读列表的头部内存 197 int32_t Read(uint8_t *pBuf, const int32_t nWantSize) 198 { 199 if(nWantSize <= 0 || NULL == pBuf) 200 { 201 return 0; 202 } 203 204 int32_t nDataSize = ((m_nDataSize < nWantSize)?m_nDataSize : nWantSize); 205 if(nDataSize<=0) 206 { 207 return 0; 208 } 209 210 if(m_nReadIndex < m_nWriteIndex) 211 { 212 memcpy(pBuf, &m_pBuf[m_nReadIndex], nDataSize); 213 m_nReadIndex += nDataSize; 214 } 215 else 216 { 217 int32_t nLeftDataSize = m_nBufSize - m_nReadIndex; 218 if(nLeftDataSize < nDataSize) 219 { 220 memcpy(pBuf, &m_pBuf[m_nReadIndex], nLeftDataSize); 221 memcpy(pBuf+nLeftDataSize, &m_pBuf[0], nDataSize-nLeftDataSize); 222 m_nReadIndex = nDataSize-nLeftDataSize; 223 } 224 else 225 { 226 memcpy(pBuf, &m_pBuf[m_nReadIndex], nDataSize); 227 m_nReadIndex += nDataSize; 228 } 229 } 230 m_nDataSize -= nDataSize; 231 return nDataSize; 232 } 233 234 235 //读取数据但是不修改读索引 236 int32_t PeekRead(uint8_t *pBuf, const int32_t nWantSize) 237 { 238 if(nWantSize <= 0 || pBuf == NULL) 239 { 240 return 0; 241 } 242 243 int32_t nDataSize = ((m_nDataSize < nWantSize) ? m_nDataSize : nWantSize); 244 if(m_nReadIndex < m_nWriteIndex) 245 { 246 memcpy(pBuf, &m_pBuf[m_nReadIndex], nDataSize); 247 } 248 else if(m_nReadIndex == m_nWriteIndex) 249 { 250 return 0; 251 } 252 else 253 { 254 int32_t nLeftDataSize = m_nBufSize - m_nReadIndex; 255 if(nLeftDataSize < nDataSize) 256 { 257 memcpy(pBuf, &m_pBuf[m_nReadIndex], nLeftDataSize); 258 memcpy(pBuf+nLeftDataSize, &m_pBuf[0], nDataSize-nLeftDataSize); 259 } 260 else 261 { 262 memcpy(pBuf, &m_pBuf[m_nReadIndex], nDataSize); 263 } 264 } 265 return nDataSize; 266 } 267 268 };