diff options
author | Pedro Alves <palves@redhat.com> | 2017-10-19 15:25:59 +0100 |
---|---|---|
committer | Pedro Alves <palves@redhat.com> | 2017-10-19 15:25:59 +0100 |
commit | 2edf834e298b15c882678db22e86745f701807fa (patch) | |
tree | 313e311b94f78d0c0044cc682bf4eab2b45ed169 | |
parent | 280958942b74f3164708700f195188c4d3446f58 (diff) | |
download | gdb-2edf834e298b15c882678db22e86745f701807fa.zip gdb-2edf834e298b15c882678db22e86745f701807fa.tar.gz gdb-2edf834e298b15c882678db22e86745f701807fa.tar.bz2 |
xml_fetch_content_from_file: Read in whole file in one go
There doesn't seem to be a good reason we're reading the file one
chunk at a time.
gdb/ChangeLog:
2017-10-19 Pedro Alves <palves@redhat.com>
* xml-support.c (xml_fetch_content_from_file): Don't read in
chunks. Instead use fseek to determine the file's size, and read
it in one go.
-rw-r--r-- | gdb/ChangeLog | 6 | ||||
-rw-r--r-- | gdb/xml-support.c | 36 |
2 files changed, 19 insertions, 23 deletions
diff --git a/gdb/ChangeLog b/gdb/ChangeLog index bbb0aab..9e7c342 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,9 @@ +2017-10-19 Pedro Alves <palves@redhat.com> + + * xml-support.c (xml_fetch_content_from_file): Don't read in + chunks. Instead use fseek to determine the file's size, and read + it in one go. + 2017-11-18 Keith Seitz <keiths@redhat.com> * c-exp.y (oper): Canonicalize conversion operators of user-defined diff --git a/gdb/xml-support.c b/gdb/xml-support.c index 42a4c91..1f53d7a 100644 --- a/gdb/xml-support.c +++ b/gdb/xml-support.c @@ -998,7 +998,6 @@ xml_fetch_content_from_file (const char *filename, void *baton) { const char *dirname = (const char *) baton; gdb_file_up file; - size_t len, offset; if (dirname && *dirname) { @@ -1015,34 +1014,25 @@ xml_fetch_content_from_file (const char *filename, void *baton) if (file == NULL) return NULL; - /* Read in the whole file, one chunk at a time. */ - len = 4096; - offset = 0; - gdb::unique_xmalloc_ptr<char> text ((char *) xmalloc (len)); - while (1) - { - size_t bytes_read; + /* Read in the whole file. */ - /* Continue reading where the last read left off. Leave at least - one byte so that we can NUL-terminate the result. */ - bytes_read = fread (text.get () + offset, 1, len - offset - 1, - file.get ()); - if (ferror (file.get ())) - { - warning (_("Read error from \"%s\""), filename); - return NULL; - } + size_t len; - offset += bytes_read; + if (fseek (file.get (), 0, SEEK_END) == -1) + perror_with_name (_("seek to end of file")); + len = ftell (file.get ()); + rewind (file.get ()); - if (feof (file.get ())) - break; + gdb::unique_xmalloc_ptr<char> text ((char *) xmalloc (len + 1)); - len = len * 2; - text.reset ((char *) xrealloc (text.release (), len)); + fread (text.get (), 1, len, file.get ()); + if (ferror (file.get ())) + { + warning (_("Read error from \"%s\""), filename); + return NULL; } - text.get ()[offset] = '\0'; + text.get ()[len] = '\0'; return text; } |