[PATCH v2 1/7] drm/xe: Introduce const cast helper

Michal Wajdeczko michal.wajdeczko at intel.com
Fri Jul 19 16:55:43 UTC 2024



On 19.07.2024 18:32, Lucas De Marchi wrote:
> On Wed, Jul 17, 2024 at 09:51:49PM GMT, Michal Wajdeczko wrote:
>> Typically we want to preserve pointer constness when converting
>> from one xe pointer to another, but in some rare cases, like kunit
>> parameter conversions, we might want to discard this constness.
>> Add a helper that we will use to clearly indicate our intention.
>>
>> Signed-off-by: Michal Wajdeczko <michal.wajdeczko at intel.com>
>> ---
>> drivers/gpu/drm/xe/xe_device.h | 5 +++++
>> 1 file changed, 5 insertions(+)
>>
>> diff --git a/drivers/gpu/drm/xe/xe_device.h
>> b/drivers/gpu/drm/xe/xe_device.h
>> index 0a2a3e7fd402..c2b1f9f066bd 100644
>> --- a/drivers/gpu/drm/xe/xe_device.h
>> +++ b/drivers/gpu/drm/xe/xe_device.h
>> @@ -20,6 +20,11 @@ static inline struct xe_device
>> *pdev_to_xe_device(struct pci_dev *pdev)
>>     return pci_get_drvdata(pdev);
>> }
>>
>> +static inline struct xe_device *xe_device_const_cast(const struct
>> xe_device *xe)
>> +{
>> +    return pdev_to_xe_device(to_pci_dev(xe->drm.dev));
> 
> why are you going through pdev_to_xe_device() rather than doing what the
> function name says and just remove the const?

IIRC this was my initial way how to get rid of the const without doing a
forced cast and it was before I decided to introduce a helper for it and
before this helper was named this way ;)

and while explicit cast is more direct (and I can still do that way in
next rev if preferred), then it will be just pure C language concept,
while the potential advantage of doing that through pdev_to_xe_device()
is that it shows that this non-const xe pointer could be still obtained
indirectly, so it should be safe to do so

> 
> Lucas De Marchi
> 
>> +}
>> +
>> static inline struct xe_device *ttm_to_xe_device(struct ttm_device *ttm)
>> {
>>     return container_of(ttm, struct xe_device, ttm);
>> -- 
>> 2.43.0
>>


More information about the Intel-xe mailing list