Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
  • A aml
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 33
    • Issues 33
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 7
    • Merge requests 7
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • argo
  • aml
  • Issues
  • #45
Closed
Open
Issue created Jul 30, 2019 by Swann Perarnau@perarnauOwner

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.

Edited Jul 30, 2019 by Swann Perarnau
Assignee
Assign to
Time tracking