From: Paul Eggert Date: Fri, 24 Dec 2004 08:02:03 +0000 (+0000) Subject: Include . X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d140c4471e983a16c5836de180ddf11a12eaa39;p=pspp 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 index 8b89354d4e..509f6dea85 100644 --- 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 @@ 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,27 @@ 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); + 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 (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));