Thursday, October 04, 2007

hacks the data structure of kernel

#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
I was wondering above when I found list.h with kernel 2.4.Thank JeFF's help that kick confusion shit of macro out of my brain.Probaly you can look at code shit that was below.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

struct list_head;
struct pci_bus;

#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
/* (struct pci_bus *)((char*)pb2-(unsigned long)(&((struct pci_bus*)0)->next-bus)) ) */
#define pci_bus_b(n) list_entry(n, struct pci_bus, next_bus)

struct list_head {
struct list_head *next, *prev;
};

struct pci_bus
{
int a;
int b;
int c;
int bus_num;
struct list_head next_bus;
};

void main()
{
struct pci_bus a;
struct pci_bus b;

memset(&a, 0, sizeof(a));
memset(&b, 0, sizeof(b));
a.a=1;
a.b=2;
a.c=3;
a.bus_num=4;

b.a=10;
b.b=11;
b.c=12;
b.bus_num = 13;

a.next_bus.next = &(b.next_bus);
b.next_bus.prev = &(a.next_bus);

struct list_head *pb = a.next_bus.next;
struct pci_bus *p_busb = pci_bus_b(pb);
printf("bus num = %d\n", p_busb->bus_num);
printf("addr = %s\n",pb);

struct list_head *pb2 = a.next_bus.next;
struct pci_bus *p_busb2 = (struct pci_bus *)((char*)pb2 );
printf(" -2- bus2 num = %d\n", p_busb2->bus_num);

}

No comments: