diff options
author | Phil Jones <philj56@gmail.com> | 2022-06-07 13:47:35 +0100 |
---|---|---|
committer | Phil Jones <philj56@gmail.com> | 2022-06-07 15:31:49 +0100 |
commit | 51bbf779ba2c9d5954e2c9470a8eae7c1ddd38a5 (patch) | |
tree | f2b52f0211f9052fefa64e23c6a0d81305391589 /src/shm.c | |
parent | 7562d7b539d8013376de2cff494231ba307f4ee1 (diff) |
Switch from using (E)GL to wl_shm.
eglInitialize() is slow (~50-100ms), and uses a fair amount of memory
(~100 MB). For such a small, simple program that just wants to launch as
quickly as possible, wl_shm performs better.
Diffstat (limited to 'src/shm.c')
-rw-r--r-- | src/shm.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/shm.c b/src/shm.c new file mode 100644 index 0000000..e145d4a --- /dev/null +++ b/src/shm.c @@ -0,0 +1,49 @@ +#include <errno.h> +#include <fcntl.h> +#include <sys/mman.h> +#include <time.h> +#include <unistd.h> +#include "shm.h" + +static void randname(char *buf) +{ + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + long r = ts.tv_nsec; + for (int i = 0; i < 6; ++i) { + buf[i] = 'A'+(r&15)+(r&16)*2; + r >>= 5; + } +} + +static int create_shm_file(void) +{ + int retries = 100; + do { + char name[] = "/wl_shm-XXXXXX"; + randname(name + sizeof(name) - 7); + --retries; + int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600); + if (fd >= 0) { + shm_unlink(name); + return fd; + } + } while (retries > 0 && errno == EEXIST); + return -1; +} + +int shm_allocate_file(size_t size) +{ + int fd = create_shm_file(); + if (fd < 0) + return -1; + int ret; + do { + ret = ftruncate(fd, size); + } while (ret < 0 && errno == EINTR); + if (ret < 0) { + close(fd); + return -1; + } + return fd; +} |