Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C Programming - usage example for container_of in functions

 
Thread Tools Search this Thread
Old 11-07-2009, 02:14 AM   #1
Default usage example for container_of in functions


Yo, me again ^_^

say I have a structure of this type

typedef struct{
int data;
int *(*foo)(void *obj1, void *obj2);
}mystruct;

I'd like to know if and how I can use the function "container_of"
within foo, to track back the pointer of the allocated "mystruct"
instance.

Thanks,
R


InuY4sha
  Reply With Quote
Old 11-07-2009, 02:49 AM   #2
Keith Thompson
 
Posts: n/a
Default Re: usage example for container_of in functions
InuY4sha <> writes:
> say I have a structure of this type
>
> typedef struct{
> int data;
> int *(*foo)(void *obj1, void *obj2);
> }mystruct;
>
> I'd like to know if and how I can use the function "container_of"
> within foo, to track back the pointer of the allocated "mystruct"
> instance.


What is "container_of"?

[... google ...]

Ah, I see, it's a macro defined in the Linux kernel. You might want
to ask in a Linux-specific newsgroup.

I doubt that what you're trying to do is possible. Given a mystruct
object "obj", and given a pointer to the member "obj.foo" of that
object, you could compute a pointer to "obj". But within the function
to which obj.foo points, you don't have a pointer to foo.obj (unless
you pass it as an argument).

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


Keith Thompson
  Reply With Quote
Old 11-07-2009, 04:15 AM   #3
Seebs
 
Posts: n/a
Default Re: usage example for container_of in functions
On 2009-11-07, InuY4sha <> wrote:
> Yo, me again ^_^
>
> say I have a structure of this type
>
> typedef struct{
> int data;
> int *(*foo)(void *obj1, void *obj2);
> }mystruct;
>
> I'd like to know if and how I can use the function "container_of"
> within foo, to track back the pointer of the allocated "mystruct"
> instance.


You can't, because you'd need to use it, not on the function pointed to
by the member foo, but on the member foo. Which you can't.

Likely workaround: Define the function with another argument, representing
the struct:

typedef struct mystruct {
int data;
int *(*foo)(void *obj1, void *obj2, struct mystruct *me);
} mystruct;
#define STRUCT_CALL(s, m, a, b) ((s)->(m)(a, b, (s)))

struct mystruct *blah;
STRUCT_CALL(blah, foo, &o1, &o2);

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


Seebs
  Reply With Quote
Old 11-07-2009, 04:17 AM   #4
Seebs
 
Posts: n/a
Default Re: usage example for container_of in functions
On 2009-11-07, Keith Thompson <kst-> wrote:
> Ah, I see, it's a macro defined in the Linux kernel. You might want
> to ask in a Linux-specific newsgroup.


You might think, but he's not actually asking about the Linux kernel per
se, but about a way to have embedded objects within structs. Think of it
used in a context like:

struct list { struct list *next; }

struct foo {
int i;
struct list l;
unsigned char *data;
};

You can iterate over a series of lists, then take the list and get a pointer
to the containing struct foo.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


Seebs
  Reply With Quote
Old 11-07-2009, 08:08 AM   #5
InuY4sha
 
Posts: n/a
Default Re: usage example for container_of in functions
On 7 Nov, 05:15, Seebs <usenet-nos...@seebs.net> wrote:
> On 2009-11-07, InuY4sha <inuy4...@gmail.com> wrote:
>
> > Yo, me again ^_^

>
> > say I have a structure of this type

>
> > typedef struct{
> > * * *int data;
> > * * *int *(*foo)(void *obj1, void *obj2);
> > }mystruct;

>
> > I'd like to know if and how I can use the function "container_of"
> > within foo, to track back the pointer of *the allocated "mystruct"
> > instance.

>
> You can't, because you'd need to use it, not on the function pointed to
> by the member foo, but on the member foo. *Which you can't.
>
> Likely workaround: *Define the function with another argument, representing
> the struct:
>
> typedef struct mystruct {
> * int data;
> * int *(*foo)(void *obj1, void *obj2, struct mystruct *me);} mystruct;
>
> #define STRUCT_CALL(s, m, a, b) ((s)->(m)(a, b, (s)))
>
> struct mystruct *blah;
> STRUCT_CALL(blah, foo, &o1, &o2);
>
> -s
> --
> Copyright 2009, all wrongs reversed. *Peter Seebach / usenet-nos...@seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


Yes I also thought to that workaround.. I just wanted to know if there
would be a cleaner / more transparent way to have some kind of "c++"
class polimorphysm where I can have a same function that adapt to the
context within which it's used...


InuY4sha
  Reply With Quote
Old 11-07-2009, 09:45 AM   #6
Seebs
 
Posts: n/a
Default Re: usage example for container_of in functions
On 2009-11-07, InuY4sha <> wrote:
> Yes I also thought to that workaround.. I just wanted to know if there
> would be a cleaner / more transparent way to have some kind of "c++"
> class polimorphysm where I can have a same function that adapt to the
> context within which it's used...


Not really. You can sorta fake it up, but somewhere in there, you'd
probably be better off either switching to a design that fits C, or switching
to one of the OO C-like languages. (Of them, I like Objective-C best,
Java next best, and C++ least.)

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


Seebs
  Reply With Quote
Old 11-07-2009, 09:52 AM   #7
Nick
 
Posts: n/a
Default Re: usage example for container_of in functions
InuY4sha <> writes:

> On 7 Nov, 05:15, Seebs <usenet-nos...@seebs.net> wrote:
>> On 2009-11-07, InuY4sha <inuy4...@gmail.com> wrote:
>>
>> > Yo, me again ^_^

>>
>> > say I have a structure of this type

>>
>> > typedef struct{
>> > Â* Â* Â*int data;
>> > Â* Â* Â*int *(*foo)(void *obj1, void *obj2);
>> > }mystruct;

>>
>> > I'd like to know if and how I can use the function "container_of"
>> > within foo, to track back the pointer of Â*the allocated "mystruct"
>> > instance.

>>
>> You can't, because you'd need to use it, not on the function pointed to
>> by the member foo, but on the member foo. Â*Which you can't.
>>
>> Likely workaround: Â*Define the function with another argument, representing
>> the struct:
>>
>> typedef struct mystruct {
>> Â* int data;
>> Â* int *(*foo)(void *obj1, void *obj2, struct mystruct *me);} mystruct;
>>
>> #define STRUCT_CALL(s, m, a, b) ((s)->(m)(a, b, (s)))
>>
>> struct mystruct *blah;
>> STRUCT_CALL(blah, foo, &o1, &o2);

>
> Yes I also thought to that workaround.. I just wanted to know if there
> would be a cleaner / more transparent way to have some kind of "c++"
> class polimorphysm where I can have a same function that adapt to the
> context within which it's used...


The problem as I see it (having been pointed at something very similar
to this yesterday) is that in your case by the time you're in the
function you have no knowledge at all of how you got there. If you
could guarantee that any of the parameters were any part of the
structure (so, say, obj1 was always a pointer to the int data) then you
could mess around with offsetof (which is all that container_of does) to
get back to the top of the structure. But you need to actually have the
structure in some way - there's no way I know (standard or not) to
distinguish between these cases (which is what I think you're trying
to do):

foo(datap1,datap2);
mystruct->foo(datap1,datap2);

After all, somewhere you actually have defined foo - it's not in the
structure - and in both cases it's that same actual code, at the same
actual address, that's being called.
--
Online waterways route planner: http://canalplan.org.uk
or : http://canalplan.eu


Nick
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Give you enough string functions in Java web reporting tool freezea Software 0 10-08-2009 09:03 AM
Need Network Help =?Utf-8?B?c3QwY2sxbmc=?= Wireless Networking 4 09-13-2007 08:31 AM
increase in cpu usage on locking and locking the system sowmya.rangineni@gmail.com Computer Support 4 06-15-2007 11:53 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46