Unify and cleanup malloc chains in creator functions
Now that we settled on having a single create/destroy interface for each implementation of a building block, we can start addressing the issue of the numerous mallocs
that appear in the codebase.
In particular, we have create functions where this appears:
void *ptr = malloc(sizeof(struct aml_generic));
struct aml_generic *generic_p = ptr;
generic_p->data = malloc(sizeof(struct aml_implementation));
In the past, we solved this issue with the following pattern:
void *ptr = malloc(sizeof(struct aml_generic) + sizeof(struct aml_implementation));
struct aml_generic *generic_p = ptr;
generic_p->data = ptr + sizeof(aml_implementation);
The problem is that it doesn't work in all cases: there are no guaranties that the second pointer is properly aligned.
We would like to fix this problem, while at the same time refactoring the code so that it becomes easier in the future to replace our internal mallocs by a custom implementation (aml-optimized).
The current idea is to design a set of macros that can do:
#define ALIGN_MASK(size) ((size)-(size_t)1)
#define align(size, type) ((sizeof(type) + ALIGN_MASK(size)) & ~ALIGN_MASK(size))
#define aml_malloc(p, l, a, b) { \
assert(sizeof(a) % sizeof(void *) == 0); \
assert(sizeof(b) % sizeof(void *) == 0); \
p = malloc(align(sizeof(void *),a) + sizeof(b)); \
}
Or something close to that. Note that the use of a default alignment value is mostly to streamline the process, we don't really need it.