Skip to content

Commit

Permalink
Add fast branch that skips serial cache entirely
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Oct 30, 2022
1 parent 5aa0462 commit f895c6f
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions libgphoto2_port/serial/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ gp_port_serial_read (GPPort *dev, char *bytes, int size)
FD_ZERO (&readfs);
FD_SET (dev->pl->fd, &readfs);

while (readen < size) {
while (size > 0) {

/* Set timeout value within input loop */
timeout.tv_usec = (dev->timeout % 1000) * 1000;
Expand Down Expand Up @@ -564,25 +564,30 @@ gp_port_serial_read (GPPort *dev, char *bytes, int size)
/* Ok, we read 1 byte and it is 0xff */
/* FALLTHROUGH */
}
} else if (dev->pl->cachep == dev->pl->cachee && size >= sizeof(dev->pl->cache)) {
/* We're trying to read a chunk larger than the cache and the cache is empty.
In this case, skip the cache entirely and read as much as we can directly into the destination. */
now = read (dev->pl->fd, bytes, size);
if (now < 0)
return GP_ERROR_IO_READ;
} else {
if (dev->pl->cachep == dev->pl->cachee) {
/* Cache is empty, fill it up. */
/* We're reading only a few bytes and the cache is empty; fill it up. */
now = read (dev->pl->fd, dev->pl->cache, sizeof(dev->pl->cache));
if (now < 0) {
/* nothing more to read from the actual port */
if (now < 0)
return GP_ERROR_IO_READ;
}
/* Reset cache pointers */
dev->pl->cachep = dev->pl->cache;
dev->pl->cachee = dev->pl->cache + now;
}
/* read up to the required chunk size from cache */
now = MIN(size - readen, dev->pl->cachee - dev->pl->cachep);
now = MIN(size, dev->pl->cachee - dev->pl->cachep);
memcpy(bytes, dev->pl->cachep, now);
dev->pl->cachep += now;
}
bytes += now;
readen += now;
size -= readen;
}

return readen;
Expand Down

0 comments on commit f895c6f

Please sign in to comment.