linux内核动态数组,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_headlist_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 */

intsemid;/* 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_trefcnt;

spinlock_tlock;

struct list_headlist_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类型的指针本质上是一样的,

|

new[1]取的值是new+1,

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

|

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值