Simplify allocation APIs
If we consider simple objects as arrays of size 1, we can considerably
simplify these APIs, merging the *ARRAY and the non-array variants.
That will produce more readable code, since lines will be shorter (by
not having ARRAY in the macro names, as all macros will consistently
handle arrays), and the allocated size will be also more explicit.
The syntax will now be of the form:
p = MALLOC(42, foo_t); // allocate 42 elements of type foo_t.
p = MALLOC(1, bar_t); // allocate 1 element of type foo_t.
The _array() allocation functions should _never_ be called directly, and
instead these macros should be used.
The non-array functions (e.g., malloc(3)) still have their place, but
are limited to allocating structures with flexible array members. For
any other uses, the macros should be used.
Thus, we don't use any array or ARRAY variants in any code any more, and
they are only used as implementation details of these macros.
Link: <https://software.codidact.com/posts/285898/288023#answer-288023>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
committed by
Serge Hallyn
parent
065a752b42
commit
09775d3718
+9
-9
@@ -359,7 +359,7 @@ static void get_defaults (void)
|
||||
int wlen;
|
||||
|
||||
len = strlen(prefix) + strlen(USER_DEFAULTS_FILE) + 2;
|
||||
default_file = MALLOCARRAY(len, char);
|
||||
default_file = MALLOC(len, char);
|
||||
if (default_file == NULL)
|
||||
return;
|
||||
wlen = snprintf(default_file, len, "%s/%s", prefix, USER_DEFAULTS_FILE);
|
||||
@@ -472,7 +472,7 @@ static void get_defaults (void)
|
||||
char* _def_template; /* avoid const warning */
|
||||
|
||||
len = strlen(prefix) + strlen(cp) + 2;
|
||||
_def_template = XMALLOCARRAY(len, char);
|
||||
_def_template = XMALLOC(len, char);
|
||||
wlen = snprintf(_def_template, len, "%s/%s", prefix, cp);
|
||||
assert (wlen == (int) len -1);
|
||||
def_template = _def_template;
|
||||
@@ -496,7 +496,7 @@ static void get_defaults (void)
|
||||
char* _def_usrtemplate; /* avoid const warning */
|
||||
|
||||
len = strlen(prefix) + strlen(cp) + 2;
|
||||
_def_usrtemplate = XMALLOCARRAY(len, char);
|
||||
_def_usrtemplate = XMALLOC(len, char);
|
||||
wlen = snprintf(_def_usrtemplate, len, "%s/%s", prefix, cp);
|
||||
assert (wlen == (int) len -1);
|
||||
def_usrtemplate = _def_usrtemplate;
|
||||
@@ -586,7 +586,7 @@ static int set_defaults (void)
|
||||
|
||||
|
||||
len = strlen(prefix) + strlen(NEW_USER_FILE) + 2;
|
||||
new_file = MALLOCARRAY(len, char);
|
||||
new_file = MALLOC(len, char);
|
||||
if (new_file == NULL) {
|
||||
fprintf (stderr,
|
||||
_("%s: cannot create new defaults file: %s\n"),
|
||||
@@ -598,7 +598,7 @@ static int set_defaults (void)
|
||||
|
||||
if (prefix[0]) {
|
||||
len = strlen(prefix) + strlen(USER_DEFAULTS_FILE) + 2;
|
||||
default_file = MALLOCARRAY(len, char);
|
||||
default_file = MALLOC(len, char);
|
||||
if (default_file == NULL) {
|
||||
fprintf (stderr,
|
||||
_("%s: cannot create new defaults file: %s\n"),
|
||||
@@ -1627,7 +1627,7 @@ static void process_flags (int argc, char **argv)
|
||||
size_t len = strlen (def_home) + strlen (user_name) + 2;
|
||||
int wlen;
|
||||
|
||||
uh = XMALLOCARRAY (len, char);
|
||||
uh = XMALLOC(len, char);
|
||||
wlen = snprintf (uh, len, "%s/%s", def_home, user_name);
|
||||
assert (wlen == (int) len -1);
|
||||
|
||||
@@ -1637,7 +1637,7 @@ static void process_flags (int argc, char **argv)
|
||||
size_t len = strlen(prefix) + strlen(user_home) + 2;
|
||||
int wlen;
|
||||
char* _prefix_user_home; /* to avoid const warning */
|
||||
_prefix_user_home = XMALLOCARRAY(len, char);
|
||||
_prefix_user_home = XMALLOC(len, char);
|
||||
wlen = snprintf(_prefix_user_home, len, "%s/%s", prefix, user_home);
|
||||
assert (wlen == (int) len -1);
|
||||
prefix_user_home = _prefix_user_home;
|
||||
@@ -2451,7 +2451,7 @@ static void create_mail (void)
|
||||
return;
|
||||
}
|
||||
size = strlen(prefix) + strlen(spool) + strlen(user_name) + 3;
|
||||
file = XMALLOCARRAY(size, char);
|
||||
file = XMALLOC(size, char);
|
||||
if (prefix[0])
|
||||
sprintf (file, "%s/%s/%s", prefix, spool, user_name);
|
||||
else
|
||||
@@ -2563,7 +2563,7 @@ int main (int argc, char **argv)
|
||||
#endif
|
||||
|
||||
sys_ngroups = sysconf (_SC_NGROUPS_MAX);
|
||||
user_groups = XMALLOCARRAY (1 + sys_ngroups, char *);
|
||||
user_groups = XMALLOC(1 + sys_ngroups, char *);
|
||||
/*
|
||||
* Initialize the list to be empty
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user