linux内核代码 sem.c 中结将结构体视为数组的有关问题

linux内核代码 sem.c 中结将结构体视为数组的问题
在Linux3.0.1的内核代码中:…/ipc/sem.c 的第1246行有如下代码:new->semadj = (short *) &new[1];
其中:
new 的定义为:struct sem_undo *new;
new 通过这条语句申请空间:new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
其中使用到一些结构体的定义如下:
in : …/ipc/sem.h
/* 
 * Each task has a list of undo requests. They are executed automatically
 * when the process exits.
 */
struct sem_undo {
struct list_head list_proc; /* per-process list: all undos from one process. */
/* rcu protected */
struct rcu_head rcu; /* rcu struct for sem_undo() */
struct sem_undo_list *ulp; /* sem_undo_list for the process */
struct list_head list_id; /* per semaphore array list: all undos for one array */
int semid; /* semaphore set identifier */
short * semadj; /* array of adjustments, one per semaphore */
};

/*
 * sem_undo_list controls shared access to the list of sem_undo structures
 * that may be shared among all a CLONE_SYSVSEM task group.
 */
struct sem_undo_list {
atomic_t refcnt;
spinlock_t lock;
struct list_head list_proc;
};

in : …/include/linux/types.h
struct list_head {
struct list_head *next, *prev;
};

in : …/include/linux/rcupdate.h
/*
 * struct rcu_head – callback structure for use with RCU
 * @next: next update requests in a list
 * @func: actual update function to call after the grace period.
 */
struct rcu_head {
struct rcu_head *next;
void (*func)(struct rcu_head *head);
};

in : …/include/linux/spinlock_types.h

typedef struct spinlock {
union {
struct raw_spinlock rlock;

#ifdef CONFIG_DEBUG_LOCK_ALLOC
# define LOCK_PADSIZE (offsetof(struct raw_spinlock, dep_map))
struct {
u8 __padding[LOCK_PADSIZE];
struct lockdep_map dep_map;
};
#endif
};
} spinlock_t;

  我想问的问题是:语句:new->semadj = (short *) &new[1]; 是什么意思,如何实现赋值的。在new->semadj = (short *) &new[1]; 中如何将 new 一个指向结构体的指针与数组联系起来的。
  语句:new->semadj = (short *) &new[1]; 在函数: static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid) 中使用到。
  还请知道的朋友告知。先谢谢了!


举个最简单的例子:
你定义一个指针:char *p;
你可以这样用:&p[1],
这里只不过是指向结构体的指针,和指向char类型的指针本质上是一样的,


不要说这个例子你不明白,如果真不明白,那就必需看一下c语言的书了。

探讨

举个最简单的例子:
你定义一个指针:char *p;
你可以这样用:&p[1],
这里只不过是指向结构体的指针,和指向char类型的指针本质上是一样的,


new[1]取的值是new+1,
执行过后new->semadj的值是new[1]的地址。

linux内核代码 sem.c 中结将结构体视为数组的有关问题

相关文章:

你感兴趣的文章:

标签云: