home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / gui / precog2_1.lha / Precognition2_1 / src / src.lha / Precognition / AutoCleanUp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  1.3 KB  |  72 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <exec/lists.h>
  4. #include <exec/nodes.h>
  5. #ifndef __GNUC__
  6. #include <clib/exec_protos.h>
  7. #include <clib/alib_protos.h>
  8. #endif
  9. #ifdef __GNUC__
  10. #include <proto/exec.h>
  11. #endif
  12. #ifdef __SASC
  13. #include <proto/exec.h>
  14. #endif
  15.  
  16.  
  17. #include "AutoCleanUp.h"
  18.  
  19. struct CleanUpNode
  20. {
  21.    struct MinNode  cun_Node;
  22.    ptr2func        CleanUpFunc;
  23.    void           *UserData;
  24. };
  25.  
  26. #define NEWNODE ( (struct CleanUpNode *)AllocMem( sizeof( struct CleanUpNode ), MEMF_CLEAR ) )
  27. #define FREENODE(n) FreeMem( n, sizeof( struct CleanUpNode ) )
  28.  
  29. #define NEVER_INITIALIZED ( CleanUpList.mlh_Head == NULL )
  30.  
  31. struct MinList CleanUpList;
  32.  
  33. void*
  34. RegisterCleanUp( ptr2func CleanUpFunc, void *UserData )
  35. {
  36.    struct CleanUpNode *node;
  37.  
  38.    if NEVER_INITIALIZED
  39.       NewList( (struct List*)&CleanUpList );
  40.  
  41.    if( ( node = NEWNODE ) != NULL )
  42.    {
  43.       node->CleanUpFunc = CleanUpFunc;
  44.       node->UserData    = UserData;
  45.  
  46.       AddHead( (struct List *)&CleanUpList,
  47.                (struct Node *)node );
  48.    }
  49.  
  50.    return node;
  51. }
  52.  
  53.  
  54. void
  55. AutoCleanUp( void )
  56. {
  57.    struct CleanUpNode *node;
  58.  
  59.    if NEVER_INITIALIZED
  60.    {
  61.       return;
  62.    }
  63.  
  64.    while( ( node = (struct CleanUpNode *)
  65.                    RemHead( (struct List *)&CleanUpList ) ) != NULL )
  66.    {
  67.       (*node->CleanUpFunc)( node->UserData );
  68.       FREENODE( node );
  69.    }
  70. }
  71.  
  72.