[PATCH 03/10] remote: Make readahead_cache a C++ class

Pedro Alves
2018-05-16T14:18:38Z

The idea here is eliminate the get_remote_state calls from within
readahead_cache_invalidate, readahead_cache_invalidate_fd,
remote_hostio_pread_from_cache by making those functions be class
methods instead. Later on we'll have one readahead_cache instance per
remote connection, and this change makes that easier.

gdb/ChangeLog:
2018-05-07 Pedro Alves <palves@redhat.com>

* remote.c (struct readahead_cache) <invalidate, invalidate_fd,
pread>: New method declarations.
(remote_target::open_1): Adjust.
(readahead_cache_invalidate): Rename to ...
(readahead_cache::invalidate): ... this, and adjust to be a class
method.
(readahead_cache_invalidate_fd): Rename to ...
(readahead_cache::invalidate_fd): ... this, and adjust to be a
class method.
(remote_hostio_pwrite): Adjust.
(remote_hostio_pread_from_cache): Rename to ...
(readahead_cache::pread): ... this, and adjust to be a class
method.
(remote_hostio_close): Adjust.
---
gdb/remote.c | 70 ++++++++++++++++++++++++++++++------------------------------
1 file changed, 35 insertions(+), 35 deletions(-)

diff --git a/gdb/remote.c b/gdb/remote.c
index bc4815c67e..a416d9285b 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -518,8 +518,6 @@ static void remote_btrace_maybe_reopen (void);

static int stop_reply_queue_length (void);

-static void readahead_cache_invalidate (void);
-
static void remote_unpush_and_throw (void);

static struct remote_state *get_remote_state (void);
@@ -575,6 +573,16 @@ typedef unsigned char threadref[OPAQUETHREADBYTES];

struct readahead_cache
{
+ /* Invalidate the readahead cache. */
+ void invalidate ();
+
+ /* Invalidate the readahead cache if it is holding data for FD. */
+ void invalidate_fd (int fd);
+
+ /* Serve pread from the readahead cache. Returns number of bytes
+ read, or 0 if the request can't be served from the cache. */
+ int pread (int fd, gdb_byte *read_buf, size_t len, ULONGEST offset);
+
/* The file descriptor for the file that is being cached. -1 if the
cache is invalid. */
int fd = -1;
@@ -5299,7 +5307,7 @@ remote_target::open_1 (const char *name, int from_tty, int extended_p)
rs->use_threadinfo_query = 1;
rs->use_threadextra_query = 1;

- readahead_cache_invalidate ();
+ rs->readahead_cache.invalidate ();

if (target_async_permitted)
{
@@ -11668,25 +11676,21 @@ remote_hostio_send_command (int command_bytes, int which_packet,
return ret;
}

-/* Invalidate the readahead cache. */
+/* See declaration.h. */

-static void
-readahead_cache_invalidate (void)
+void
+readahead_cache::invalidate ()
{
- struct remote_state *rs = get_remote_state ();
-
- rs->readahead_cache.fd = -1;
+ this->fd = -1;
}

-/* Invalidate the readahead cache if it is holding data for FD. */
+/* See declaration.h. */

-static void
-readahead_cache_invalidate_fd (int fd)
+void
+readahead_cache::invalidate_fd (int fd)
{
- struct remote_state *rs = get_remote_state ();
-
- if (rs->readahead_cache.fd == fd)
- rs->readahead_cache.fd = -1;
+ if (this->fd == fd)
+ this->fd = -1;
}

/* Set the filesystem remote_hostio functions that take FILENAME
@@ -11793,7 +11797,7 @@ remote_hostio_pwrite (struct target_ops *self,
int left = get_remote_packet_size ();
int out_len;

- readahead_cache_invalidate_fd (fd);
+ rs->readahead_cache.invalidate_fd (fd);

remote_buffer_add_string (&p, &left, "vFile:pwrite:");

@@ -11857,26 +11861,22 @@ remote_hostio_pread_vFile (struct target_ops *self,
return ret;
}

-/* Serve pread from the readahead cache. Returns number of bytes
- read, or 0 if the request can't be served from the cache. */
+/* See declaration.h. */

-static int
-remote_hostio_pread_from_cache (struct remote_state *rs,
- int fd, gdb_byte *read_buf, size_t len,
- ULONGEST offset)
+int
+readahead_cache::pread (int fd, gdb_byte *read_buf, size_t len,
+ ULONGEST offset)
{
- struct readahead_cache *cache = &rs->readahead_cache;
-
- if (cache->fd == fd
- && cache->offset <= offset
- && offset < cache->offset + cache->bufsize)
+ if (this->fd == fd
+ && this->offset <= offset
+ && offset < this->offset + this->bufsize)
{
- ULONGEST max = cache->offset + cache->bufsize;
+ ULONGEST max = this->offset + this->bufsize;

if (offset + len > max)
len = max - offset;

- memcpy (read_buf, cache->buf + offset - cache->offset, len);
+ memcpy (read_buf, this->buf + offset - this->offset, len);
return len;
}

@@ -11892,9 +11892,9 @@ remote_hostio_pread (struct target_ops *self,
{
int ret;
struct remote_state *rs = get_remote_state ();
- struct readahead_cache *cache = &rs->readahead_cache;
+ readahead_cache *cache = &rs->readahead_cache;

- ret = remote_hostio_pread_from_cache (rs, fd, read_buf, len, offset);
+ ret = cache->pread (fd, read_buf, len, offset);
if (ret > 0)
{
cache->hit_count++;
@@ -11919,12 +11919,12 @@ remote_hostio_pread (struct target_ops *self,
cache->offset, remote_errno);
if (ret <= 0)
{
- readahead_cache_invalidate_fd (fd);
+ cache->invalidate_fd (fd);
return ret;
}

cache->bufsize = ret;
- return remote_hostio_pread_from_cache (rs, fd, read_buf, len, offset);
+ return cache->pread (fd, read_buf, len, offset);
}

int
@@ -11943,7 +11943,7 @@ remote_hostio_close (struct target_ops *self, int fd, int *remote_errno)
char *p = rs->buf;
int left = get_remote_packet_size () - 1;

- readahead_cache_invalidate_fd (fd);
+ rs->readahead_cache.invalidate_fd (fd);

remote_buffer_add_string (&p, &left, "vFile:close:");

--
2.14.3