2 * Copyright (c) 2000 by The XFree86 Project, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Except as contained in this notice, the name of the copyright holder(s)
23 * and author(s) shall not be used in advertising or otherwise to promote
24 * the sale, use or other dealings in this Software without prior written
25 * authorization from the copyright holder(s) and author(s).
28 /* Maybe this file belongs elsewhere? */
30 #define LOADERDECLARATIONS
31 #ifdef HAVE_XORG_CONFIG_H
32 #include <xorg-config.h>
35 #include "loaderProcs.h"
40 * This should be static, but miinitext wants it. FIXME: make extension
41 * initialization not completely terrible.
43 ExtensionModule *ExtensionModuleList = NULL;
44 static int numExtensionModules = 0;
46 static ExtensionModule *
47 NewExtensionModule(void)
49 ExtensionModule *save = ExtensionModuleList;
53 if (!ExtensionModuleList)
54 numExtensionModules = 0;
56 n = numExtensionModules + 1;
57 ExtensionModuleList = realloc(ExtensionModuleList,
58 (n + 1) * sizeof(ExtensionModule));
59 if (ExtensionModuleList == NULL) {
60 ExtensionModuleList = save;
63 numExtensionModules++;
64 ExtensionModuleList[numExtensionModules].name = NULL;
65 return ExtensionModuleList + (numExtensionModules - 1);
70 LoadExtension(ExtensionModule * e, Bool builtin)
72 ExtensionModule *newext;
74 if (e == NULL || e->name == NULL)
77 if (!(newext = NewExtensionModule()))
81 xf86MsgVerb(X_INFO, 2, "Initializing built-in extension %s\n",
84 xf86MsgVerb(X_INFO, 2, "Loading extension %s\n", e->name);
86 newext->name = e->name;
87 newext->initFunc = e->initFunc;
88 newext->disablePtr = e->disablePtr;
89 newext->setupFunc = e->setupFunc;
90 newext->initDependencies = e->initDependencies;
92 if (e->setupFunc != NULL)
97 * Sort ExtensionModuleList according to the initialisation order
98 * dependencies. The code for this is taken from BSD's tsort,
99 * and carries the following copyright/license:
102 * Copyright (c) 1989, 1993, 1994
103 * The Regents of the University of California. All rights reserved.
105 * This code is derived from software contributed to Berkeley by
106 * Michael Rendell of Memorial University of Newfoundland.
108 * Redistribution and use in source and binary forms, with or without
109 * modification, are permitted provided that the following conditions
111 * 1. Redistributions of source code must retain the above copyright
112 * notice, this list of conditions and the following disclaimer.
113 * 2. Redistributions in binary form must reproduce the above copyright
114 * notice, this list of conditions and the following disclaimer in the
115 * documentation and/or other materials provided with the distribution.
116 * 3. All advertising materials mentioning features or use of this software
117 * must display the following acknowledgement:
118 * This product includes software developed by the University of
119 * California, Berkeley and its contributors.
120 * 4. Neither the name of the University nor the names of its contributors
121 * may be used to endorse or promote products derived from this software
122 * without specific prior written permission.
124 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
125 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
126 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
127 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
128 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
129 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
130 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
131 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
132 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
133 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
137 #define NF_MARK 0x1 /* marker for cycle detection */
138 #define NF_ACYCLIC 0x2 /* this node is cycle free */
139 #define NF_NODEST 0x4 /* Unreachable */
141 typedef struct node_str NODE;
143 NODE **n_prevp; /* pointer to previous node's n_next */
144 NODE *n_next; /* next node in graph */
145 NODE **n_arcs; /* array of arcs to other nodes */
146 int n_narcs; /* number of arcs in n_arcs[] */
147 int n_arcsize; /* size of n_arcs[] array */
148 int n_refcnt; /* # of arcs pointing to this node */
149 int n_flags; /* NF_* */
150 const char *n_name; /* name of this node */
153 static NODE *graph = NULL, **cycle_buf = NULL, **longest_cycle = NULL;
154 static int longest = 0;
155 static NODE *sorted = NULL, *last = NULL;
157 /* Find a node in the graph (insert if not found) and return a pointer to it. */
159 get_node(const char *name)
163 for (n = graph; n && n->n_name && strcmp(n->n_name, name);
168 n = xnfalloc(sizeof(NODE));
177 /* Add to linked list. */
178 if ((n->n_next = graph) != NULL)
179 graph->n_prevp = &n->n_next;
187 * add an arc from node s1 to node s2 in the graph. If s1 or s2 are not in
188 * the graph, then add them.
191 add_arc(const char *s1, const char *s2)
205 * Check if this arc is already here.
207 for (i = 0; i < n1->n_narcs; i++)
208 if (n1->n_arcs[i] == n2)
213 if (n1->n_narcs == n1->n_arcsize) {
216 bsize = n1->n_arcsize * sizeof(*n1->n_arcs) * 2;
217 n1->n_arcs = xnfrealloc(n1->n_arcs, bsize);
218 n1->n_arcsize = bsize / sizeof(*n1->n_arcs);
220 n1->n_arcs[n1->n_narcs++] = n2;
225 * Clear the NODEST flag from all nodes.
232 for (n = graph; n != NULL; n = n->n_next)
233 n->n_flags &= ~NF_NODEST;
236 /* print node and remove from graph (does not actually free node) */
238 remove_node(NODE * n)
245 ErrorF("%s\n", n->n_name);
247 newnode = xnfalloc(sizeof(NODE));
248 memcpy(newnode, n, sizeof(NODE));
250 last->n_next = newnode;
254 newnode->n_next = NULL;
256 for (np = n->n_arcs, i = n->n_narcs; --i >= 0; np++)
259 *n->n_prevp = n->n_next;
261 n->n_next->n_prevp = n->n_prevp;
265 free_nodes(NODE * nodelist)
269 for (n = nodelist; n;) {
270 nextnode = n->n_next;
276 /* look for the longest? cycle from node from to node to. */
278 find_cycle(NODE * from, NODE * to, int longest_len, int depth)
284 * avoid infinite loops and ignore portions of the graph known
287 if (from->n_flags & (NF_NODEST | NF_MARK | NF_ACYCLIC))
289 from->n_flags |= NF_MARK;
291 for (np = from->n_arcs, i = from->n_narcs; --i >= 0; np++) {
292 cycle_buf[depth] = *np;
294 if (depth + 1 > longest_len) {
295 longest_len = depth + 1;
296 memcpy((char *)longest_cycle,
297 (char *)cycle_buf, longest_len * sizeof(NODE *));
300 if ((*np)->n_flags & (NF_MARK | NF_ACYCLIC | NF_NODEST))
302 len = find_cycle(*np, to, longest_len, depth + 1);
305 ErrorF("%*s %s->%s %d\n", depth, "",
306 from->n_name, to->n_name, len);
310 (*np)->n_flags |= NF_NODEST;
312 if (len > longest_len)
315 if (len > 0 && !longest)
319 from->n_flags &= ~NF_MARK;
323 /* do topological sort on graph */
330 while (graph != NULL) {
332 * Keep getting rid of simple cases until there are none left,
333 * if there are any nodes still in the graph, then there is
337 for (cnt = 0, n = graph; n != NULL; n = next) {
339 if (n->n_refcnt == 0) {
344 } while (graph != NULL && cnt);
351 * Allocate space for two cycle logs - one to be used
352 * as scratch space, the other to save the longest
355 for (cnt = 0, n = graph; n != NULL; n = n->n_next)
357 cycle_buf = xnfalloc(sizeof(NODE *) * cnt);
358 longest_cycle = xnfalloc(sizeof(NODE *) * cnt);
359 if (cycle_buf == NULL || longest_cycle == NULL)
362 for (n = graph; n != NULL; n = n->n_next)
363 if (!(n->n_flags & NF_ACYCLIC)) {
364 if ((cnt = find_cycle(n, n, 0, 0))) {
365 ErrorF("tsort: cycle in data");
366 for (i = 0; i < cnt; i++)
367 ErrorF("%s", longest_cycle[i]->n_name);
372 /* to avoid further checks */
373 n->n_flags |= NF_ACYCLIC;
379 ErrorF("tsort: internal error -- could not find cycle");
388 LoaderSortExtensions(void)
391 ExtensionModule *ext, *newList;
399 longest_cycle = NULL;
402 * Parse list and build the graph. Enter them in reverse order
403 * because tsort() will reverse those that have no depedencies.
405 for (i = numExtensionModules - 1; i >= 0; i--) {
406 ext = &ExtensionModuleList[i];
407 add_arc(ext->name, ext->name);
409 ErrorF("Extension %s:\n", ext->name);
411 if (ext->initDependencies)
412 for (j = 0; ext->initDependencies[j]; j++) {
413 add_arc(ext->initDependencies[j], ext->name);
415 ErrorF("\t%s\n", ext->initDependencies[j]);
420 newList = xnfalloc((numExtensionModules + 1) * sizeof(ExtensionModule));
422 for (node = sorted; node; node = node->n_next) {
423 for (j = 0; j < numExtensionModules; j++)
424 if (!strcmp(node->n_name, ExtensionModuleList[j].name))
426 if (j != numExtensionModules)
427 newList[i++] = ExtensionModuleList[j];
433 newList[i].name = NULL;
434 free(ExtensionModuleList);
435 ExtensionModuleList = newList;
437 for (i = 0; ExtensionModuleList[i].name; i++)
438 ErrorF("Extension %s\n", ExtensionModuleList[i].name);