sgl

annotate src/wsys.c @ 1:0c13a30be2c1

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