Cleanup: Creator: Replace 'void' MEM_[cm]allocN with templated, type-safe MEM_[cm]allocN<T>.
The main issue of 'type-less' standard C allocations is that there is no check on allocated type possible. This is a serious source of annoyance (and crashes) when making some low-level structs non-trivial, as tracking down all usages of these structs in higher-level other structs and their allocation is... really painful. `MEM_[cm]allocN<T>` templates on the other hand do check that the given type is trivial, at build time (static assert), which makes such issue... trivial to catch. NOTE: New code should strive to use `MEM_new` (i.e. allocation and construction) as much as possible, even for trivial PoD types. Pull Request: https://projects.blender.org/blender/blender/pulls/135620
This commit is contained in:
committed by
Bastien Montagne
parent
6542c86ff5
commit
09221e4db2
@@ -326,7 +326,7 @@ static int *parse_int_relative_clamp_n(
|
||||
}
|
||||
}
|
||||
|
||||
int *values = MEM_mallocN(sizeof(*values) * len, __func__);
|
||||
int *values = MEM_malloc_arrayN<int>(size_t(len), __func__);
|
||||
int i = 0;
|
||||
while (true) {
|
||||
const char *str_end = strchr(str, sep);
|
||||
@@ -382,7 +382,7 @@ static int (*parse_int_range_relative_clamp_n(const char *str,
|
||||
}
|
||||
}
|
||||
|
||||
int(*values)[2] = static_cast<int(*)[2]>(MEM_mallocN(sizeof(*values) * len, __func__));
|
||||
int(*values)[2] = MEM_malloc_arrayN<int[2]>(size_t(len), __func__);
|
||||
int i = 0;
|
||||
while (true) {
|
||||
const char *str_end_range;
|
||||
@@ -439,7 +439,7 @@ fail:
|
||||
# ifdef WIN32
|
||||
static char **argv_duplicate(const char **argv, int argc)
|
||||
{
|
||||
char **argv_copy = static_cast<char **>(MEM_mallocN(sizeof(*argv_copy) * argc, __func__));
|
||||
char **argv_copy = MEM_malloc_arrayN<char *>(size_t(argc), __func__);
|
||||
for (int i = 0; i < argc; i++) {
|
||||
argv_copy[i] = BLI_strdup(argv[i]);
|
||||
}
|
||||
@@ -472,8 +472,7 @@ static bool main_arg_deferred_is_set()
|
||||
static void main_arg_deferred_setup(BA_ArgCallback func, int argc, const char **argv, void *data)
|
||||
{
|
||||
BLI_assert(app_state.main_arg_deferred == nullptr);
|
||||
BA_ArgCallback_Deferred *d = static_cast<BA_ArgCallback_Deferred *>(
|
||||
MEM_callocN(sizeof(*d), __func__));
|
||||
BA_ArgCallback_Deferred *d = MEM_callocN<BA_ArgCallback_Deferred>(__func__);
|
||||
d->func = func;
|
||||
d->argc = argc;
|
||||
d->argv = argv;
|
||||
|
||||
Reference in New Issue
Block a user