sgl

view src/wsys.c @ 1:0c13a30be2c1

wsys
author John Tsiombikas <nuclear@siggraph.org>
date Wed, 11 May 2011 05:34:37 +0300
parents
children 1b6c5dadb460
line source
1 #include <stdio.h>
2 #include "wsys.h"
4 struct wsys_module *wslist;
5 int wscount;
7 int sgl_register_module(struct wsys_module *ws)
8 {
9 ws->next = wslist;
10 wslist = ws;
11 wscount++;
12 return 0;
13 }
15 #define APPEND(node) \
16 do { \
17 if(!res) { \
18 res = tail = node; \
19 } else { \
20 tail->next = (node); \
21 tail = (node); \
22 } \
23 (node) = (node)->next; \
24 tail->next = 0; \
25 } while(0)
27 static struct wsys_module *merge(struct wsys_module *list1, struct wsys_module *list2)
28 {
29 struct wsys_module *res, *tail;
31 res = tail = 0;
33 while(list1 || list2) {
34 if(list1 && list2) {
35 if(list1->prio <= list2->prio) {
36 APPEND(list1);
37 } else {
38 APPEND(list2);
39 }
40 } else if(list1) {
41 APPEND(list1);
42 } else {
43 APPEND(list2);
44 }
45 }
46 return res;
47 }
49 static struct wsys_module *msort(struct wsys_module *list, int count)
50 {
51 int i, mid = count / 2;
52 struct wsys_module *prev, *list2 = list;
54 if(!list->next) {
55 return list;
56 }
57 for(i=0; i<mid; i++) {
58 prev = list2;
59 list2 = list2->next;
60 }
61 prev->next = 0;
63 list = msort(list, mid);
64 list2 = msort(list2, count - mid);
65 return merge(list, list2);
66 }