【转帖】Linux对I/O端口资源的管理 (1)
来源:互联网
作者:west263.com
时间:2008-04-16
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
3.2.5 分配接口allocate_resource()
在find_resource()函数的基础上,函数allocate_resource()实现:在一颗资源树中分配一条指定大小的、且包含在指定区域[min,max]中的、未使用资源区域。其源代码如下:
/*
* Allocate empty slot in the resource tree given range and alignment.
*/
int allocate_resource(struct resource *root, struct resource *new,
unsigned long size,
unsigned long min, unsigned long max,
unsigned long align,
void (*alignf)(void *, struct resource *, unsigned long),
void *alignf_data)
{
int err;
write_lock(&resource_lock);
err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
if (err >= 0 && __request_resource(root, new))
err = -EBUSY;
write_unlock(&resource_lock);
return err;
}
3.2.6 获取资源的名称列表
函数get_resource_list()用于获取根节点root的子资源名字列表。该函数主要用来支持/proc/文档系统(比如实现proc/ioports文档和/proc/iomem文档)。其源代码如下:
int get_resource_list(struct resource *root, char *buf, int size)
{
char *fmt;
int retval;
fmt = lx-lx : %s
;
if (root->end < 0x10000)
fmt = lx-lx : %s
;
read_lock(&resource_lock);
retval = do_resource_list(root->child, fmt, 8, buf, buf size) - buf;
read_unlock(&resource_lock);
return retval;
}
能够看出,该函数主要通过调用内部静态函数do_resource_list()来实现其功能,其源代码如下:
/*
* This generates reports for /proc/ioports and /proc/iomem
*/
static char * do_resource_list(struct resource *entry, const char *fmt,
int offset, char *buf, char *end)
{
if (offset < 0)
offset = 0;
while (entry) {
const char *name = entry->name;
unsigned long from, to;
if ((int) (end-buf) < 80)
return buf;
from = entry->start;
to = entry->end;
if (!name)
name = ;
buf = sprintf(buf, fmt offset, from, to, name);
if (entry->child)
buf = do_resource_list(entry->child, fmt, offset-2, buf, end);
entry = entry->sibling;
}
return buf;
}
函数do_resource_list()主要通过一个while{}循环连同递归嵌套调用来实现,较为简单,这里就不在周详解释了。
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



