[Mesa-dev] [PATCH] gbm: Set errno on errors

Ander Conselvan de Oliveira conselvan2 at gmail.com
Tue Mar 25 04:58:28 PDT 2014


From: Ander Conselvan de Oliveira <ander.conselvan.de.oliveira at intel.com>

This should give the caller some information of what called the error.
For the gbm_bo_import() case, for instance, it is possible to know if
the import is not supported or the error was caused by an invalid
parameter.
---
 src/gbm/backends/dri/gbm_dri.c | 38 ++++++++++++++++++++++++++++++--------
 src/gbm/main/gbm.c             | 20 +++++++++++++-------
 2 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/src/gbm/backends/dri/gbm_dri.c b/src/gbm/backends/dri/gbm_dri.c
index acf6b24..92b6fac 100644
--- a/src/gbm/backends/dri/gbm_dri.c
+++ b/src/gbm/backends/dri/gbm_dri.c
@@ -30,6 +30,7 @@
 #include <stddef.h>
 #include <stdint.h>
 #include <string.h>
+#include <errno.h>
 #include <limits.h>
 
 #include <sys/types.h>
@@ -353,8 +354,10 @@ gbm_dri_bo_write(struct gbm_bo *_bo, const void *buf, size_t count)
 {
    struct gbm_dri_bo *bo = gbm_dri_bo(_bo);
 
-   if (bo->image != NULL)
+   if (bo->image != NULL) {
+      errno = EINVAL;
       return -1;
+   }
 
    memcpy(bo->map, buf, count);
 
@@ -417,8 +420,10 @@ gbm_dri_bo_import(struct gbm_device *gbm,
    int gbm_format;
 
    /* Required for query image WIDTH & HEIGHT */
-   if (dri->image->base.version < 4)
+   if (dri->image->base.version < 4) {
+      errno = ENOSYS;
       return NULL;
+   }
 
    switch (type) {
 #if HAVE_WAYLAND_PLATFORM
@@ -426,12 +431,16 @@ gbm_dri_bo_import(struct gbm_device *gbm,
    {
       struct wl_drm_buffer *wb;
 
-      if (!dri->wl_drm)
+      if (!dri->wl_drm) {
+         errno = EINVAL;
          return NULL;
+      }
 
       wb = wayland_drm_buffer_get(dri->wl_drm, (struct wl_resource *) buffer);
-      if (!wb)
+      if (!wb) {
+         errno = EINVAL;
          return NULL;
+      }
 
       image = wb->driver_buffer;
 
@@ -458,18 +467,23 @@ gbm_dri_bo_import(struct gbm_device *gbm,
    case GBM_BO_IMPORT_EGL_IMAGE:
    {
       int dri_format;
-      if (dri->lookup_image == NULL)
+      if (dri->lookup_image == NULL) {
+         errno = EINVAL;
          return NULL;
+      }
 
       image = dri->lookup_image(dri->screen, buffer, dri->lookup_user_data);
       dri->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &dri_format);
       gbm_format = gbm_dri_to_gbm_format(dri_format);
-      if (gbm_format == 0)
+      if (gbm_format == 0) {
+         errno = EINVAL;
          return NULL;
+      }
       break;
    }
 
    default:
+      errno = ENOSYS;
       return NULL;
    }
 
@@ -486,6 +500,7 @@ gbm_dri_bo_import(struct gbm_device *gbm,
       dri_use |= __DRI_IMAGE_USE_CURSOR;
    if (dri->image->base.version >= 2 &&
        !dri->image->validateUsage(bo->image, dri_use)) {
+      errno = EINVAL;
       free(bo);
       return NULL;
    }
@@ -517,10 +532,14 @@ create_dumb(struct gbm_device *gbm,
    struct drm_mode_destroy_dumb destroy_arg;
    int ret;
 
-   if (!(usage & GBM_BO_USE_CURSOR_64X64))
+   if (!(usage & GBM_BO_USE_CURSOR_64X64)) {
+      errno = EINVAL;
       return NULL;
-   if (format != GBM_FORMAT_ARGB8888)
+   }
+   if (format != GBM_FORMAT_ARGB8888) {
+      errno = EINVAL;
       return NULL;
+   }
 
    bo = calloc(1, sizeof *bo);
    if (bo == NULL)
@@ -611,6 +630,7 @@ gbm_dri_bo_create(struct gbm_device *gbm,
       dri_format = __DRI_IMAGE_FORMAT_XRGB2101010;
       break;
    default:
+      errno = EINVAL;
       goto failed;
    }
 
@@ -690,6 +710,8 @@ dri_device_create(int fd)
    int ret;
 
    dri = calloc(1, sizeof *dri);
+   if (!dri)
+      return NULL;
 
    dri->base.base.fd = fd;
    dri->base.base.bo_create = gbm_dri_bo_create;
diff --git a/src/gbm/main/gbm.c b/src/gbm/main/gbm.c
index 30785a6..7ce2ae2 100644
--- a/src/gbm/main/gbm.c
+++ b/src/gbm/main/gbm.c
@@ -36,6 +36,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <errno.h>
 
 #include "gbm.h"
 #include "gbmint.h"
@@ -109,7 +110,7 @@ _gbm_mesa_get_device(int fd)
    int i;
 
    if (fd < 0 || fstat(fd, &buf) < 0 || !S_ISCHR(buf.st_mode)) {
-      fprintf(stderr, "_gbm_mesa_get_device: invalid fd: %d\n", fd);
+      errno = EINVAL;
       return NULL;
    }
 
@@ -145,7 +146,7 @@ gbm_create_device(int fd)
    struct stat buf;
 
    if (fd < 0 || fstat(fd, &buf) < 0 || !S_ISCHR(buf.st_mode)) {
-      fprintf(stderr, "gbm_create_device: invalid fd: %d\n", fd);
+      errno = EINVAL;
       return NULL;
    }
 
@@ -241,7 +242,7 @@ gbm_bo_get_handle(struct gbm_bo *bo)
  * \param bo The buffer object
  * \param buf The data to write
  * \param count The number of bytes to write
- * \return Returns -1 on error, 0 otherwise
+ * \return Returns 0 on success, otherwise -1 is returned an errno set
  */
 GBM_EXPORT int
 gbm_bo_write(struct gbm_bo *bo, const void *buf, size_t count)
@@ -315,7 +316,7 @@ gbm_bo_destroy(struct gbm_bo *bo)
  *
  * \return A newly allocated buffer that should be freed with gbm_bo_destroy()
  * when no longer needed. If an error occurs during allocation %NULL will be
- * returned.
+ * returned and errno set.
  *
  * \sa enum gbm_bo_format for the list of formats
  * \sa enum gbm_bo_flags for the list of usage flags
@@ -325,12 +326,16 @@ gbm_bo_create(struct gbm_device *gbm,
               uint32_t width, uint32_t height,
               uint32_t format, uint32_t usage)
 {
-   if (width == 0 || height == 0)
+   if (width == 0 || height == 0) {
+      errno = EINVAL;
       return NULL;
+   }
 
    if (usage & GBM_BO_USE_CURSOR_64X64 &&
-       (width != 64 || height != 64))
+       (width != 64 || height != 64)) {
+      errno = EINVAL;
       return NULL;
+   }
 
    return gbm->bo_create(gbm, width, height, format, usage);
 }
@@ -355,7 +360,8 @@ gbm_bo_create(struct gbm_device *gbm,
  * \param usage The union of the usage flags for this buffer
  *
  * \return A newly allocated buffer object that should be freed with
- * gbm_bo_destroy() when no longer needed.
+ * gbm_bo_destroy() when no longer needed. On error, %NULL is returned
+ * and errno is set.
  *
  * \sa enum gbm_bo_flags for the list of usage flags
  */
-- 
1.8.3.2



More information about the mesa-dev mailing list