We can use >= instead of > to avoid an unneeded reallocation. Reduce duplicated code.

This commit is contained in:
Isaac Connor 2021-03-25 12:52:41 -04:00
parent 3f8b9db3d9
commit 292bad8718
1 changed files with 6 additions and 12 deletions

View File

@ -39,8 +39,11 @@ unsigned int Buffer::expand(unsigned int count) {
int headSpace = mHead - mStorage;
int tailSpace = spare - headSpace;
int width = mTail - mHead;
if ( spare > static_cast<int>(count) ) {
if ( spare >= static_cast<int>(count) ) {
// There is enough space in the allocation might need to shift everything over though
//
if ( tailSpace < static_cast<int>(count) ) {
// if there is extra space at the head, shift everything over
memmove(mStorage, mHead, mSize);
mHead = mStorage;
mTail = mHead + width;
@ -71,25 +74,16 @@ int Buffer::read_into(int sd, unsigned int bytes) {
}
int Buffer::read_into(int sd, unsigned int bytes, struct timeval timeout) {
// Make sure there is enough space
this->expand(bytes);
fd_set set;
FD_ZERO(&set); /* clear the set */
FD_SET(sd, &set); /* add our file descriptor to the set */
int rv = select(sd + 1, &set, NULL, NULL, &timeout);
if (rv == -1) {
Error("Error %d %s from select", errno, strerror(errno));
return rv;
} else if (rv == 0) {
printf("timeout"); /* a timeout occured */
Debug(1, "timeout"); /* a timeout occured */
return 0;
}
int bytes_read = read(sd, mTail, bytes);
if ( bytes_read > 0 ) {
mTail += bytes_read;
mSize += bytes_read;
}
return bytes_read;
return read_into(sd, bytes);
}