home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 1.ddi / MWHC.001 / X < prev    next >
Encoding:
Text File  |  1992-12-09  |  4.1 KB  |  209 lines

  1. /*
  2.  * $Log: doentry.c,v $
  3.  * Revision 1.2  1992/03/04  20:21:18  thomasn
  4.  * *** empty log message ***
  5.  * */
  6.  
  7. // Define OLD_INIT to test these routines with old-style ctor and dtor arrays.
  8. // The new arrays consist of structs that associate an ordering with the
  9. // functions.
  10. // #define OLD_INIT 1
  11.  
  12. // Define TESTING to make this a standalone module that tests itself.
  13. // Only valid for new init style
  14. // #define TESTING 1
  15.  
  16. // Define TESTING2 if you want copious debug traces.
  17. // #define TESTING2 1
  18.  
  19. // MAX_INIT is the maximum initialization level.  The possible levels are
  20. // in the range 0...MAX_INIT.
  21. #define MAX_INIT 255
  22.  
  23. #if defined(TESTING) && defined(OLD_INIT)
  24. # error Can't test old initialization style.
  25. #endif
  26.  
  27. #if defined(TESTING2) && !defined(TESTING)
  28. #error Can't output debug traces unless in testing mode.
  29. #endif
  30.  
  31. typedef unsigned long ulong;
  32.  
  33. #ifndef OLD_INIT
  34. typedef struct {
  35.        ulong    init_level;    // C++ initialization level
  36.        void (*ct)();        // pointer to constructor function
  37.        ulong    slop;        // not used now, maybe used later.
  38.        } ct_elem, *ct_array;
  39.  
  40. #else
  41. typedef void (*ct)();
  42. typedef ct *ct_array;
  43. #endif
  44.  
  45.  
  46. static ct_array destructors=0;
  47. static ulong n_destructors=0;
  48. static void _mwdo_dtors();
  49. #include <stdlib.h>
  50.  
  51.  
  52. /*
  53.  *  First, a few routines to manipulate unordered arrays that have some
  54.  *  ordering built into them...
  55.  */
  56. static int comparegl(int val, int gr, int less) {
  57.     return (val > gr && val < less);
  58.     }
  59. static int comparelg(int val, int less, int gr) {
  60.     return (val > gr && val < less);
  61.     }
  62.         
  63. /*
  64.  *  Now, iterate over the list, using the appropriate compare function
  65.  *  depending on the order of the traversal.
  66.  */
  67. static void iterate(ct_array ctors, int n_ctors, int start, int finish) {
  68.     int cur = start;
  69.     int next_high = finish;
  70.  
  71.     int (*compare)(int,int,int);
  72.  
  73.     if (start>finish) compare = comparegl;
  74.     else compare = comparelg;
  75.  
  76.     for(int done = 0; done < n_ctors; ) {
  77.         for (int done2 = 0; done2 < n_ctors; done2++) {
  78. #ifdef TESTING2
  79.             printf("Testing element %d (level %d) for level %d\n",
  80.                 done2, ctors[done2].init_level, cur);
  81. #endif
  82.             if (ctors[done2].init_level == cur) {
  83.                 if (ctors[done2].ct) (ctors[done2].ct)();
  84.                 done++;
  85.                 }
  86.             else {
  87.                 if (compare(ctors[done2].init_level,
  88.                         next_high, cur)) {
  89.                     next_high = ctors[done2].init_level;
  90. #ifdef TESTING2
  91.                     printf("Updating next_high to %d\n",
  92.                         next_high);
  93.                     }
  94.                 else {
  95.                     printf("Not updating next_high\n");
  96. #endif
  97.                     }
  98.                 }
  99.             }
  100. #ifdef TESTING2
  101.         printf("next_high = %d\n",next_high);
  102. #endif
  103.         cur = next_high;
  104.         next_high = finish;
  105.         }
  106.     }
  107.  
  108.  
  109.  
  110. /*
  111.  *
  112.  *
  113.  */
  114. void _mwdo_ctors(ct_array ctors, int ctor_size,
  115.             ct_array dtors, int dtor_size) {
  116.  
  117.     int n_ctors = ctor_size / sizeof(ctors[0]); // adjust size
  118.  
  119. #ifndef OLD_INIT
  120.     iterate(ctors, n_ctors, MAX_INIT, 0);
  121.  
  122. #else
  123.     int i = 0;
  124.     while (i < n_ctors) {
  125.         if (ctors[i]) (ctors[i])();
  126.         i++;
  127.         }
  128.     
  129. #endif
  130.  
  131.     // register our destructor handler.
  132.     if (dtor_size && dtors!=0) {
  133.         destructors = dtors;
  134.         n_destructors = dtor_size / sizeof(dtors[0]);
  135.         atexit(_mwdo_dtors);
  136.         }
  137.     }
  138.  
  139. static void _mwdo_dtors() {
  140.  
  141. #ifndef OLD_INIT
  142.     iterate(destructors, n_destructors, 0, MAX_INIT);
  143. #else
  144.     // destruct in reverse order
  145.     int i = n_destructors-1;
  146.     while (i>=0) {    
  147.         if (destructors[i]) (destructors[i])();
  148.         i--;
  149.         }
  150.     n_destructors = 0;  // Make sure we only destruct once
  151. #endif
  152.     }
  153.  
  154.  
  155. #ifdef TESTING
  156.  
  157. void ctor1() {
  158.     printf("In ctor 1\n");
  159.     }
  160.  
  161. void ctor2() {
  162.     printf("In ctor 2\n");
  163.     }
  164.  
  165. void ctor3() {
  166.     printf("In ctor 3\n");
  167.     }
  168.  
  169. void ctor4() {
  170.     printf("In ctor 4\n");
  171.     }
  172.  
  173. void dtor1() {
  174.     printf("In dtor 1\n");
  175.     }
  176.  
  177. void dtor2() {
  178.     printf("In dtor 2\n");
  179.     }
  180.  
  181. void dtor3() {
  182.     printf("In dtor 3\n");
  183.     }
  184.  
  185. void dtor4() {
  186.     printf("In dtor 4\n");
  187.     }
  188.  
  189. ct_elem ctlist[4] = {
  190.     { 40, ctor1, 0 },
  191.     { 24, ctor2, 0 },
  192.     {125, ctor3, 0 },
  193.     { 31, ctor4, 0 }
  194.     };
  195.  
  196. ct_elem dtlist[4] = {
  197.     { 40, dtor1, 0 },
  198.     { 24, dtor2, 0 },
  199.     {125, dtor3, 0 },
  200.     { 31, dtor4, 0 }
  201.     };
  202.  
  203. void main () {
  204.     _mwdo_ctors(ctlist, sizeof(ctlist), dtlist, sizeof(dtlist));
  205.     return;
  206.     }
  207.     
  208. #endif
  209.