2 * (c) 2011 Alexander Warg <warg@os.inf.tu-dresden.de>
3 * economic rights: Technische Universität Dresden (Germany)
5 * This file is part of TUD:OS and distributed under the terms of the
6 * GNU General Public License 2.
7 * Please see the COPYING-GPL-2 file for details.
17 Generic_device::alloc_child_resource(Resource *r, Device *cld)
19 bool found_as = false;
20 for (Resource_list::iterator br = resources()->begin();
21 br != resources()->end(); ++br)
32 if (!br->compatible(r, true))
37 if (br->provided()->alloc(*br, this, r, cld, parent() && !parent()->resource_allocated(*br)))
39 d_printf(DBG_ALL, "allocated resource: ");
47 if (!found_as && parent())
48 return parent()->alloc_child_resource(r, cld);
51 d_printf(DBG_ERR, "ERROR: could not reserve resource\n");
61 Device::request_resource(Resource *r)
65 // Are we the root device?
72 if (p->resource_allocated(r))
73 return; // already allocated
78 if (!p->request_child_resource(r, this))
81 printf("WARNING: could not request resource in parent resource\n");
88 Device::request_resources()
90 Resource_list const *rl = resources();
92 // Are we the root device?
96 for (Resource_list::iterator r = rl->begin();
104 Device::request_child_resources()
106 for (iterator dev = begin(0); dev != end(); ++dev)
108 // First, try to map all our resources of out child (dev) into
109 // provided resources of ourselves
110 (*dev)->request_resources();
112 // Second, recurse down to our child (dev)
113 (*dev)->request_child_resources();
118 // sortet set of resource, device pairs
127 Res_dev(Resource *r, Device *d) : r(r), d(d) {}
130 static bool res_cmp(Res_dev const &l, Res_dev const &r)
131 { return l.r->alignment() > r.r->alignment(); }
133 typedef std::multiset<Res_dev, bool (*)(Res_dev const &l, Res_dev const &r)> UAD;
138 Device::allocate_pending_resources()
140 allocate_pending_child_resources();
141 Device *p = parent();
146 UAD to_allocate(res_cmp);
148 for (Resource_list::iterator r = resources()->begin();
149 r != resources()->end(); ++r)
160 if (p->resource_allocated(*r))
163 printf("unallocated resource: ");
166 to_allocate.insert(Res_dev(*r, this));
169 for (UAD::const_iterator i = to_allocate.begin(); i != to_allocate.end(); ++i)
170 p->alloc_child_resource((*i).r, this);
174 Device::allocate_pending_child_resources()
176 UAD to_allocate(res_cmp);
178 for (iterator dev = begin(0); dev != end(); ++dev)
180 dev->allocate_pending_child_resources();
182 for (Resource_list::iterator r = dev->resources()->begin();
183 r != dev->resources()->end(); ++r)
194 if (resource_allocated(*r))
197 printf("unallocated resource: ");
200 to_allocate.insert(Res_dev(*r, *dev));
204 for (UAD::const_iterator i = to_allocate.begin(); i != to_allocate.end(); ++i)
205 alloc_child_resource((*i).r, (*i).d);
210 Generic_device::setup_resources()
212 for (iterator i = begin(0); i != end(); ++i)
213 i->setup_resources();
217 Generic_device::request_child_resource(Resource *r, Device *cld)
219 bool found_as = false;
223 // scan through all our resources and try to find a
224 // provided resource that is consumed by resource 'r'
225 for (Resource_list::iterator br = resources()->begin();
226 br != resources()->end(); ++br)
237 if (!br->compatible(r, exact))
242 if (br->provided()->request(*br, this, r, cld))
248 else if (!found_as && parent())
250 // If there is no proper resource provided by
251 // ourselves that fits resource r, than try to
252 // go up the hierarchy to our parent node
254 printf("try parent resource request...\n");
257 return parent()->request_child_resource(r, cld);