All of lore.kernel.org
 help / color / mirror / Atom feed
From: Robert Foss <robert.foss@collabora.com>
To: Emil Velikov <emil.l.velikov@gmail.com>, dri-devel@lists.freedesktop.org
Subject: Re: [PATCH libdrm v2 04/10] xf86drm: Allocate drmDevicePtr's on stack
Date: Fri, 29 Jun 2018 17:49:47 +0200	[thread overview]
Message-ID: <1161b785-3e45-1702-0bc2-da568d399206@collabora.com> (raw)
In-Reply-To: <20180629152017.29843-1-emil.l.velikov@gmail.com>

LGTM

On 2018-06-29 17:20, Emil Velikov wrote:
> From: Emil Velikov <emil.velikov@collabora.com>
> 
> Currently we dynamically allocate 16 pointers and reallocate more as
> needed.
> 
> Instead, allocate the maximum number (256) on stack - the number is
> small enough and is unlikely to change in the foreseeable future.
> 
> This allows us to simplify the error handling and even shed a few bytes
> off the final binary.
> 
> v2:
>   - add a define & description behind the magic 256 (Rob)
>   - report error to strerr and skip when over 256 device nodes (Rob)
> 
> Cc: Robert Foss <robert.foss@collabora.com>
> Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
> Tested-by: Robert Foss <robert.foss@collabora.com> (v1)
> Reviewed-by: Robert Foss <robert.foss@collabora.com> (v1)
> Reviewed-by: Eric Engestrom <eric@engestrom.ch> (v1)
> ---
>   xf86drm.c | 79 ++++++++++++++++---------------------------------------
>   1 file changed, 23 insertions(+), 56 deletions(-)
> 
> diff --git a/xf86drm.c b/xf86drm.c
> index 114cf855..02da3e1f 100644
> --- a/xf86drm.c
> +++ b/xf86drm.c
> @@ -3766,6 +3766,13 @@ drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev)
>       return false;
>   }
>   
> +/*
> + * The kernel drm core has a number of places that assume maximum of
> + * 3x64 devices nodes. That's 64 for each of primary, control and
> + * render nodes. Rounded it up to 256 for simplicity.
> + */
> +#define MAX_DRM_NODES 256
> +
>   /**
>    * Get information about the opened drm device
>    *
> @@ -3846,7 +3853,7 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
>   
>       return 0;
>   #else
> -    drmDevicePtr *local_devices;
> +    drmDevicePtr local_devices[MAX_DRM_NODES];
>       drmDevicePtr d;
>       DIR *sysdir;
>       struct dirent *dent;
> @@ -3854,7 +3861,6 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
>       int subsystem_type;
>       int maj, min;
>       int ret, i, node_count;
> -    int max_count = 16;
>       dev_t find_rdev;
>   
>       if (drm_device_validate_flags(flags))
> @@ -3877,15 +3883,9 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
>       if (subsystem_type < 0)
>           return subsystem_type;
>   
> -    local_devices = calloc(max_count, sizeof(drmDevicePtr));
> -    if (local_devices == NULL)
> -        return -ENOMEM;
> -
>       sysdir = opendir(DRM_DIR_NAME);
> -    if (!sysdir) {
> -        ret = -errno;
> -        goto free_locals;
> -    }
> +    if (!sysdir)
> +        return -errno;
>   
>       i = 0;
>       while ((dent = readdir(sysdir))) {
> @@ -3893,16 +3893,12 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
>           if (ret)
>               continue;
>   
> -        if (i >= max_count) {
> -            drmDevicePtr *temp;
> -
> -            max_count += 16;
> -            temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
> -            if (!temp)
> -                goto free_devices;
> -            local_devices = temp;
> +        if (i >= MAX_DRM_NODES) {
> +            fprintf(stderr, "More than %d drm nodes detected. "
> +                    "Please report a bug - that should not happen.\n"
> +                    "Skipping extra nodes\n", MAX_DRM_NODES);
> +            break;
>           }
> -
>           local_devices[i] = d;
>           i++;
>       }
> @@ -3921,18 +3917,9 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
>       }
>   
>       closedir(sysdir);
> -    free(local_devices);
>       if (*device == NULL)
>           return -ENODEV;
>       return 0;
> -
> -free_devices:
> -    drmFreeDevices(local_devices, i);
> -    closedir(sysdir);
> -
> -free_locals:
> -    free(local_devices);
> -    return ret;
>   #endif
>   }
>   
> @@ -3968,25 +3955,18 @@ int drmGetDevice(int fd, drmDevicePtr *device)
>    */
>   int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_devices)
>   {
> -    drmDevicePtr *local_devices;
> +    drmDevicePtr local_devices[MAX_DRM_NODES];
>       drmDevicePtr device;
>       DIR *sysdir;
>       struct dirent *dent;
>       int ret, i, node_count, device_count;
> -    int max_count = 16;
>   
>       if (drm_device_validate_flags(flags))
>           return -EINVAL;
>   
> -    local_devices = calloc(max_count, sizeof(drmDevicePtr));
> -    if (local_devices == NULL)
> -        return -ENOMEM;
> -
>       sysdir = opendir(DRM_DIR_NAME);
> -    if (!sysdir) {
> -        ret = -errno;
> -        goto free_locals;
> -    }
> +    if (!sysdir)
> +        return -errno;
>   
>       i = 0;
>       while ((dent = readdir(sysdir))) {
> @@ -3994,16 +3974,12 @@ int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_devices)
>           if (ret)
>               continue;
>   
> -        if (i >= max_count) {
> -            drmDevicePtr *temp;
> -
> -            max_count += 16;
> -            temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
> -            if (!temp)
> -                goto free_devices;
> -            local_devices = temp;
> +        if (i >= MAX_DRM_NODES) {
> +            fprintf(stderr, "More than %d drm nodes detected. "
> +                    "Please report a bug - that should not happen.\n"
> +                    "Skipping extra nodes\n", MAX_DRM_NODES);
> +            break;
>           }
> -
>           local_devices[i] = device;
>           i++;
>       }
> @@ -4025,16 +4001,7 @@ int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_devices)
>       }
>   
>       closedir(sysdir);
> -    free(local_devices);
>       return device_count;
> -
> -free_devices:
> -    drmFreeDevices(local_devices, i);
> -    closedir(sysdir);
> -
> -free_locals:
> -    free(local_devices);
> -    return ret;
>   }
>   
>   /**
> 
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

  reply	other threads:[~2018-06-29 15:49 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-25 17:36 [PATCH libdrm 01/10] xf86drm: drmGetDevice2: error out if the fd has unknown subsys Emil Velikov
2018-06-25 17:36 ` [PATCH libdrm 02/10] xf86drm: introduce drm_device_has_rdev() helper Emil Velikov
2018-06-28 10:14   ` Robert Foss
2018-06-25 17:36 ` [PATCH libdrm 03/10] xf86drm: Fold drmDevice processing into process_device() helper Emil Velikov
2018-06-28 11:50   ` Robert Foss
2018-06-25 17:36 ` [PATCH libdrm 04/10] xf86drm: Allocate drmDevicePtr's on stack Emil Velikov
2018-06-28 12:52   ` Robert Foss
2018-06-28 17:07     ` Emil Velikov
2018-06-29 15:20   ` [PATCH libdrm v2 " Emil Velikov
2018-06-29 15:49     ` Robert Foss [this message]
2018-06-29 15:22   ` [PATCH libdrm v2 05/10] xf86drm: introduce a get_real_pci_path() helper Emil Velikov
2018-06-25 17:36 ` [PATCH libdrm " Emil Velikov
2018-06-28 10:21   ` Eric Engestrom
2018-06-28 10:23     ` Eric Engestrom
2018-06-28 16:42     ` Emil Velikov
2018-06-28 16:06   ` Robert Foss
2018-06-25 17:36 ` [PATCH libdrm 06/10] xf86drm: Add drmDevice support for virtio_gpu Emil Velikov
2018-06-28 16:08   ` Robert Foss
2018-06-25 17:36 ` [PATCH libdrm 07/10] tests/drmdevices: install alongside other utilities Emil Velikov
2018-06-28 16:09   ` Robert Foss
2018-06-25 17:36 ` [PATCH libdrm 08/10] tests/drmdevice: add a couple of printf headers Emil Velikov
2018-06-28 16:09   ` Robert Foss
2018-06-25 17:36 ` [PATCH libdrm 09/10] drmdevice: convert the tabbed output into a tree Emil Velikov
2018-06-28 10:19   ` Eric Engestrom
2018-06-28 16:43     ` Emil Velikov
2018-06-28 16:09   ` Robert Foss
2018-06-29 15:24   ` [PATCH libdrm v2 " Emil Velikov
2018-06-25 17:36 ` [PATCH libdrm 10/10] drmdevice: print the correct host1x information Emil Velikov
2018-06-28 16:09   ` Robert Foss
2018-06-28 10:11 ` [PATCH libdrm 01/10] xf86drm: drmGetDevice2: error out if the fd has unknown subsys Robert Foss
2018-06-28 10:11 ` Robert Foss

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1161b785-3e45-1702-0bc2-da568d399206@collabora.com \
    --to=robert.foss@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=emil.l.velikov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.