inline void expand(int need) {
if (_pstart == NULL) {
int len = 256;
while (len 1;
_pfree = _pdata = _pstart = (unsigned char*)malloc(len);
_pend = _pstart + len;
} else if (_pend - _pfree // 空间不够
int flen = static_cast((_pend - _pfree) + (_pdata - _pstart));
int dlen = static_cast(_pfree - _pdata);
if (flen 4 < dlen) {
int bufsize = static_cast((_pend - _pstart) * 2);
while (bufsize - dlen < need)
bufsize <<= 1;
unsigned char *newbuf = (unsigned char *)malloc(bufsize);
if (newbuf == NULL)
{
TBSYS_LOG(ERROR, "expand data buffer failed, length: %d", bufsize);
}
assert(newbuf != NULL);
if (dlen > 0) {
memcpy(newbuf, _pdata, dlen);
}
free(_pstart);
_pdata = _pstart = newbuf;
_pfree = _pstart + dlen;
_pend = _pstart + bufsize;
} else {
memmove(_pstart, _pdata, dlen);
_pfree = _pstart + dlen;
_pdata = _pstart;
}
}
}