diff options
author | Keith Packard <keithp@keithp.com> | 2014-10-09 14:01:21 +0200 |
---|---|---|
committer | Keith Packard <keithp@keithp.com> | 2014-10-09 14:09:09 +0200 |
commit | b63ea144a0439c54a3a147274afeeb115caced5a (patch) | |
tree | 061586cef6548383e9e45711dd6904e4d9409d46 /src/xshmfence_alloc.c | |
parent | 9c4f070e1304a3503cfab08f68573443025fc4c9 (diff) | |
download | xorg-lib-libxshmfence-b63ea144a0439c54a3a147274afeeb115caced5a.tar.gz |
Use linux 3.17 memfd_create syscall when available
Linux 3.17 introduces a new anonymous memory allocation that returns a
file descriptor which we can pass around. Use this in preference to
creating a file in the filesystem where available.
Signed-off-by: Keith Packard <keithp@keithp.com>
Diffstat (limited to 'src/xshmfence_alloc.c')
-rw-r--r-- | src/xshmfence_alloc.c | 50 |
1 files changed, 42 insertions, 8 deletions
diff --git a/src/xshmfence_alloc.c b/src/xshmfence_alloc.c index 58416cd..05cf953 100644 --- a/src/xshmfence_alloc.c +++ b/src/xshmfence_alloc.c @@ -26,6 +26,34 @@ #include "xshmfenceint.h" +#if !HAVE_MEMFD_CREATE +#if HAVE_DECL___NR_MEMFD_CREATE +#include <asm/unistd.h> +static int memfd_create(const char *name, + unsigned int flags) +{ + return syscall(__NR_memfd_create, name, flags); +} +#define HAVE_MEMFD_CREATE 1 +#endif +#endif + +#if HAVE_MEMFD_CREATE + +/* Get defines for the memfd_create syscall, using the + * header if available, or just defining the constants otherwise + */ + +#if HAVE_MEMFD_H +#include <sys/memfd.h> +#else +/* flags for memfd_create(2) (unsigned int) */ +#define MFD_CLOEXEC 0x0001U +#define MFD_ALLOW_SEALING 0x0002U +#endif + +#endif + /** * xshmfence_alloc_shm: * @@ -41,16 +69,22 @@ xshmfence_alloc_shm(void) char template[] = SHMDIR "/shmfd-XXXXXX"; int fd; -#ifdef O_TMPFILE - fd = open(SHMDIR, O_TMPFILE|O_RDWR|O_CLOEXEC|O_EXCL, 0666); +#if HAVE_MEMFD_CREATE + fd = memfd_create("xshmfence", MFD_CLOEXEC|MFD_ALLOW_SEALING); if (fd < 0) #endif - { - fd = mkstemp(template); - if (fd < 0) - return fd; - unlink(template); - } + { +#ifdef O_TMPFILE + fd = open(SHMDIR, O_TMPFILE|O_RDWR|O_CLOEXEC|O_EXCL, 0666); + if (fd < 0) +#endif + { + fd = mkstemp(template); + if (fd < 0) + return fd; + unlink(template); + } + } if (ftruncate(fd, sizeof (struct xshmfence)) < 0) { close(fd); return -1; |