- 论坛徽章:
- 0
|
- struct pid
- {
- atomic_t count;
- unsigned int level;
- /* lists of tasks that use this pid */
- struct hlist_head tasks[PIDTYPE_MAX];
- struct rcu_head rcu;
- struct upid numbers[1];
- };
复制代码 numbers[1]形式上只有一个数组项,但实际上可以根据需要进行扩展。
在struct pid *alloc_pid(struct pid_namespace *ns)函数中分配一个
struct pid结构体pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
但是之后就直接通过i来使用numbers[],如下- struct pid *alloc_pid(struct pid_namespace *ns)
- {
- struct pid *pid;
- enum pid_type type;
- int i, nr;
- struct pid_namespace *tmp;
- struct upid *upid;
- pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
- if (!pid)
- goto out;
- tmp = ns;
- for (i = ns->level; i >= 0; i--) {
- nr = alloc_pidmap(tmp);
- if (nr < 0)
- goto out_free;
- pid->numbers[i].nr = nr;
- pid->numbers[i].ns = tmp;
- tmp = tmp->parent;
- }
- get_pid_ns(ns);
- pid->level = ns->level;
- atomic_set(&pid->count, 1);
- for (type = 0; type < PIDTYPE_MAX; ++type)
- INIT_HLIST_HEAD(&pid->tasks[type]);
- upid = pid->numbers + ns->level;
- spin_lock_irq(&pidmap_lock);
- for ( ; upid >= pid->numbers; --upid)
- hlist_add_head_rcu(&upid->pid_chain,
- &pid_hash[pid_hashfn(upid->nr, upid->ns)]);
- spin_unlock_irq(&pidmap_lock);
- out:
- return pid;
- out_free:
- while (++i <= ns->level)
- free_pidmap(pid->numbers + i);
- kmem_cache_free(ns->pid_cachep, pid);
- pid = NULL;
- goto out;
- }
复制代码- struct pid *find_pid_ns(int nr, struct pid_namespace *ns)
- {
- struct hlist_node *elem;
- struct upid *pnr;
- hlist_for_each_entry_rcu(pnr, elem, &pid_hash[pid_hashfn(nr, ns)], pid_chain)
- if (pnr->nr == nr && pnr->ns == ns)
- return container_of(pnr, struct pid,
- numbers[ns->level]);
- return NULL;
- }
复制代码 假设ns->level等于3,那么下面pid->numbers.nr中numbers引用的内存在哪里?
“numbers[1]形式上只有一个数组项,但实际上可以根据需要进行扩展”这句话怎么理解?
需要时进行扩展是怎么扩展的?
在函数
直接用container_of(pnr, struct pid,numbers[ns->level])来通过numbers[]中的upid来找到
upid相关的pid,这里假设ontainer_of(pnr, struct pid, numbers[ns->level]);中的ns->level等于3
,那么numbers[3]在哪里,numbers[]是怎么进行扩展的?
总之 ”numbers[1]形式上只有一个数组项,但实际上可以根据需要进行扩展“ 怎么个扩展法?
-----以上代码都摘自Linux源代码 |
|