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:
Alejandro Colomar
2023-04-05 21:17:38 +02:00
committed by Serge Hallyn
parent 065a752b42
commit 09775d3718
40 changed files with 115 additions and 120 deletions
+8 -13
View File
@@ -20,17 +20,12 @@
#include "defines.h"
#define CALLOC(n, type) ((type *) calloc(n, sizeof(type)))
#define XCALLOC(n, type) ((type *) xcalloc(n, sizeof(type)))
#define MALLOCARRAY(n, type) ((type *) mallocarray(n, sizeof(type)))
#define XMALLOCARRAY(n, type) ((type *) xmallocarray(n, sizeof(type)))
#define CALLOC(n, type) ((type *) calloc(n, sizeof(type)))
#define XCALLOC(n, type) ((type *) xcalloc(n, sizeof(type)))
#define MALLOC(n, type) ((type *) mallocarray(n, sizeof(type)))
#define XMALLOC(n, type) ((type *) xmallocarray(n, sizeof(type)))
#define MALLOC(type) MALLOCARRAY(1, type)
#define XMALLOC(type) XMALLOCARRAY(1, type)
#define REALLOC(ptr, type) REALLOCARRAY(ptr, 1, type)
#define REALLOCF(ptr, type) REALLOCARRAYF(ptr, 1, type)
#define REALLOCARRAY(ptr, n, type) \
#define REALLOC(ptr, n, type) \
({ \
__auto_type p_ = (ptr); \
\
@@ -39,7 +34,7 @@
(type *) reallocarray(p_, n, sizeof(type)); \
})
#define REALLOCARRAYF(ptr, n, type) \
#define REALLOCF(ptr, n, type) \
({ \
__auto_type p_ = (ptr); \
\
@@ -48,7 +43,7 @@
(type *) reallocarrayf(p_, n, sizeof(type)); \
})
#define XREALLOCARRAY(ptr, n, type) \
#define XREALLOC(ptr, n, type) \
({ \
__auto_type p_ = (ptr); \
\
@@ -113,7 +108,7 @@ reallocarrayf(void *p, size_t nmemb, size_t size)
inline char *
xstrdup(const char *str)
{
return strcpy(XMALLOCARRAY(strlen(str) + 1, char), str);
return strcpy(XMALLOC(strlen(str) + 1, char), str);
}
+8 -8
View File
@@ -364,11 +364,11 @@ int commonio_lock_nowait (struct commonio_db *db, bool log)
}
file_len = strlen(db->filename) + 11;/* %lu max size */
lock_file_len = strlen(db->filename) + 6; /* sizeof ".lock" */
file = MALLOCARRAY(file_len, char);
file = MALLOC(file_len, char);
if (file == NULL) {
goto cleanup_ENOMEM;
}
lock = MALLOCARRAY(lock_file_len, char);
lock = MALLOC(lock_file_len, char);
if (lock == NULL) {
goto cleanup_ENOMEM;
}
@@ -640,7 +640,7 @@ int commonio_open (struct commonio_db *db, int mode)
}
buflen = BUFLEN;
buf = MALLOCARRAY (buflen, char);
buf = MALLOC(buflen, char);
if (NULL == buf) {
goto cleanup_ENOMEM;
}
@@ -651,7 +651,7 @@ int commonio_open (struct commonio_db *db, int mode)
size_t len;
buflen += BUFLEN;
cp = REALLOCARRAY (buf, buflen, char);
cp = REALLOC(buf, buflen, char);
if (NULL == cp) {
goto cleanup_buf;
}
@@ -685,7 +685,7 @@ int commonio_open (struct commonio_db *db, int mode)
}
}
p = MALLOC (struct commonio_entry);
p = MALLOC(1, struct commonio_entry);
if (NULL == p) {
goto cleanup_entry;
}
@@ -762,7 +762,7 @@ commonio_sort (struct commonio_db *db, int (*cmp) (const void *, const void *))
return 0;
}
entries = MALLOCARRAY (n, struct commonio_entry *);
entries = MALLOC(n, struct commonio_entry *);
if (entries == NULL) {
return -1;
}
@@ -1081,7 +1081,7 @@ int commonio_update (struct commonio_db *db, const void *eptr)
return 1;
}
/* not found, new entry */
p = MALLOC (struct commonio_entry);
p = MALLOC(1, struct commonio_entry);
if (NULL == p) {
db->ops->free (nentry);
errno = ENOMEM;
@@ -1118,7 +1118,7 @@ int commonio_append (struct commonio_db *db, const void *eptr)
return 0;
}
/* new entry */
p = MALLOC (struct commonio_entry);
p = MALLOC(1, struct commonio_entry);
if (NULL == p) {
db->ops->free (nentry);
errno = ENOMEM;
+2 -2
View File
@@ -448,14 +448,14 @@ void setdef_config_file (const char* file)
char* cp;
len = strlen(file) + strlen(sysconfdir) + 2;
cp = MALLOCARRAY(len, char);
cp = MALLOC(len, char);
if (cp == NULL)
exit (13);
snprintf(cp, len, "%s/%s", file, sysconfdir);
sysconfdir = cp;
#ifdef VENDORDIR
len = strlen(file) + strlen(vendordir) + 2;
cp = MALLOCARRAY(len, char);
cp = MALLOC(len, char);
if (cp == NULL)
exit (13);
snprintf(cp, len, "%s/%s", file, vendordir);
+2 -2
View File
@@ -312,7 +312,7 @@ static /*@null@*/struct commonio_entry *merge_group_entries (
/* Concatenate the 2 lines */
new_line_len = strlen (gr1->line) + strlen (gr2->line) +1;
new_line = MALLOCARRAY (new_line_len + 1, char);
new_line = MALLOC(new_line_len + 1, char);
if (NULL == new_line) {
return NULL;
}
@@ -394,7 +394,7 @@ static int split_groups (unsigned int max_members)
continue;
}
new = MALLOC (struct commonio_entry);
new = MALLOC(1, struct commonio_entry);
if (NULL == new) {
return 0;
}
+2 -2
View File
@@ -22,7 +22,7 @@
struct group *gr;
int i;
gr = MALLOC (struct group);
gr = MALLOC(1, struct group);
if (NULL == gr) {
return NULL;
}
@@ -47,7 +47,7 @@
for (i = 0; grent->gr_mem[i]; i++);
/*@-mustfreeonly@*/
gr->gr_mem = MALLOCARRAY (i + 1, char *);
gr->gr_mem = MALLOC(i + 1, char *);
/*@=mustfreeonly@*/
if (NULL == gr->gr_mem) {
gr_free(gr);
+6 -6
View File
@@ -66,7 +66,7 @@ static /*@null@*/char **build_list (char *s, char **list[], size_t * nlist)
while (s != NULL && *s != '\0') {
size = (nelem + 1) * sizeof (ptr);
ptr = REALLOCARRAY (*list, size, char *);
ptr = REALLOC(*list, size, char *);
if (NULL != ptr) {
ptr[nelem] = s;
nelem++;
@@ -80,7 +80,7 @@ static /*@null@*/char **build_list (char *s, char **list[], size_t * nlist)
}
}
size = (nelem + 1) * sizeof (ptr);
ptr = REALLOCARRAY (*list, size, char *);
ptr = REALLOC(*list, size, char *);
if (NULL != ptr) {
ptr[nelem] = NULL;
*list = ptr;
@@ -120,7 +120,7 @@ void endsgent (void)
size_t len = strlen (string) + 1;
if (len > sgrbuflen) {
char *buf = REALLOCARRAY (sgrbuf, len, char);
char *buf = REALLOC(sgrbuf, len, char);
if (NULL == buf) {
return NULL;
}
@@ -198,7 +198,7 @@ void endsgent (void)
char *cp;
if (0 == buflen) {
buf = MALLOCARRAY (BUFSIZ, char);
buf = MALLOC(BUFSIZ, char);
if (NULL == buf) {
return NULL;
}
@@ -219,7 +219,7 @@ void endsgent (void)
&& (feof (fp) == 0)) {
size_t len;
cp = REALLOCARRAY (buf, buflen * 2, char);
cp = REALLOC(buf, buflen * 2, char);
if (NULL == cp) {
return NULL;
}
@@ -440,7 +440,7 @@ int putsgent (const struct sgrp *sgrp, FILE * fp)
size += strlen (sgrp->sg_mem[i]) + 1;
}
buf = MALLOCARRAY (size, char);
buf = MALLOC(size, char);
if (NULL == buf) {
return -1;
}
+1 -1
View File
@@ -108,7 +108,7 @@ void nss_init(const char *nsswitch_path) {
fprintf(shadow_logfd, "Using files\n");
goto null_subid;
}
subid_nss = MALLOC(struct subid_nss_ops);
subid_nss = MALLOC(1, struct subid_nss_ops);
if (!subid_nss) {
goto close_lib;
}
+1 -1
View File
@@ -59,7 +59,7 @@ int run_parts (const char *directory, const char *name, const char *action)
struct stat sb;
path_length=strlen(directory) + strlen(namelist[n]->d_name) + 2;
char *s = MALLOCARRAY(path_length, char);
char *s = MALLOC(path_length, char);
if (!s) {
printf ("could not allocate memory\n");
for (; n<scanlist; n++) {
+2 -2
View File
@@ -46,7 +46,7 @@ static char **list (char *s)
member name, or terminating NULL). */
if (i >= size) {
size = i + 100; /* at least: i + 1 */
members = REALLOCARRAYF(members, size, char *);
members = REALLOCF(members, size, char *);
if (!members)
return NULL;
}
@@ -79,7 +79,7 @@ struct group *sgetgrent (const char *buf)
allocate a larger block */
free (grpbuf);
size = strlen (buf) + 1000; /* at least: strlen(buf) + 1 */
grpbuf = MALLOCARRAY (size, char);
grpbuf = MALLOC(size, char);
if (grpbuf == NULL) {
size = 0;
return NULL;
+2 -2
View File
@@ -50,7 +50,7 @@
for (i = 0; NULL != sgent->sg_adm[i]; i++);
/*@-mustfreeonly@*/
sg->sg_adm = MALLOCARRAY (i + 1, char *);
sg->sg_adm = MALLOC(i + 1, char *);
/*@=mustfreeonly@*/
if (NULL == sg->sg_adm) {
free (sg->sg_passwd);
@@ -75,7 +75,7 @@
for (i = 0; NULL != sgent->sg_mem[i]; i++);
/*@-mustfreeonly@*/
sg->sg_mem = MALLOCARRAY (i + 1, char *);
sg->sg_mem = MALLOC(i + 1, char *);
/*@=mustfreeonly@*/
if (NULL == sg->sg_mem) {
for (i = 0; NULL != sg->sg_adm[i]; i++) {
+1 -1
View File
@@ -32,7 +32,7 @@ int sssd_flush_cache (int dbflags)
if (rv == -1 && errno == ENOENT)
return 0;
sss_cache_args = MALLOCARRAY(4, char);
sss_cache_args = MALLOC(4, char);
if (sss_cache_args == NULL) {
return -1;
}
+4 -4
View File
@@ -34,7 +34,7 @@ static /*@null@*/ /*@only@*/void *subordinate_dup (const void *ent)
const struct subordinate_range *rangeent = ent;
struct subordinate_range *range;
range = MALLOC (struct subordinate_range);
range = MALLOC(1, struct subordinate_range);
if (NULL == range) {
return NULL;
}
@@ -316,12 +316,12 @@ static bool have_range(struct commonio_db *db,
static bool append_range(struct subid_range **ranges, const struct subordinate_range *new, int n)
{
if (!*ranges) {
*ranges = MALLOC(struct subid_range);
*ranges = MALLOC(1, struct subid_range);
if (!*ranges)
return false;
} else {
struct subid_range *alloced;
alloced = REALLOCARRAY(*ranges, n + 1, struct subid_range);
alloced = REALLOC(*ranges, n + 1, struct subid_range);
if (!alloced)
return false;
*ranges = alloced;
@@ -935,7 +935,7 @@ static int append_uids(uid_t **uids, const char *owner, int n)
return n;
}
ret = REALLOCARRAY(*uids, n + 1, uid_t);
ret = REALLOC(*uids, n + 1, uid_t);
if (!ret) {
free(*uids);
return -1;