// boundary checks done by the caller
uns size = cf_type_size(item->type, item->u.utype);
ASSERT(size >= sizeof(uns));
- *ptr = cf_malloc((number+1) * size) + size;
- * (uns*) (*ptr - size) = number;
+ *ptr = cf_malloc(sizeof(uns) + number * size) + sizeof(uns);
+ DARY_LEN(*ptr) = number;
return cf_parse_ary(number, pars, *ptr, type, &item->u);
}
void *old_p = *ptr;
uns size = cf_type_size(item->type, item->u.utype);
ASSERT(size >= sizeof(uns));
- int old_nr = old_p ? * (int*) (old_p - size) : 0;
+ int old_nr = old_p ? DARY_LEN(old_p) : 0;
int taken = MIN(number, ABS(item->number)-old_nr);
*processed = taken;
// stretch the dynamic array
- void *new_p = cf_malloc((old_nr + taken + 1) * size) + size;
- * (uns*) (new_p - size) = old_nr + taken;
+ void *new_p = cf_malloc(sizeof(uns) + (old_nr + taken) * size) + sizeof(uns);
+ DARY_LEN(new_p) = old_nr + taken;
cf_journal_block(ptr, sizeof(void*));
*ptr = new_p;
if (op == OP_APPEND) {
}
}
-byte *
-cf_interpret_clear(struct cf_item *item, void *ptr)
+static byte *
+interpret_clear(struct cf_item *item, void *ptr)
{
if (item->cls == CC_LIST) {
cf_journal_block(ptr, sizeof(clist));
clist_init(ptr);
} else if (item->cls == CC_DYNAMIC) {
cf_journal_block(ptr, sizeof(void *));
- uns size = cf_type_size(item->type, item->u.utype);
- static u64 zero = 0;
- if (size <= sizeof(zero))
- *(void**)ptr = (&zero) + 1;
- else
- *(void**)ptr = cf_malloc_zero(size) + size;
+ static uns zero = 0;
+ * (void**) ptr = (&zero) + 1;
} else if (item->cls == CC_STATIC && item->type == CT_STRING) {
cf_journal_block(ptr, item->number * sizeof(byte*));
bzero(ptr, item->number * sizeof(byte*));
int taken; // process as many parameters as possible
if (op == OP_CLEAR)
- taken = 0, msg = cf_interpret_clear(item, ptr);
+ taken = 0, msg = interpret_clear(item, ptr);
else if (op == OP_SET)
msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
else if (item->cls == CC_DYNAMIC)
break;
case OP_CLEAR:
taken = 0;
- msg = cf_interpret_clear(item, item->ptr);
+ msg = interpret_clear(item, item->ptr);
break;
case OP_APPEND:
case OP_PREPEND:
/* If you aren't picky about the number of parameters */
#define CF_ANY_NUM -0x7fffffff
-#define DARY_LEN(a) *(uns*)(a-1)
+#define DARY_LEN(a) ((uns*)a)[-1]
// length of a dynamic array
-#define DARY_ALLOC(type,len,val...) (type[]) { (type)len, ##val } + 1
- // creates a static instance of a dynamic array, works only for integer and pointer types
+#define DARY_ALLOC(type,len,val...) ((struct { uns l; type a[len]; }) { .l = len, .a = { val } }).a
+ // creates a static instance of a dynamic array
/* Memory allocation: conf-alloc.c */
struct mempool;