From: Nathan Sidwell Date: Mon, 15 Mar 2004 12:31:41 +0000 (+0000) Subject: * objdump.c (struct SFILE): Replace current pointer with pos X-Git-Tag: gprof-pre-ansify-2004-05-26~371 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=964f808ee132850ad1b2ab3bdbd82c933affbb1e;p=pf3gnuchains%2Fpf3gnuchains4x.git * objdump.c (struct SFILE): Replace current pointer with pos offset, rename size to alloc. (objdump_sprintf): Avoid unnecessary copies in the common case (disassemble_bytes): Keep sfile live throughout the function. Adjust usage appropriately. --- diff --git a/binutils/ChangeLog b/binutils/ChangeLog index f54ca79671..469c5b6ecd 100644 --- a/binutils/ChangeLog +++ b/binutils/ChangeLog @@ -1,3 +1,11 @@ +2004-03-15 Nathan Sidwell + + * objdump.c (struct SFILE): Replace current pointer with pos + offset, rename size to alloc. + (objdump_sprintf): Avoid unnecessary copies in the common case + (disassemble_bytes): Keep sfile live throughout the + function. Adjust usage appropriately. + 2004-03-10 Ben Elliston * MAINTAINERS: Update my mail address. diff --git a/binutils/objdump.c b/binutils/objdump.c index f67aacb888..1e9d97058b 100644 --- a/binutils/objdump.c +++ b/binutils/objdump.c @@ -1142,8 +1142,8 @@ show_line (bfd *abfd, asection *section, bfd_vma addr_offset) typedef struct { char *buffer; - size_t size; - char *current; + size_t pos; + size_t alloc; } SFILE; /* sprintf to a "stream". */ @@ -1151,39 +1151,25 @@ typedef struct static int objdump_sprintf (SFILE *f, const char *format, ...) { - char *buf; size_t n; va_list args; - va_start (args, format); - - vasprintf (&buf, format, args); - - if (buf == NULL) + while (1) { + size_t space = f->alloc - f->pos; + + va_start (args, format); + n = vsnprintf (f->buffer + f->pos, space, format, args); va_end (args); - fatal (_("Out of virtual memory")); - } - - n = strlen (buf); - while ((size_t) ((f->buffer + f->size) - f->current) < n + 1) - { - size_t curroff; - - curroff = f->current - f->buffer; - f->size *= 2; - f->buffer = xrealloc (f->buffer, f->size); - f->current = f->buffer + curroff; + if (space > n) + break; + + f->alloc = (f->alloc + n) * 2; + f->buffer = xrealloc (f->buffer, f->alloc); } - - memcpy (f->current, buf, n); - f->current += n; - f->current[0] = '\0'; - - free (buf); - - va_end (args); + f->pos += n; + return n; } @@ -1243,10 +1229,15 @@ disassemble_bytes (struct disassemble_info * info, int skip_addr_chars; bfd_vma addr_offset; int opb = info->octets_per_byte; + SFILE sfile; aux = (struct objdump_disasm_info *) info->application_data; section = aux->sec; + sfile.alloc = 120; + sfile.buffer = xmalloc (sfile.alloc); + sfile.pos = 0; + if (insns) octets_per_line = 4; else @@ -1311,7 +1302,6 @@ disassemble_bytes (struct disassemble_info * info, else { char buf[50]; - SFILE sfile; int bpc = 0; int pb = 0; @@ -1344,9 +1334,7 @@ disassemble_bytes (struct disassemble_info * info, if (insns) { - sfile.size = 120; - sfile.buffer = xmalloc (sfile.size); - sfile.current = sfile.buffer; + sfile.pos = 0; info->fprintf_func = (fprintf_ftype) objdump_sprintf; info->stream = (FILE *) &sfile; info->bytes_per_line = 0; @@ -1371,9 +1359,8 @@ disassemble_bytes (struct disassemble_info * info, octets_per_line = info->bytes_per_line; if (octets < 0) { - if (sfile.current != sfile.buffer) + if (sfile.pos) printf ("%s\n", sfile.buffer); - free (sfile.buffer); break; } } @@ -1447,11 +1434,8 @@ disassemble_bytes (struct disassemble_info * info, if (! insns) printf ("%s", buf); - else - { - printf ("%s", sfile.buffer); - free (sfile.buffer); - } + else if (sfile.pos) + printf ("%s", sfile.buffer); if (prefix_addresses ? show_raw_insn > 0 @@ -1558,6 +1542,8 @@ disassemble_bytes (struct disassemble_info * info, addr_offset += octets / opb; } + + free (sfile.buffer); } static void