All of lore.kernel.org
 help / color / mirror / Atom feed
* Function Pointer Question
@ 2015-04-02  4:16 Nicholas Krause
  2015-04-02 10:15 ` Malte Vesper
  0 siblings, 1 reply; 9+ messages in thread
From: Nicholas Krause @ 2015-04-02  4:16 UTC (permalink / raw)
  To: kernelnewbies

Greetings All, 
I'm a little rough with how function pointers work in C. I assumed there similar to way C++ stores virtual functions internally in  the vtable for virtual functions  by the compiler and checks the table at runtime. Please let me know if I'm wrong in my understanding. 
Thanks, 
Nick 

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Function Pointer Question
  2015-04-02  4:16 Function Pointer Question Nicholas Krause
@ 2015-04-02 10:15 ` Malte Vesper
  2015-04-02 10:54   ` Nicholas Krause
  0 siblings, 1 reply; 9+ messages in thread
From: Malte Vesper @ 2015-04-02 10:15 UTC (permalink / raw)
  To: kernelnewbies

Virtual functions are implemented on on the base of vtables, which in 
turn are implemented using function pointers.
So it would be good to get a grasp on functionpointers (they exist in 
C++ as well), also I believe this is not a C mailing list, take a look 
at stackoverflow or a c/c++ site or book of your choice.
If you know how a pointer works, you will find it easy to understand how 
a functionpointer works.

On 02/04/15 05:16, Nicholas Krause wrote:
> Greetings All,
> I'm a little rough with how function pointers work in C. I assumed there similar to way C++ stores virtual functions internally in  the vtable for virtual functions  by the compiler and checks the table at runtime. Please let me know if I'm wrong in my understanding.
> Thanks,
> Nick
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Function Pointer Question
  2015-04-02 10:15 ` Malte Vesper
@ 2015-04-02 10:54   ` Nicholas Krause
  2015-04-02 11:00     ` Malte Vesper
                       ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Nicholas Krause @ 2015-04-02 10:54 UTC (permalink / raw)
  To: kernelnewbies



On April 2, 2015 6:15:19 AM EDT, Malte Vesper <malte.vesper@postgrad.manchester.ac.uk> wrote:
>Virtual functions are implemented on on the base of vtables, which in 
>turn are implemented using function pointers.
>So it would be good to get a grasp on functionpointers (they exist in 
>C++ as well), also I believe this is not a C mailing list, take a look 
>at stackoverflow or a c/c++ site or book of your choice.
>If you know how a pointer works, you will find it easy to understand
>how 
>a functionpointer works.
>
>On 02/04/15 05:16, Nicholas Krause wrote:
>> Greetings All,
>> I'm a little rough with how function pointers work in C. I assumed
>there similar to way C++ stores virtual functions internally in  the
>vtable for virtual functions  by the compiler and checks the table at
>runtime. Please let me know if I'm wrong in my understanding.
>> Thanks,
>> Nick
>>
I looked in to it and the kernel seems to be one of the few places where this is done along with in line functions.  Why do we need function pointers in the kernel, outside of device drivers is my real question and is there any way to do the code using them without function pointers at all, I am assuming no. 
Thanks, 
Nick 
-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Function Pointer Question
  2015-04-02 10:54   ` Nicholas Krause
@ 2015-04-02 11:00     ` Malte Vesper
  2015-04-02 11:27     ` Sudip Mukherjee
  2015-04-02 13:13     ` Valdis.Kletnieks at vt.edu
  2 siblings, 0 replies; 9+ messages in thread
From: Malte Vesper @ 2015-04-02 11:00 UTC (permalink / raw)
  To: kernelnewbies

Since I am not sure what you are refering to exactly it is hard to 
answer. Refer to an example maybe? Also take a look at i.e. std::sort / 
the compare type (you seem to be a c++ guy). Why do we need a pointer 
there (actually function object, slight generalization of a funciton 
pointer)?

