home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / gnulib / normal / ger_atexit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  1.8 KB  |  90 lines

  1. extern struct ExecBase * SysBase;
  2.  
  3. static __inline void*
  4. AllocMem (unsigned long byteSize,unsigned long requirements)
  5. {
  6.   register void*  _res  __asm("d0");
  7.   register struct ExecBase *a6 __asm("a6") = SysBase;
  8.   register unsigned long d0 __asm("d0") = byteSize;
  9.   register unsigned long d1 __asm("d1") = requirements;
  10.   __asm __volatile ("jsr a6@(-0xc6)"
  11.   : "=r" (_res)
  12.   : "r" (a6), "r" (d0), "r" (d1)
  13.   : "a0","a1","d0","d1", "memory");
  14.   return _res;
  15. }
  16.  
  17. static __inline void
  18. FreeMem (void *memoryBlock,unsigned long byteSize)
  19. {
  20.   register struct ExecBase *a6 __asm("a6") = SysBase;
  21.   register void* a1 __asm("a1") = memoryBlock;
  22.   register unsigned long d0 __asm("d0") = byteSize;
  23.   __asm __volatile ("jsr a6@(-0xd2)"
  24.   : /* no output */
  25.   : "r" (a6), "r" (a1), "r" (d0)
  26.   : "a0","a1","d0","d1", "memory");
  27. }
  28.  
  29. struct atexit_node
  30. {
  31.     struct atexit_node *next;
  32. /*    struct atexit_node *prev; */
  33.     unsigned long function;
  34. };
  35.  
  36. static struct atexit_node*__atexit_regs=0;
  37. extern volatile void _exit(int status);
  38. extern void __do_global_dtors ();
  39.  
  40. volatile void exit(int status)
  41. {
  42.     /* alles freigeben, da wären eigentlich nur atexit()-Sachen,
  43.        für den Rest ist der Programmierer schließlich selbst
  44.            verantwortlich */
  45.  
  46.     struct atexit_node *t=__atexit_regs,*t2;
  47.  
  48.     void (*f)(void);
  49.  
  50.  
  51.     if(t)
  52.     {
  53.         while(t)
  54.         {
  55.             (unsigned long)f=t->function;
  56.             f();
  57.             t2=t;
  58.             t=t->next;
  59.             FreeMem(t2,sizeof(struct atexit_node));
  60.         }
  61.     }
  62.  
  63.     __do_global_dtors ();
  64.  
  65.     _exit(status);
  66. }
  67.  
  68. int atexit(unsigned long func)
  69. {
  70.     struct atexit_node *an;
  71.     if((an=(struct atexit_node *)AllocMem(sizeof(struct atexit_node),(1L<<16)))) /* MEMF_CLEAR */
  72.     {
  73.         struct atexit_node *t=__atexit_regs;
  74.  
  75.         /* insert new routine at begin of atexit-list */
  76.  
  77.         __atexit_regs=an;
  78.  
  79.         an->next=t;
  80.         an->function=func;
  81.  
  82.         return(0); /* succsess */
  83.     }
  84.     else
  85.     {
  86.         /* errno=12; */ /* ENOMEM */
  87.         return(-1); /* fail */
  88.     }
  89. }
  90.