]> mj.ucw.cz Git - libucw.git/commitdiff
When allocating dynamic arrays, respect alignment. I hope this is the
authorMartin Mares <mj@ucw.cz>
Sun, 30 Apr 2006 17:47:37 +0000 (19:47 +0200)
committerMartin Mares <mj@ucw.cz>
Sun, 30 Apr 2006 17:47:37 +0000 (19:47 +0200)
final revision :-)

Also, restrictions on dary element size shouldn't be necessary any longer.

lib/conf-intr.c
lib/conf.h

index 7b1564e2f185fbfa0c47739fddbe96b94dc9c576..cd015bd2282f2be8f4f386ebb6dc5f2e2d02fe4d 100644 (file)
@@ -101,6 +101,8 @@ cf_parse_ary(uns number, byte **pars, void *ptr, enum cf_type type, union cf_uni
 byte *cf_op_names[] = { CF_OPERATIONS };
 #undef T
 
+#define DARY_HDR_SIZE ALIGN(sizeof(uns), CPU_STRUCT_ALIGN)
+
 static byte *
 interpret_set_dynamic(struct cf_item *item, int number, byte **pars, void **ptr)
 {
@@ -108,8 +110,7 @@ interpret_set_dynamic(struct cf_item *item, int number, byte **pars, void **ptr)
   cf_journal_block(ptr, sizeof(void*));
   // boundary checks done by the caller
   uns size = cf_type_size(item->type, item->u.utype);
-  ASSERT(size >= sizeof(uns));
-  *ptr = cf_malloc(sizeof(uns) + number * size) + sizeof(uns);
+  *ptr = cf_malloc(DARY_HDR_SIZE + number * size) + DARY_HDR_SIZE;
   DARY_LEN(*ptr) = number;
   return cf_parse_ary(number, pars, *ptr, type, &item->u);
 }
@@ -125,7 +126,7 @@ interpret_add_dynamic(struct cf_item *item, int number, byte **pars, int *proces
   int taken = MIN(number, ABS(item->number)-old_nr);
   *processed = taken;
   // stretch the dynamic array
-  void *new_p = cf_malloc(sizeof(uns) + (old_nr + taken) * size) + sizeof(uns);
+  void *new_p = cf_malloc(DARY_HDR_SIZE + (old_nr + taken) * size) + DARY_HDR_SIZE;
   DARY_LEN(new_p) = old_nr + taken;
   cf_journal_block(ptr, sizeof(void*));
   *ptr = new_p;
index 82b46dd03dc1d6d2e2f180c0d9cdd556f91ca73e..214d9da9be2235f82e2ed95fda4f34e977cb3646 100644 (file)
@@ -124,7 +124,6 @@ struct cf_section {
 #define CF_USER(n,p,t)         { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = 1, .ptr = p, .u.utype = t }
 #define CF_USER_ARY(n,p,t,c)   { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
 #define CF_USER_DYN(n,p,t,c)   { .cls = CC_DYNAMIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
-  // Beware that CF_USER_DYN can only be used on user-defined types of size at least 4
 
 /* If you aren't picky about the number of parameters */
 #define CF_ANY_NUM             -0x7fffffff