home *** CD-ROM | disk | FTP | other *** search
- /*
- * $Log: doentry.c,v $
- * Revision 1.2 1992/03/04 20:21:18 thomasn
- * *** empty log message ***
- * */
-
- // Define OLD_INIT to test these routines with old-style ctor and dtor arrays.
- // The new arrays consist of structs that associate an ordering with the
- // functions.
- // #define OLD_INIT 1
-
- // Define TESTING to make this a standalone module that tests itself.
- // Only valid for new init style
- // #define TESTING 1
-
- // Define TESTING2 if you want copious debug traces.
- // #define TESTING2 1
-
- // MAX_INIT is the maximum initialization level. The possible levels are
- // in the range 0...MAX_INIT.
- #define MAX_INIT 255
-
- #if defined(TESTING) && defined(OLD_INIT)
- # error Can't test old initialization style.
- #endif
-
- #if defined(TESTING2) && !defined(TESTING)
- #error Can't output debug traces unless in testing mode.
- #endif
-
- typedef unsigned long ulong;
-
- #ifndef OLD_INIT
- typedef struct {
- ulong init_level; // C++ initialization level
- void (*ct)(); // pointer to constructor function
- ulong slop; // not used now, maybe used later.
- } ct_elem, *ct_array;
-
- #else
- typedef void (*ct)();
- typedef ct *ct_array;
- #endif
-
-
- static ct_array destructors=0;
- static ulong n_destructors=0;
- static void _mwdo_dtors();
- #include <stdlib.h>
-
-
- /*
- * First, a few routines to manipulate unordered arrays that have some
- * ordering built into them...
- */
- static int comparegl(int val, int gr, int less) {
- return (val > gr && val < less);
- }
- static int comparelg(int val, int less, int gr) {
- return (val > gr && val < less);
- }
-
- /*
- * Now, iterate over the list, using the appropriate compare function
- * depending on the order of the traversal.
- */
- static void iterate(ct_array ctors, int n_ctors, int start, int finish) {
- int cur = start;
- int next_high = finish;
-
- int (*compare)(int,int,int);
-
- if (start>finish) compare = comparegl;
- else compare = comparelg;
-
- for(int done = 0; done < n_ctors; ) {
- for (int done2 = 0; done2 < n_ctors; done2++) {
- #ifdef TESTING2
- printf("Testing element %d (level %d) for level %d\n",
- done2, ctors[done2].init_level, cur);
- #endif
- if (ctors[done2].init_level == cur) {
- if (ctors[done2].ct) (ctors[done2].ct)();
- done++;
- }
- else {
- if (compare(ctors[done2].init_level,
- next_high, cur)) {
- next_high = ctors[done2].init_level;
- #ifdef TESTING2
- printf("Updating next_high to %d\n",
- next_high);
- }
- else {
- printf("Not updating next_high\n");
- #endif
- }
- }
- }
- #ifdef TESTING2
- printf("next_high = %d\n",next_high);
- #endif
- cur = next_high;
- next_high = finish;
- }
- }
-
-
-
- /*
- *
- *
- */
- void _mwdo_ctors(ct_array ctors, int ctor_size,
- ct_array dtors, int dtor_size) {
-
- int n_ctors = ctor_size / sizeof(ctors[0]); // adjust size
-
- #ifndef OLD_INIT
- iterate(ctors, n_ctors, MAX_INIT, 0);
-
- #else
- int i = 0;
- while (i < n_ctors) {
- if (ctors[i]) (ctors[i])();
- i++;
- }
-
- #endif
-
- // register our destructor handler.
- if (dtor_size && dtors!=0) {
- destructors = dtors;
- n_destructors = dtor_size / sizeof(dtors[0]);
- atexit(_mwdo_dtors);
- }
- }
-
- static void _mwdo_dtors() {
-
- #ifndef OLD_INIT
- iterate(destructors, n_destructors, 0, MAX_INIT);
- #else
- // destruct in reverse order
- int i = n_destructors-1;
- while (i>=0) {
- if (destructors[i]) (destructors[i])();
- i--;
- }
- n_destructors = 0; // Make sure we only destruct once
- #endif
- }
-
-
- #ifdef TESTING
-
- void ctor1() {
- printf("In ctor 1\n");
- }
-
- void ctor2() {
- printf("In ctor 2\n");
- }
-
- void ctor3() {
- printf("In ctor 3\n");
- }
-
- void ctor4() {
- printf("In ctor 4\n");
- }
-
- void dtor1() {
- printf("In dtor 1\n");
- }
-
- void dtor2() {
- printf("In dtor 2\n");
- }
-
- void dtor3() {
- printf("In dtor 3\n");
- }
-
- void dtor4() {
- printf("In dtor 4\n");
- }
-
- ct_elem ctlist[4] = {
- { 40, ctor1, 0 },
- { 24, ctor2, 0 },
- {125, ctor3, 0 },
- { 31, ctor4, 0 }
- };
-
- ct_elem dtlist[4] = {
- { 40, dtor1, 0 },
- { 24, dtor2, 0 },
- {125, dtor3, 0 },
- { 31, dtor4, 0 }
- };
-
- void main () {
- _mwdo_ctors(ctlist, sizeof(ctlist), dtlist, sizeof(dtlist));
- return;
- }
-
- #endif
-