aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrooks Davis <brooks@FreeBSD.org>2023-11-27 17:07:06 +0000
committerBrooks Davis <brooks@FreeBSD.org>2023-11-28 17:09:27 +0000
commitc96772227b7dfcaf4eec4d07acb5c916643aca3a (patch)
treef244f052cf87ed58f032e563df3074906d999f1c
parentc3207e2d2554c8e36f9cf5950f8cd52a19fedfd5 (diff)
downloadsrc-c96772227b7dfcaf4eec4d07acb5c916643aca3a.tar.gz
src-c96772227b7dfcaf4eec4d07acb5c916643aca3a.zip
memfd_create: don't allocate heap memory
Rather than calling calloc() to allocate space for a page size array to pass to getpagesizes(), just follow the getpagesizes() implementation and allocate MAXPAGESIZES elements on the stack. This avoids the need for the allocation. While this does mean that a new libc is required to take advantage of a new huge page size, that was already true due to getpagesizes() using a static buffer of MAXPAGESIZES elements. Reviewed by: kevans, imp, emaste Sponsored by: DARPA Differential Revision: https://reviews.freebsd.org/D42710
-rw-r--r--lib/libc/gen/memfd_create.c16
1 files changed, 3 insertions, 13 deletions
diff --git a/lib/libc/gen/memfd_create.c b/lib/libc/gen/memfd_create.c
index b26d638656a4..78131f46d7b1 100644
--- a/lib/libc/gen/memfd_create.c
+++ b/lib/libc/gen/memfd_create.c
@@ -35,7 +35,6 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
-#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@@ -52,7 +51,8 @@ int
memfd_create(const char *name, unsigned int flags)
{
char memfd_name[NAME_MAX + 1];
- size_t namelen, *pgs, pgsize;
+ size_t pgs[MAXPAGESIZES];
+ size_t namelen, pgsize;
struct shm_largepage_conf slc;
int error, fd, npgs, oflags, pgidx, saved_errno, shmflags;
@@ -92,16 +92,9 @@ memfd_create(const char *name, unsigned int flags)
if (fd == -1 || (flags & MFD_HUGETLB) == 0)
return (fd);
- pgs = NULL;
- npgs = getpagesizes(NULL, 0);
+ npgs = getpagesizes(pgs, nitems(pgs));
if (npgs == -1)
goto clean;
- pgs = calloc(npgs, sizeof(size_t));
- if (pgs == NULL)
- goto clean;
- error = getpagesizes(pgs, npgs);
- if (error == -1)
- goto clean;
pgsize = (size_t)1 << ((flags & MFD_HUGE_MASK) >> MFD_HUGE_SHIFT);
for (pgidx = 0; pgidx < npgs; pgidx++) {
if (pgsize == pgs[pgidx])
@@ -111,8 +104,6 @@ memfd_create(const char *name, unsigned int flags)
errno = EOPNOTSUPP;
goto clean;
}
- free(pgs);
- pgs = NULL;
memset(&slc, 0, sizeof(slc));
slc.psind = pgidx;
@@ -125,7 +116,6 @@ memfd_create(const char *name, unsigned int flags)
clean:
saved_errno = errno;
close(fd);
- free(pgs);
errno = saved_errno;
return (-1);
}