-
Notifications
You must be signed in to change notification settings - Fork 6
Add mem wrappers and die() to avoid repeated patterns #16
Copy link
Copy link
Open
Description
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
Bufferout 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
likeArenaChunkas 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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels