changeset 5554:8ad9fa45dcee

Include <stddef.h>. (alignof, alignto): New macros. (parser_init): Don't assume that void * is aligned sufficiently for struct option.
author Paul Eggert <eggert@cs.ucla.edu>
date Fri, 24 Dec 2004 08:02:03 +0000
parents e9986aaeca84
children 12b1a84e1247
files lib/argp-parse.c
diffstat 1 files changed, 23 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/lib/argp-parse.c
+++ b/lib/argp-parse.c
@@ -22,6 +22,7 @@
 #endif
 
 #include <alloca.h>
+#include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -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));