Skip to content

Add mem wrappers and die() to avoid repeated patterns #16

@pabloosabaterr

Description

@pabloosabaterr

Currently every call to memory function (malloc, calloc, realloc, ...) needs a manual NULL check and error handling at the caller.
This makes some repetitive patterns appear through the codebase adding noise, e.g.:
at src/core/llvm

ModuleCompileTask *tasks = malloc(sizeof(ModuleCompileTask) * module_count);
pthread_t *threads = malloc(sizeof(pthread_t) * module_count);

if (!tasks || !threads) {
    fprintf(stderr, "Failed to allocate memory for compilation tasks\n");
    free(tasks);
    free(threads);
    return false;
}

with wrappers this would become:

ModuleCompileTask *tasks = xmalloc(sizeof(ModuleCompileTask) * module_count);
pthread_t *threads = xmalloc(sizeof(pthread_t) * module_count);

This would follow Git patterns for mem alloc wrappers at:

https://github.com/git/git/blob/2855562/wrapper.h#L11

A PR that closes this issue would consist of:

  • Add die() a helper with a formatted msg to stderr + exit.
  • Add the mem wrappers and refactor a file at least
  • As part of a cleanup before the actual PR, moving Buffer out of the header and moving it to the .c
    file since it's only used by the arena allocator. I would suggest to change the name as well to something
    like ArenaChunk as it is too generic rn.

This PR wouldn't aim to refactor the whole codebase, but rather provide the tools to step by step
clean the current code in following commits that use allocation functions and to be used on future
commits.

Pablo

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions