From 25b2b325885710dc7dc7539d6d37e77182767088 Mon Sep 17 00:00:00 2001 From: Emil Velikov Date: Tue, 19 Feb 2019 15:30:38 +0000 Subject: [PATCH] loader: use a maximum of 64 drmDevices Currently that's the hard-coded maximum in the kernel, even though the libdrm API allows for more. Latter is done with extendability in mind. Allocate 64 pointers^Wdevices on stack for now. Making for shorter and ever-so-slightly faster code. v2: Use single MAX_DRM_DEVICES #define (Eric) Signed-off-by: Emil Velikov Reviewed-by: Adam Jackson (v1) Reviewed-by: Eric Engestrom Part-of: --- src/loader/loader.c | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/src/loader/loader.c b/src/loader/loader.c index a6ad2ee075b..96c1d4c0e6c 100644 --- a/src/loader/loader.c +++ b/src/loader/loader.c @@ -50,6 +50,7 @@ #ifdef HAVE_LIBDRM #include +#define MAX_DRM_DEVICES 64 #ifdef USE_DRICONF #include "util/xmlconfig.h" #include "util/xmlpool.h" @@ -132,27 +133,14 @@ is_kernel_i915(int fd) int loader_open_render_node(const char *name) { - drmDevicePtr *devices, device; - int err, render = -ENOENT, fd; - unsigned int num, i; + drmDevicePtr devices[MAX_DRM_DEVICES], device; + int i, num_devices, fd = -1; - err = drmGetDevices2(0, NULL, 0); - if (err < 0) - return err; - - num = err; - - devices = calloc(num, sizeof(*devices)); - if (!devices) - return -ENOMEM; - - err = drmGetDevices2(0, devices, num); - if (err < 0) { - render = err; - goto free; - } + num_devices = drmGetDevices2(0, devices, MAX_DRM_DEVICES); + if (num_devices <= 0) + return -ENOENT; - for (i = 0; i < num; i++) { + for (i = 0; i < num_devices; i++) { device = devices[i]; if ((device->available_nodes & (1 << DRM_NODE_RENDER)) && @@ -176,16 +164,15 @@ loader_open_render_node(const char *name) } drmFreeVersion(version); - render = fd; break; } } + drmFreeDevices(devices, num_devices); - drmFreeDevices(devices, num); + if (i == num_devices) + return -ENOENT; -free: - free(devices); - return render; + return fd; } #ifdef USE_DRICONF @@ -310,8 +297,6 @@ static char *drm_get_id_path_tag_for_fd(int fd) int loader_get_user_preferred_fd(int default_fd, bool *different_device) { -/* Arbitrary "maximum" value of drm devices. */ -#define MAX_DRM_DEVICES 32 const char *dri_prime = getenv("DRI_PRIME"); char *default_tag, *prime = NULL; drmDevicePtr devices[MAX_DRM_DEVICES]; -- 2.30.2