sgl

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/wsys.c	Wed May 11 05:34:37 2011 +0300
     1.3 @@ -0,0 +1,66 @@
     1.4 +#include <stdio.h>
     1.5 +#include "wsys.h"
     1.6 +
     1.7 +struct wsys_module *wslist;
     1.8 +int wscount;
     1.9 +
    1.10 +int sgl_register_module(struct wsys_module *ws)
    1.11 +{
    1.12 +	ws->next = wslist;
    1.13 +	wslist = ws;
    1.14 +	wscount++;
    1.15 +	return 0;
    1.16 +}
    1.17 +
    1.18 +#define APPEND(node) \
    1.19 +	do { \
    1.20 +		if(!res) { \
    1.21 +			res = tail = node; \
    1.22 +		} else { \
    1.23 +			tail->next = (node); \
    1.24 +			tail = (node); \
    1.25 +		} \
    1.26 +		(node) = (node)->next; \
    1.27 +		tail->next = 0; \
    1.28 +	} while(0)
    1.29 +
    1.30 +static struct wsys_module *merge(struct wsys_module *list1, struct wsys_module *list2)
    1.31 +{
    1.32 +	struct wsys_module *res, *tail;
    1.33 +
    1.34 +	res = tail = 0;
    1.35 +
    1.36 +	while(list1 || list2) {
    1.37 +		if(list1 && list2) {
    1.38 +			if(list1->prio <= list2->prio) {
    1.39 +				APPEND(list1);
    1.40 +			} else {
    1.41 +				APPEND(list2);
    1.42 +			}
    1.43 +		} else if(list1) {
    1.44 +			APPEND(list1);
    1.45 +		} else {
    1.46 +			APPEND(list2);
    1.47 +		}
    1.48 +	}
    1.49 +	return res;
    1.50 +}
    1.51 +
    1.52 +static struct wsys_module *msort(struct wsys_module *list, int count)
    1.53 +{
    1.54 +	int i, mid = count / 2;
    1.55 +	struct wsys_module *prev, *list2 = list;
    1.56 +
    1.57 +	if(!list->next) {
    1.58 +		return list;
    1.59 +	}
    1.60 +	for(i=0; i<mid; i++) {
    1.61 +		prev = list2;
    1.62 +		list2 = list2->next;
    1.63 +	}
    1.64 +	prev->next = 0;
    1.65 +
    1.66 +	list = msort(list, mid);
    1.67 +	list2 = msort(list2, count - mid);
    1.68 +	return merge(list, list2);
    1.69 +}