Outreachy Linux Kernel Community
 help / color / mirror / Atom feed
From: "Fabio M. De Francesco" <fmdefrancesco@gmail.com>
To: Stefano Brivio <sbrivio@redhat.com>,
	jhpark1013@gmail.com, Jaehee Park <jhpark1013@gmail.com>
Cc: outreachy@lists.linux.dev
Subject: Re: patch idea for wfx
Date: Wed, 06 Apr 2022 15:54:35 +0200	[thread overview]
Message-ID: <4705777.GXAFRqVoOG@leap> (raw)
In-Reply-To: <20220406042821.GA989392@jaehee-ThinkPad-X1-Extreme>

On mercoledì 6 aprile 2022 06:28:21 CEST Jaehee Park wrote:
> -----Section 2-----
> Here's my diff (below). I've implemented container_of() in function wfx_add_interface (to start).
> - If I'm understanding this correctly, we're replacing the line
> `struct wfx_vif *wvif = (struct wfx_vif *)vif->drv_priv`
> and replacing it with
> `struct wfx_vif *wvif = container_of(vif, struct ieee80211_vif, drv_priv)`
> 
> This is the error message I get:
> ./include/linux/container_of.h:17:41: error: initialization of ‘struct wfx_vif *’ from incompatible pointer type ‘struct ieee80211_vif *’ [-Werror=incompatible-pointer-types]

Hi Jaehee,

Probably, before changing any code which could be improved with the use of 
container_of(), you'd better see how this macro is used in other code that 
works properly.

Look at the following example. In drivers/dma/tegra20-apb-dma.c, we can look 
at a one line function called "to_tegra_dma_chan()":

static inline struct tegra_dma_channel *to_tegra_dma_chan(struct dma_chan *dc)
{
        return container_of(dc, struct tegra_dma_channel, dma_chan);
}

"to_tegra_dma_chan()" takes a pointer to a "struct dma_chan" that is called 
"dc" and it returns a pointer to the "struct tegra_dma_channel" which embeds 
the above-mentioned "struct dma_chan". 

Please take a look at the definition of "struct tegra_dma_channel". You'll 
notice that it has a field of type "dma_chan" that is called "dma_chan" 
(coincidentally the names of field and type are the same; this could confuse 
you but you easily see that they are different entities):

struct tegra_dma_channel {
        struct dma_chan dma_chan;
        ...
}

How can "to_tegra_dma_chan()" return a pointer to the embedding "struct 
tegra_dma_channel"?

It uses cointainer_of(). The first argument of that macro is the pointer 
passed to the function ("struct dma_chan *dc"). The second argument is 
the embedding "struct tegra_dma_channel". The third is the name of the 
field (not the type) pointed by "dc".

I hope it helps.

Fabio



  reply	other threads:[~2022-04-06 13:54 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-31 14:14 patch idea for wfx Jaehee Park
2022-03-31 17:40 ` Fabio M. De Francesco
2022-03-31 17:52 ` Stefano Brivio
2022-03-31 21:47   ` Jaehee Park
2022-03-31 22:12     ` Stefano Brivio
2022-04-06  4:28       ` Jaehee Park
2022-04-06 13:54         ` Fabio M. De Francesco [this message]
2022-04-06 14:24           ` Jaehee Park
2022-04-06 16:04             ` Fabio M. De Francesco
2022-04-07  1:57               ` Jaehee Park
2022-04-06 15:41         ` Stefano Brivio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4705777.GXAFRqVoOG@leap \
    --to=fmdefrancesco@gmail.com \
    --cc=jhpark1013@gmail.com \
    --cc=outreachy@lists.linux.dev \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox