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;

            }

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{}循环连同递归嵌套调用来实现,较为简单,这里就不在周详解释了。

管理I/O Region资源

Linux将基于I/O映射方式的I/O端口和基于内存映射方式的I/O端口资源统称为“I/O区域”(I/O Region)。I/O Region仍然是一种I/O资源,因此他仍然能够用resource结构类型来描述。下面我们就来看看Linux是如何管理I/O Region的。

1 I/O Region的分配

在函数__request_resource()的基础上,Linux实现了用于分配I/O区域的函数__request_region(),如下:

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!