# HG changeset patch # User Paul Eggert # Date 1103875323 0 # Node ID 8ad9fa45dcee7037db56e5f94f921237a8f4490a # Parent e9986aaeca846a5b49ea88748dc9c242ff9bffab Include . (alignof, alignto): New macros. (parser_init): Don't assume that void * is aligned sufficiently for struct option. diff --git a/lib/argp-parse.c b/lib/argp-parse.c --- a/lib/argp-parse.c +++ b/lib/argp-parse.c @@ -22,6 +22,7 @@ #endif #include +#include #include #include #include @@ -42,6 +43,9 @@ #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 @@ 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,27 @@ 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) + glen = (szs.num_groups + 1) * sizeof (struct group); + gsum = alignto (glen, alignof (void *)); + clen = szs.num_child_inputs * sizeof (void *); + csum = alignto (gsum + clen, alignof (struct option)); + llen = (szs.long_len + 1) * sizeof (struct option); + lsum = alignto (csum + llen, alignof (char)); + slen = szs.short_len + 1; + ssum = lsum + slen; - parser->storage = malloc (GLEN + CLEN + LLEN + 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));