X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fargp-parse.c;h=345bddabe08bf863eefc1a151843036e4ee2d78a;hb=8579e67153c2b71f4d6e36c274b279dd6429b37d;hp=8b89354d4ee7e913b0323ec64d3274a3f381db13;hpb=41b2b676f5a327f5c356f9997fecc295fb01c673;p=gnulib.git diff --git a/lib/argp-parse.c b/lib/argp-parse.c index 8b89354d4..345bddabe 100644 --- a/lib/argp-parse.c +++ b/lib/argp-parse.c @@ -15,13 +15,14 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, - Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifdef HAVE_CONFIG_H #include #endif #include +#include #include #include #include @@ -37,11 +38,14 @@ #else # include "gettext.h" #endif -#define N_(msgid) (msgid) +#define N_(msgid) msgid #include "argp.h" #include "argp-namefrob.h" +#define alignof(type) offsetof (struct { char c; type x; }, x) +#define alignto(n, d) ((((n) + (d) - 1) / (d)) * (d)) + /* Getopt return values. */ #define KEY_END (-1) /* The end of the options. */ #define KEY_ARG 1 /* A non-option argument. */ @@ -462,6 +466,11 @@ parser_init (struct parser *parser, const struct argp *argp, struct group *group; struct parser_sizes szs; struct _getopt_data opt_data = _GETOPT_DATA_INITIALIZER; + char *storage; + size_t glen, gsum; + size_t clen, csum; + size_t llen, lsum; + size_t slen, ssum; szs.short_len = (flags & ARGP_NO_ARGS) ? 0 : 1; szs.long_len = 0; @@ -472,22 +481,33 @@ parser_init (struct parser *parser, const struct argp *argp, calc_sizes (argp, &szs); /* Lengths of the various bits of storage used by PARSER. */ -#define GLEN (szs.num_groups + 1) * sizeof (struct group) -#define CLEN (szs.num_child_inputs * sizeof (void *)) -#define LLEN ((szs.long_len + 1) * sizeof (struct option)) -#define SLEN (szs.short_len + 1) - - parser->storage = malloc (GLEN + CLEN + LLEN + SLEN); + glen = (szs.num_groups + 1) * sizeof (struct group); + clen = szs.num_child_inputs * sizeof (void *); + llen = (szs.long_len + 1) * sizeof (struct option); + slen = szs.short_len + 1; + + /* Sums of previous lengths, properly aligned. There's no need to + align gsum, since struct group is aligned at least as strictly as + void * (since it contains a void * member). And there's no need + to align lsum, since struct option is aligned at least as + strictly as char. */ + gsum = glen; + csum = alignto (gsum + clen, alignof (struct option)); + lsum = csum + llen; + ssum = lsum + slen; + + parser->storage = malloc (ssum); if (! parser->storage) return ENOMEM; + storage = parser->storage; parser->groups = parser->storage; - parser->child_inputs = (void **)((char*) parser->storage + GLEN); - parser->long_opts = (struct option *)((char*) parser->storage + GLEN + CLEN); - parser->short_opts = (char*) parser->storage + GLEN + CLEN + LLEN; + parser->child_inputs = (void **) (storage + gsum); + parser->long_opts = (struct option *) (storage + csum); + parser->short_opts = storage + lsum; parser->opt_data = opt_data; - memset (parser->child_inputs, 0, szs.num_child_inputs * sizeof (void *)); + memset (parser->child_inputs, 0, clen); parser_convert (parser, argp, flags); memset (&parser->state, 0, sizeof (struct argp_state));