From 8c6ae0f8ac5b35f5237375dfe748300544711701 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 4 Feb 2011 19:22:43 +0100 Subject: [PATCH] vasnprintf: Reduce use of malloc for small format strings. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * lib/printf-args.h (N_DIRECT_ALLOC_ARGUMENTS): New macro. (arguments): Add room for the first 7 arguments. * lib/printf-parse.h (N_DIRECT_ALLOC_DIRECTIVES): New macro. (char_directives, u8_directives, u16_directives, u32_directives): Add room for the first 7 directives. * lib/printf-parse.c: Include . (PRINTF_PARSE): Change memory handling code so that it uses the first 7 preallocated elements in an 'arguments' or 'DIRECTIVES' struct. * lib/vasnprintf.c (VASNPRINTF): Update memory handling code. Reported by Pádraig Brady . --- ChangeLog | 14 ++++++++++++++ lib/printf-args.h | 6 +++++- lib/printf-parse.c | 39 ++++++++++++++++++++++----------------- lib/printf-parse.h | 9 ++++++++- lib/vasnprintf.c | 5 +++-- 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index b0254540a6..9d9038edef 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2011-02-04 Bruno Haible + + vasnprintf: Reduce use of malloc for small format strings. + * lib/printf-args.h (N_DIRECT_ALLOC_ARGUMENTS): New macro. + (arguments): Add room for the first 7 arguments. + * lib/printf-parse.h (N_DIRECT_ALLOC_DIRECTIVES): New macro. + (char_directives, u8_directives, u16_directives, u32_directives): Add + room for the first 7 directives. + * lib/printf-parse.c: Include . + (PRINTF_PARSE): Change memory handling code so that it uses the first + 7 preallocated elements in an 'arguments' or 'DIRECTIVES' struct. + * lib/vasnprintf.c (VASNPRINTF): Update memory handling code. + Reported by Pádraig Brady . + 2011-01-31 Eric Blake dup2: work around Haiku bug diff --git a/lib/printf-args.h b/lib/printf-args.h index cf9bb8173c..e03eb6e78a 100644 --- a/lib/printf-args.h +++ b/lib/printf-args.h @@ -1,5 +1,5 @@ /* Decomposed printf argument list. - Copyright (C) 1999, 2002-2003, 2006-2007, 2009-2011 Free Software + Copyright (C) 1999, 2002-2003, 2006-2007, 2011 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify @@ -136,10 +136,14 @@ typedef struct } argument; +/* Number of directly allocated arguments (no malloc() needed). */ +#define N_DIRECT_ALLOC_ARGUMENTS 7 + typedef struct { size_t count; argument *arg; + argument direct_alloc_arg[N_DIRECT_ALLOC_ARGUMENTS]; } arguments; diff --git a/lib/printf-parse.c b/lib/printf-parse.c index e44a8b2321..c4e1d37bf5 100644 --- a/lib/printf-parse.c +++ b/lib/printf-parse.c @@ -63,6 +63,9 @@ /* malloc(), realloc(), free(). */ #include +/* memcpy(). */ +#include + /* errno. */ #include @@ -80,23 +83,20 @@ STATIC int PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a) { - const CHAR_T *cp = format; /* pointer into format */ + const CHAR_T *cp = format; /* pointer into format */ size_t arg_posn = 0; /* number of regular arguments consumed */ - size_t d_allocated; /* allocated elements of d->dir */ - size_t a_allocated; /* allocated elements of a->arg */ + size_t d_allocated; /* allocated elements of d->dir */ + size_t a_allocated; /* allocated elements of a->arg */ size_t max_width_length = 0; size_t max_precision_length = 0; d->count = 0; - d_allocated = 1; - d->dir = (DIRECTIVE *) malloc (d_allocated * sizeof (DIRECTIVE)); - if (d->dir == NULL) - /* Out of memory. */ - goto out_of_memory_1; + d_allocated = N_DIRECT_ALLOC_DIRECTIVES; + d->dir = d->direct_alloc_dir; a->count = 0; - a_allocated = 0; - a->arg = NULL; + a_allocated = N_DIRECT_ALLOC_ARGUMENTS; + a->arg = a->direct_alloc_arg; #define REGISTER_ARG(_index_,_type_) \ { \ @@ -113,12 +113,14 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a) if (size_overflow_p (memory_size)) \ /* Overflow, would lead to out of memory. */ \ goto out_of_memory; \ - memory = (argument *) (a->arg \ + memory = (argument *) (a->arg != a->direct_alloc_arg \ ? realloc (a->arg, memory_size) \ : malloc (memory_size)); \ if (memory == NULL) \ /* Out of memory. */ \ goto out_of_memory; \ + if (a->arg == a->direct_alloc_arg) \ + memcpy (memory, a->arg, a->count * sizeof (argument)); \ a->arg = memory; \ } \ while (a->count <= n) \ @@ -588,10 +590,14 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a) if (size_overflow_p (memory_size)) /* Overflow, would lead to out of memory. */ goto out_of_memory; - memory = (DIRECTIVE *) realloc (d->dir, memory_size); + memory = (DIRECTIVE *) (d->dir != d->direct_alloc_dir + ? realloc (d->dir, memory_size) + : malloc (memory_size)); if (memory == NULL) /* Out of memory. */ goto out_of_memory; + if (d->dir == d->direct_alloc_dir) + memcpy (memory, d->dir, d->count * sizeof (DIRECTIVE)); d->dir = memory; } } @@ -610,19 +616,18 @@ PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a) return 0; error: - if (a->arg) + if (a->arg != a->direct_alloc_arg) free (a->arg); - if (d->dir) + if (d->dir != d->direct_alloc_dir) free (d->dir); errno = EINVAL; return -1; out_of_memory: - if (a->arg) + if (a->arg != a->direct_alloc_arg) free (a->arg); - if (d->dir) + if (d->dir != d->direct_alloc_dir) free (d->dir); -out_of_memory_1: errno = ENOMEM; return -1; } diff --git a/lib/printf-parse.h b/lib/printf-parse.h index bb16df2d08..c819f944ab 100644 --- a/lib/printf-parse.h +++ b/lib/printf-parse.h @@ -1,5 +1,5 @@ /* Parse printf format string. - Copyright (C) 1999, 2002-2003, 2005, 2007, 2009-2011 Free Software + Copyright (C) 1999, 2002-2003, 2005, 2007, 2010-2011 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify @@ -47,6 +47,9 @@ /* xxx_directive: A parsed directive. xxx_directives: A parsed format string. */ +/* Number of directly allocated directives (no malloc() needed). */ +#define N_DIRECT_ALLOC_DIRECTIVES 7 + /* A parsed directive. */ typedef struct { @@ -71,6 +74,7 @@ typedef struct char_directive *dir; size_t max_width_length; size_t max_precision_length; + char_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES]; } char_directives; @@ -100,6 +104,7 @@ typedef struct u8_directive *dir; size_t max_width_length; size_t max_precision_length; + u8_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES]; } u8_directives; @@ -127,6 +132,7 @@ typedef struct u16_directive *dir; size_t max_width_length; size_t max_precision_length; + u16_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES]; } u16_directives; @@ -154,6 +160,7 @@ typedef struct u32_directive *dir; size_t max_width_length; size_t max_precision_length; + u32_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES]; } u32_directives; diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index 13b60c44a0..8f07308781 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -1753,8 +1753,9 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, return NULL; #define CLEANUP() \ - free (d.dir); \ - if (a.arg) \ + if (d.dir != d.direct_alloc_dir) \ + free (d.dir); \ + if (a.arg != a.direct_alloc_arg) \ free (a.arg); if (PRINTF_FETCHARGS (args, &a) < 0) -- 2.30.2