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. *