1 资源定义 static struct resource res[] = { [0] = { .start = 1, .end = 5, }, [1] = { .start = 7 .end = 11, }, [2] = { .start = 15, .end = 30, }, }; request_mem_region(res[0]->start, 4, "res0"); request_mem_region(res[2]->start, 4, "res2"); request_mem_region(res[1]->start, 15,"res1"); 2 资源分布图 3 源码分析 static struct resource * __request_resource(struct resource *root, struct resource *new) { resource_size_t start = new->start; resource_size_t end = new->end; struct resource *tmp, **p; if (end < start) return root; if (start < root->start) return root; if (end > root->end) return root; p = &root->child; for (;;) { tmp = *p; if (!tmp || tmp->start > end) { new->sibling = tmp; //Insert new resource to struct. //如果节点为NULL或者新节点的地址范围大于sibing的地址范围 //插入该节点 *p = new; new->parent = root; return NULL; } p = &tmp->sibling; //Research next sibling. if (tmp->end < start) continue; return tmp; } } Author: Woodpecker Pecker.hu@gmail.com