From: Martin Mares Date: Sun, 30 Apr 2006 17:47:37 +0000 (+0200) Subject: When allocating dynamic arrays, respect alignment. I hope this is the X-Git-Tag: holmes-import~645^2~1 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=ce0e823dc4cdb520f63eca96a122896917a49e56;p=libucw.git When allocating dynamic arrays, respect alignment. I hope this is the final revision :-) Also, restrictions on dary element size shouldn't be necessary any longer. --- diff --git a/lib/conf-intr.c b/lib/conf-intr.c index 7b1564e2..cd015bd2 100644 --- a/lib/conf-intr.c +++ b/lib/conf-intr.c @@ -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; diff --git a/lib/conf.h b/lib/conf.h index 82b46dd0..214d9da9 100644 --- a/lib/conf.h +++ b/lib/conf.h @@ -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