[Mesa-dev] [PATCH 1/4] loader: use a maximum of 64 drmDevices

Emil Velikov emil.l.velikov at gmail.com
Tue Feb 19 15:30:38 UTC 2019


From: Emil Velikov <emil.velikov at collabora.com>

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.

Signed-off-by: Emil Velikov <emil.velikov at collabora.com>
---
 src/loader/loader.c | 40 ++++++++++++++--------------------------
 1 file changed, 14 insertions(+), 26 deletions(-)

diff --git a/src/loader/loader.c b/src/loader/loader.c
index 0f799c14fd8..56187080bbc 100644
--- a/src/loader/loader.c
+++ b/src/loader/loader.c
@@ -110,27 +110,15 @@ static char *loader_get_kernel_driver_name(int fd)
 int
 loader_open_render_node(const char *name)
 {
-   drmDevicePtr *devices, device;
-   int err, render = -ENOENT, fd;
-   unsigned int num, i;
-
-   err = drmGetDevices2(0, NULL, 0);
-   if (err < 0)
-      return err;
-
-   num = err;
-
-   devices = calloc(num, sizeof(*devices));
-   if (!devices)
-      return -ENOMEM;
+#define MAX_DRM_DEVICES 64
+   drmDevicePtr devices[MAX_DRM_DEVICES], device;
+   int i, num_devices, fd;
 
-   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 num_devices;
 
-   for (i = 0; i < num; i++) {
+   for (i = 0; i < num_devices; i++) {
       device = devices[i];
 
       if ((device->available_nodes & (1 << DRM_NODE_RENDER)) &&
@@ -154,16 +142,16 @@ 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;
+#undef MAX_DRM_DEVICES
 }
 
 #ifdef USE_DRICONF
@@ -287,8 +275,7 @@ 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
+#define MAX_DRM_DEVICES 64
    const char *dri_prime = getenv("DRI_PRIME");
    char *default_tag, *prime = NULL;
    drmDevicePtr devices[MAX_DRM_DEVICES];
@@ -367,6 +354,7 @@ int loader_get_user_preferred_fd(int default_fd, bool *different_device)
    free(default_tag);
    free(prime);
    return default_fd;
+#undef MAX_DRM_DEVICES
 }
 #else
 int
-- 
2.20.1



More information about the mesa-dev mailing list