+2004-03-15 Nathan Sidwell <nathan@codesourcery.com>
+
+ * 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 <bje@gnu.org>
* MAINTAINERS: Update my mail address.
typedef struct
{
char *buffer;
- size_t size;
- char *current;
+ size_t pos;
+ size_t alloc;
} SFILE;
/* sprintf to a "stream". */
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;
}
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
else
{
char buf[50];
- SFILE sfile;
int bpc = 0;
int pb = 0;
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;
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;
}
}
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
addr_offset += octets / opb;
}
+
+ free (sfile.buffer);
}
static void