From 3193ab5807bf09f221644f9bb7100a2aa893b683 Mon Sep 17 00:00:00 2001 From: Leandro Ribeiro Date: Tue, 27 Apr 2021 19:00:34 -0300 Subject: [PATCH] backend-drm: simplify compile time array copy In drm_fb_get_from_dmabuf() we have some compile time array copies, and multiple static_assert() calls are needed (for safety). This makes the code unpleasant to read. Add ARRAY_COPY macro, to simplify the code. Signed-off-by: Leandro Ribeiro --- libweston/backend-drm/fb.c | 41 +++++--------------------------------- shared/helpers.h | 19 ++++++++++++++++++ 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/libweston/backend-drm/fb.c b/libweston/backend-drm/fb.c index 986a4aa8..4943793f 100644 --- a/libweston/backend-drm/fb.c +++ b/libweston/backend-drm/fb.c @@ -267,30 +267,9 @@ drm_fb_get_from_dmabuf(struct linux_dmabuf_buffer *dmabuf, fb->refcnt = 1; fb->type = BUFFER_DMABUF; - static_assert(ARRAY_LENGTH(import_mod.fds) == - ARRAY_LENGTH(dmabuf->attributes.fd), - "GBM and linux_dmabuf FD size must match"); - static_assert(sizeof(import_mod.fds) == sizeof(dmabuf->attributes.fd), - "GBM and linux_dmabuf FD size must match"); - memcpy(import_mod.fds, dmabuf->attributes.fd, sizeof(import_mod.fds)); - - static_assert(ARRAY_LENGTH(import_mod.strides) == - ARRAY_LENGTH(dmabuf->attributes.stride), - "GBM and linux_dmabuf stride size must match"); - static_assert(sizeof(import_mod.strides) == - sizeof(dmabuf->attributes.stride), - "GBM and linux_dmabuf stride size must match"); - memcpy(import_mod.strides, dmabuf->attributes.stride, - sizeof(import_mod.strides)); - - static_assert(ARRAY_LENGTH(import_mod.offsets) == - ARRAY_LENGTH(dmabuf->attributes.offset), - "GBM and linux_dmabuf offset size must match"); - static_assert(sizeof(import_mod.offsets) == - sizeof(dmabuf->attributes.offset), - "GBM and linux_dmabuf offset size must match"); - memcpy(import_mod.offsets, dmabuf->attributes.offset, - sizeof(import_mod.offsets)); + ARRAY_COPY(import_mod.fds, dmabuf->attributes.fd); + ARRAY_COPY(import_mod.strides, dmabuf->attributes.stride); + ARRAY_COPY(import_mod.offsets, dmabuf->attributes.offset); fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD_MODIFIER, &import_mod, GBM_BO_USE_SCANOUT); @@ -303,18 +282,8 @@ drm_fb_get_from_dmabuf(struct linux_dmabuf_buffer *dmabuf, fb->size = 0; fb->fd = backend->drm.fd; - static_assert(ARRAY_LENGTH(fb->strides) == - ARRAY_LENGTH(dmabuf->attributes.stride), - "drm_fb and dmabuf stride size must match"); - static_assert(sizeof(fb->strides) == sizeof(dmabuf->attributes.stride), - "drm_fb and dmabuf stride size must match"); - memcpy(fb->strides, dmabuf->attributes.stride, sizeof(fb->strides)); - static_assert(ARRAY_LENGTH(fb->offsets) == - ARRAY_LENGTH(dmabuf->attributes.offset), - "drm_fb and dmabuf offset size must match"); - static_assert(sizeof(fb->offsets) == sizeof(dmabuf->attributes.offset), - "drm_fb and dmabuf offset size must match"); - memcpy(fb->offsets, dmabuf->attributes.offset, sizeof(fb->offsets)); + ARRAY_COPY(fb->strides, dmabuf->attributes.stride); + ARRAY_COPY(fb->offsets, dmabuf->attributes.offset); fb->format = pixel_format_get_info(dmabuf->attributes.format); if (!fb->format) { diff --git a/shared/helpers.h b/shared/helpers.h index 44f2f764..eb0a8f78 100644 --- a/shared/helpers.h +++ b/shared/helpers.h @@ -41,6 +41,25 @@ extern "C" { #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) #endif +#define STRING(s) #s + +/** + * Compile-time copy of hardcoded arrays. + * + * @param dst the array to copy to. + * @param src the source array. + */ +#ifndef ARRAY_COPY +#define ARRAY_COPY(dst, src) \ +do { \ + static_assert(ARRAY_LENGTH(src) == ARRAY_LENGTH(dst), \ + "src and dst sizes must match"); \ + static_assert(sizeof(src) == sizeof(dst), \ + "src and dst sizes must match"); \ + memcpy((src), (dst), sizeof(src)); \ +} while (0) +#endif + /** * Returns the smaller of two values. *