[v5,06/12] drm/cma-helper: Turn to_drm_gem_cma_obj() into a macro

Submitted by Noralf Trønnes on Nov. 7, 2017, 7:13 p.m.

Details

Message ID 20171107191348.17555-7-noralf@tronnes.org
State Accepted
Commit 8d25ccebef55b8fc47b674f24ccdccbef08e2f32
Headers show
Series "drm/framebuffer: Add framebuffer debugfs file" ( rev: 5 ) in DRI devel

Not browsing as part of any series.

Commit Message

Noralf Trønnes Nov. 7, 2017, 7:13 p.m.
This allows the argument to be a const.

The other option was to keep it an inline function and make the argument
a const:

static inline struct drm_gem_cma_object *
to_drm_gem_cma_obj(const struct drm_gem_object *gem_obj)
{
	return container_of(gem_obj, struct drm_gem_cma_object, base);
}

This will happily return a non-const pointer to the drm_gem_cma_object
based on a const pointer to the contained drm_gem_object, thus creating
const-safety problems.

There was an attempt to fix the problem in the container_of() macro
itself (see https://lkml.org/lkml/2017/5/19/381) but the patch seems to
have fallen through the cracks. It would require turning this inline
function into a macro.

By making this a macro now, we will benefit from a possible future
enhancement of container_of(). We don't loose type checking by doing
this, container_of() takes care of that.

Suggested-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
---
 include/drm/drm_gem_cma_helper.h | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

Patch hide | download patch | download mbox

diff --git a/include/drm/drm_gem_cma_helper.h b/include/drm/drm_gem_cma_helper.h
index 58a739bf15f1..7a3dcf0cf289 100644
--- a/include/drm/drm_gem_cma_helper.h
+++ b/include/drm/drm_gem_cma_helper.h
@@ -20,11 +20,8 @@  struct drm_gem_cma_object {
 	void *vaddr;
 };
 
-static inline struct drm_gem_cma_object *
-to_drm_gem_cma_obj(struct drm_gem_object *gem_obj)
-{
-	return container_of(gem_obj, struct drm_gem_cma_object, base);
-}
+#define to_drm_gem_cma_obj(gem_obj) \
+	container_of(gem_obj, struct drm_gem_cma_object, base)
 
 #ifndef CONFIG_MMU
 #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS \

Comments

Hi Noralf,

Thank you for the patch.

On Tuesday, 7 November 2017 21:13:42 EET Noralf Trønnes wrote:
> This allows the argument to be a const.
> 
> The other option was to keep it an inline function and make the argument
> a const:
> 
> static inline struct drm_gem_cma_object *
> to_drm_gem_cma_obj(const struct drm_gem_object *gem_obj)
> {
> 	return container_of(gem_obj, struct drm_gem_cma_object, base);
> }
> 
> This will happily return a non-const pointer to the drm_gem_cma_object
> based on a const pointer to the contained drm_gem_object, thus creating
> const-safety problems.
> 
> There was an attempt to fix the problem in the container_of() macro
> itself (see https://lkml.org/lkml/2017/5/19/381) but the patch seems to
> have fallen through the cracks. It would require turning this inline
> function into a macro.
> 
> By making this a macro now, we will benefit from a possible future
> enhancement of container_of(). We don't loose type checking by doing
> this, container_of() takes care of that.
> 
> Suggested-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Signed-off-by: Noralf Trønnes <noralf@tronnes.org>

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
>  include/drm/drm_gem_cma_helper.h | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
> 
> diff --git a/include/drm/drm_gem_cma_helper.h
> b/include/drm/drm_gem_cma_helper.h index 58a739bf15f1..7a3dcf0cf289 100644
> --- a/include/drm/drm_gem_cma_helper.h
> +++ b/include/drm/drm_gem_cma_helper.h
> @@ -20,11 +20,8 @@ struct drm_gem_cma_object {
>  	void *vaddr;
>  };
> 
> -static inline struct drm_gem_cma_object *
> -to_drm_gem_cma_obj(struct drm_gem_object *gem_obj)
> -{
> -	return container_of(gem_obj, struct drm_gem_cma_object, base);
> -}
> +#define to_drm_gem_cma_obj(gem_obj) \
> +	container_of(gem_obj, struct drm_gem_cma_object, base)
> 
>  #ifndef CONFIG_MMU
>  #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS \