kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: ruben@mrbrklyn.com (Ruben Safir)
To: kernelnewbies@lists.kernelnewbies.org
Subject: Kernel thread scheduling
Date: Sun, 12 Apr 2015 00:53:05 -0400	[thread overview]
Message-ID: <5529FA31.1080108@mrbrklyn.com> (raw)
In-Reply-To: <5529F181.2090300@gmail.com>

On 04/12/2015 12:16 AM, nick wrote:
> This finds the next node in the red black tree for  sched_enities.
> Basically rb_next finds the next node in the tree. The argument is 
> the rb_node structure embedded in the structure using a red black
> tree.


The problem is this


struct mystruc {
	int a;
	char * string;
} wonderful;

char * string2 = wonderful.string;



There is no way to know that string2 comes from wonderful.
That is the problem I have with the nodes

The node is an instance of sched_entity.  But unless there is a field
that says what task_entity it is embedded in, in of itself, there is no
way to know.  There is no 'this' C.

Obviously if does know.  I'll look at chapter 6

I'm also looking at this



static struct sched_entity *__pick_next_entity(struct sched_entity *se)
{
	struct rb_node *next = rb_next(&se->run_node);

	if (!next)
		return NULL;

	return rb_entry(next, struct sched_entity, run_node); <<==macro
}



The macro is:  I think:
http://lxr.free-electrons.com/source/include/linux/rbtree.h#L50

50 #define rb_entry(ptr, type, member) container_of(ptr, type, member)

container_of is
http://lxr.free-electrons.com/source/include/linux/kernel.h#L798


791 /**
792  * container_of - cast a member of a structure out to the containing
structure
793  * @ptr:        the pointer to the member.
794  * @type:       the type of the container struct this is embedded in.
795  * @member:     the name of the member within the struct.
796  *
797  */
798 #define container_of(ptr, type, member) ({                      \
799         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
800         (type *)( (char *)__mptr - offsetof(type,member) );})


Now I have embedded macros which I thought you couldn't even do...

In trying to understand this I stumbled on

http://linuxkernel51.blogspot.com/2011/02/how-containerof-macro-works-example.html
 How "container_of" macro works, & an Example
Here iam giving a small code snippet that gives and idea about working
of "container_of", this posed me little difficulty in understanding,
after google-ing i got some examples and after working on that i wrote a
simple C application that depicts its working. here i have defined two
macros "offsetof" and "container_of" which i have extracted from
"kernel.h" header.
       Please interpret this code and try some trick to understand
"container_of".

container_of macro is defined in linux/kernel.h

syntax: container_of( pointer, container_type, container_field );

This macro takes a pointer to a filed name container_field, within a
structure of type container_type, and returns a pointer to the
containing structure .

simply this is a convenience macro that may be used to obtain a pointer
to a structure from a pointer to some other structure contained with in it.


Code :

#include <stdio.h>
#include <stdlib.h>

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

#define container_of(ptr, type, member) ({            \
 const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
 (type *)( (char *)__mptr - offsetof(type,member) );})

struct test1 {
 int a;
};

struct test2 {
 int b;
 struct test1 z;
 int c;
};

int main()
{
 /* existing structure */
 struct test2 *obj;
 obj = malloc(sizeof(struct test2));
 if(obj == NULL){
       printf("Error: Memory not allocated...!\n");
 }
 obj->z.a = 51;
 obj->b = 43;
 obj->c = 53;

 /* pointer to existing entry */
 struct test1 *obj1 = &obj->z; //both of type test1

 struct test2 *obj2 = container_of(obj1, struct test2, z);

 printf("obj2->b = %d\n", obj2->b); ///notsure what this does,need a nap

 return EXIT_SUCCESS;
}

  reply	other threads:[~2015-04-12  4:53 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-20 23:19 Kernel thread scheduling Vincenzo Scotti
2015-03-20 23:27 ` Jeff Haran
2015-03-21  6:33   ` Anand Moon
2015-03-22 23:14     ` Vincenzo Scotti
2015-03-22 23:30       ` nick
2015-03-23  0:05         ` Ruben Safir
2015-03-23  0:35           ` nick
2015-04-10  1:51             ` Ruben Safir
     [not found]               ` <55272EA8.7010908@gmail.com>
2015-04-10  2:12                 ` Ruben Safir
2015-04-10  2:52                   ` nick
2015-04-10  3:37                     ` Ruben Safir
     [not found]                       ` <5527CB72.1000401@gmail.com>
2015-04-12  2:21                         ` Ruben Safir
2015-04-12  3:02                           ` Ruben Safir
2015-04-12  4:16                             ` nick
2015-04-12  4:53                               ` Ruben Safir [this message]
     [not found]                               ` <A2417C6E7F04A0438F09C31B33A6BE8B01D9CE3BE7@B-EXH-MBX2.liunet.edu>
2015-04-12  5:06                                 ` Ruben Safir
2015-04-13  3:21                                   ` nick
2015-04-17 13:10                                 ` Ruben Safir
2015-04-17 13:14                                 ` Ruben Safir
2015-04-16 14:56                     ` Ruben Safir
2015-04-16 15:07                       ` Ricardo Ribalda Delgado
2015-04-16 15:11                         ` Ruben Safir
2015-04-16 15:12                         ` Ruben Safir
2015-04-16 15:51                           ` Ricardo Ribalda Delgado
2015-04-16 15:10                       ` Aruna Hewapathirane
2015-04-16 15:37                         ` Ruben Safir
2015-04-16 15:11                       ` Mark P
2015-04-16 16:31                         ` Jeff Haran
2015-04-16 17:08                           ` Ruben Safir
2015-04-16 17:34                             ` Jeff Haran
2015-04-16 18:28                               ` Ruben Safir
2015-04-16 18:47                                 ` Valdis.Kletnieks at vt.edu
2015-04-16 21:41                                   ` Jeff Haran
2015-04-17  7:45                                     ` Silvan Jegen
2015-04-17  8:50                                     ` Ruben Safir
2015-04-16 23:05                                   ` Ruben Safir
2015-04-16 18:32                       ` John de la Garza
2015-04-16 18:38                         ` Ruben Safir
2015-04-16 18:42                         ` Ruben Safir
2015-04-16 19:43                           ` Silvan Jegen

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=5529FA31.1080108@mrbrklyn.com \
    --to=ruben@mrbrklyn.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /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;
as well as URLs for NNTP newsgroup(s).