home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff384.lzh / NorthC / Example2.LZH / top / coalesce.c < prev    next >
C/C++ Source or Header  |  1990-08-30  |  3KB  |  164 lines

  1. /*
  2.  * Following coalesce functions added by Jeff Lydiatt.  Having split
  3.  * code, data, or BSS hunks in the same section seems to cause the
  4.  * PD Linker Blink some problems.  These functions save all data, BSS,
  5.  * or directives like "PUBLIC" to be dumped at the end.
  6.  */
  7.  
  8. /* Changed to work under NORTHC by Steve Hawtin */
  9.  
  10. #include "top.h"
  11.  
  12. typedef struct save{
  13.     struct save *next;
  14.     char   *value;
  15.     } SAVE;
  16.  
  17. static SAVE *phead;    /* List of Directives like PUBLIC. */
  18. static SAVE *ptail;
  19. static SAVE *dhead;    /* Data list */
  20. static SAVE *dtail;
  21. static SAVE *bhead;    /* BSS list */
  22. static SAVE *btail;
  23. #ifndef NORTHC
  24. static char *dataname;    /* pointer to current data section name. */
  25. static char *bssname;    /* ditto for bss. */
  26. #endif
  27.  
  28. static char *myalloc(size)
  29. unsigned int size;
  30. {
  31.     register char *p;
  32.     char *malloc();
  33.  
  34.     if ( (p = malloc(size)) == NULL ){
  35.         fputs( "Out of Memory!\n", stderr);
  36.         exit(EXIT_FAILURE);
  37.     }
  38.  
  39.     return p;
  40. }
  41.  
  42. #ifndef NORTHC
  43.     void
  44. newBSS(name)
  45. char *name;
  46. {
  47.     register char *p;
  48.  
  49.     if (bssname == NULL){
  50.         p = myalloc(strlen(name)+1);
  51.         strcpy(p,name);
  52.         bssname = p;
  53.     }
  54. }
  55.  
  56.     void
  57. newDATA(name)
  58. char *name;
  59. {
  60.     register char *p;
  61.  
  62.     if (dataname == NULL){
  63.         p = myalloc(strlen(name)+1);
  64.         strcpy(p,name);
  65.         dataname = p;
  66.     }
  67. }
  68. #endif
  69.  
  70. #define BSS  0
  71. #define DATA 1
  72.  
  73.     void
  74. store(m)
  75. int m;
  76. {
  77.     register SAVE *s;
  78.     register char *p;
  79.  
  80.     s = (SAVE *)myalloc(sizeof(struct save));
  81.     p = myalloc(strlen(t_line)+1);
  82.     strcpy(p,t_line);
  83.     s->value = p;
  84.  
  85.     switch(m){
  86.         case BSS:
  87.             if (btail == NULL)
  88.                 bhead = s;
  89.             else
  90.                 btail->next = s;
  91.             btail = s;
  92.             s->next = NULL;
  93.             break;
  94.         case DATA:
  95.             if (dtail == NULL)
  96.                 dhead = s;
  97.             else
  98.                 dtail->next = s;
  99.             dtail = s;
  100.             s->next = NULL;
  101.             break;
  102.         default:
  103.             if (ptail == NULL)
  104.                 phead = s;
  105.             else
  106.                 ptail->next = s;
  107.             ptail = s;
  108.             s->next = NULL;
  109.             break;
  110.     }
  111. }
  112.  
  113. /*
  114.  * Called after the last code section to dump all BSS and DATA
  115.  * sections together.  Follow that with any directives.
  116.  */
  117.  
  118.     void
  119. dumpall()
  120. {
  121.     void dump();
  122.  
  123.     if ( dhead ){
  124.         fprintf(ofp, "\tSECTION\tCODATA,DATA\n");
  125.         dump(dhead);
  126.         dhead = dtail = NULL;
  127. #ifndef NORTHC
  128.         free(dataname);
  129.         dataname = NULL;
  130. #endif
  131.     }
  132.  
  133.     if ( bhead ){
  134.         fprintf(ofp, "\tSECTION\tCOBSS,BSS\n");
  135.         dump(bhead);
  136.         bhead = btail = NULL;
  137. #ifndef NORTHC
  138.         free(bssname);
  139.         bssname = NULL;
  140. #endif
  141.     }
  142.  
  143.     if ( phead ){
  144.         dump(phead);
  145.         phead = ptail = NULL;
  146.     }
  147.  
  148.     fprintf( ofp, "\tEND\n");
  149. }
  150.  
  151.     static void
  152. dump(head)
  153. SAVE *head;
  154. {
  155.     register SAVE *p, *q;
  156.  
  157.     for ( p=head; p; p=q ){
  158.         fputs( p->value, ofp);
  159.         q = p->next;
  160.         free(p->value);
  161.         free(p);
  162.     }
  163. }
  164.