Why would it be helpful to define your on method to be called on wakeup 
(one of the places where I know fp's are used)?

On 02/04/15 11:54, Nicholas Krause wrote:
>
> On April 2, 2015 6:15:19 AM EDT, Malte Vesper <malte.vesper@postgrad.manchester.ac.uk> wrote:
>> Virtual functions are implemented on on the base of vtables, which in
>> turn are implemented using function pointers.
>> So it would be good to get a grasp on functionpointers (they exist in
>> C++ as well), also I believe this is not a C mailing list, take a look
>> at stackoverflow or a c/c++ site or book of your choice.
>> If you know how a pointer works, you will find it easy to understand
>> how
>> a functionpointer works.
>>
>> On 02/04/15 05:16, Nicholas Krause wrote:
>>> Greetings All,
>>> I'm a little rough with how function pointers work in C. I assumed
>> there similar to way C++ stores virtual functions internally in  the
>> vtable for virtual functions  by the compiler and checks the table at
>> runtime. Please let me know if I'm wrong in my understanding.
>>> Thanks,
>>> Nick
>>>
> I looked in to it and the kernel seems to be one of the few places where this is done along with in line functions.  Why do we need function pointers in the kernel, outside of device drivers is my real question and is there any way to do the code using them without function pointers at all, I am assuming no.
> Thanks,
> Nick

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Function Pointer Question
  2015-04-02 10:54   ` Nicholas Krause
  2015-04-02 11:00     ` Malte Vesper
@ 2015-04-02 11:27     ` Sudip Mukherjee
  2015-04-02 11:52       ` Malte Vesper
  2015-04-02 13:13     ` Valdis.Kletnieks at vt.edu
  2 siblings, 1 reply; 9+ messages in thread
From: Sudip Mukherjee @ 2015-04-02 11:27 UTC (permalink / raw)
  To: kernelnewbies

> I looked in to it and the kernel seems to be one of the few places where this is done along with in line functions.  Why do we need function pointers in the kernel, outside of device drivers is my real question and is there any way to do the code using them without function pointers at all, I am assuming no.

Suppose you are registering a pci driver, and you are telling the pci
layer that function xyz()  is your probe function. incidentally, there
is also another driver which is also having a probe function called
xyz(). if you do not give the pointer to your function and pci layer
starts calling the functions by its name , then which xyz() should be
executed now???

regards
sudip

> Thanks,
> Nick

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Function Pointer Question
  2015-04-02 11:27     ` Sudip Mukherjee
@ 2015-04-02 11:52       ` Malte Vesper
  2015-04-02 12:25         ` Sudip Mukherjee
  0 siblings, 1 reply; 9+ messages in thread
From: Malte Vesper @ 2015-04-02 11:52 UTC (permalink / raw)
  To: kernelnewbies

On 02/04/15 12:27, Sudip Mukherjee wrote:
>> I looked in to it and the kernel seems to be one of the few places where this is done along with in line functions.  Why do we need function pointers in the kernel, outside of device drivers is my real question and is there any way to do the code using them without function pointers at all, I am assuming no.
> Suppose you are registering a pci driver, and you are telling the pci
> layer that function xyz()  is your probe function. incidentally, there
> is also another driver which is also having a probe function called
> xyz(). if you do not give the pointer to your function and pci layer
> starts calling the functions by its name , then which xyz() should be
> executed now???
>
> regards
> sudip
>
>> Thanks,
>> Nick
That would not compile for the very reason of name conflict, if the 
functions would be in the same namespace...
But the question also read "Why do we need function pointers in the 
kernel, outside of device drivers is "

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Function Pointer Question
  2015-04-02 11:52       ` Malte Vesper
@ 2015-04-02 12:25         ` Sudip Mukherjee
  0 siblings, 0 replies; 9+ messages in thread
From: Sudip Mukherjee @ 2015-04-02 12:25 UTC (permalink / raw)
  To: kernelnewbies

> But the question also read "Why do we need function pointers in the kernel,
> outside of device drivers is "

then, can "workqueues" be an ideal example of his question ?

regards
sudip

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Function Pointer Question
  2015-04-02 10:54   ` Nicholas Krause
  2015-04-02 11:00     ` Malte Vesper
  2015-04-02 11:27     ` Sudip Mukherjee
@ 2015-04-02 13:13     ` Valdis.Kletnieks at vt.edu
  2015-04-02 14:15       ` Nicholas Krause
  2 siblings, 1 reply; 9+ messages in thread
From: Valdis.Kletnieks at vt.edu @ 2015-04-02 13:13 UTC (permalink / raw)
  To: kernelnewbies

On Thu, 02 Apr 2015 06:54:42 -0400, Nicholas Krause said:

> I looked in to it and the kernel seems to be one of the few places where this
> is done along with in line functions.  Why do we need function pointers in the
> kernel, outside of device drivers is my real question and is there any way to
> do the code using them without function pointers at all, I am assuming no.

Geez Nick, you didn't look very hard at all, did you?

[/usr/src/linux-next] find include -type f | xargs egrep '^struct .*_op.*{$' | sort

There's 391 of them (at least in yesterday's linux-next tree).

You should *at least* know and understand 'struct file_operations',
'struct dentry_operations', 'struct inode_operations', and 'struct super_operations"
if you plan to have any realistic hope of understanding the VFS - which should
be important to you, as I seem to recall you were interested in btrfs2.

See include/linux/fs.h for the details.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150402/2d5accad/attachment.bin 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Function Pointer Question
  2015-04-02 13:13     ` Valdis.Kletnieks at vt.edu
@ 2015-04-02 14:15       ` Nicholas Krause
  0 siblings, 0 replies; 9+ messages in thread
From: Nicholas Krause @ 2015-04-02 14:15 UTC (permalink / raw)
  To: kernelnewbies



On April 2, 2015 9:13:38 AM EDT, Valdis.Kletnieks at vt.edu wrote:
>On Thu, 02 Apr 2015 06:54:42 -0400, Nicholas Krause said:
>
>> I looked in to it and the kernel seems to be one of the few places
>where this
>> is done along with in line functions.  Why do we need function
>pointers in the
>> kernel, outside of device drivers is my real question and is there
>any way to
>> do the code using them without function pointers at all, I am
>assuming no.
>
>Geez Nick, you didn't look very hard at all, did you?
>
>[/usr/src/linux-next] find include -type f | xargs egrep '^struct
>.*_op.*{$' | sort
>
>There's 391 of them (at least in yesterday's linux-next tree).
>
>You should *at least* know and understand 'struct file_operations',
>'struct dentry_operations', 'struct inode_operations', and 'struct
>super_operations"
>if you plan to have any realistic hope of understanding the VFS - which
>should
>be important to you, as I seem to recall you were interested in btrfs2.
>
>See include/linux/fs.h for the details.
I do know about those too, however I was just curious about areas that the kernel does this I known the vfs and drivers do this. 
Thanks, 
Nick 

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2015-04-02 14:15 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-02  4:16 Function Pointer Question Nicholas Krause
2015-04-02 10:15 ` Malte Vesper
2015-04-02 10:54   ` Nicholas Krause
2015-04-02 11:00     ` Malte Vesper
2015-04-02 11:27     ` Sudip Mukherjee
2015-04-02 11:52       ` Malte Vesper
2015-04-02 12:25         ` Sudip Mukherjee
2015-04-02 13:13     ` Valdis.Kletnieks at vt.edu
2015-04-02 14:15       ` Nicholas Krause

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.