home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / iffconverter / memorymanagement.c < prev    next >
C/C++ Source or Header  |  1997-01-07  |  4KB  |  156 lines

  1. /*
  2. **     $VER: MemoryManagement.c V0.02 (20-06-95)
  3. **
  4. **     Author:  Gerben Venekamp
  5. **     Updates: 14-06-95  Version 0.01     Initial module.
  6. **              20-06-95  Version 0.02     FreeMemory and FreeThisMem have
  7. **                                         completly rewritten. FreeThisMem
  8. **                                         now marks freed memory as invalid.
  9. **                                         FreeMemory relies entierly on
  10. **                                         FreeThisMem.
  11. **
  12. **  MemoryManagement.c contains all the neseccary functions to control
  13. **  memory management for IFFConverter.
  14. **
  15. */
  16.  
  17.  
  18. #include <exec/memory.h>
  19. #include <proto/exec.h>
  20.  
  21. #include "IFFConverter.h"
  22.  
  23.  
  24. // Defining variables
  25. APTR LoadFileName     = NULL;
  26. APTR SaveFileName     = NULL;
  27. APTR SaveBuffer       = NULL;
  28. ULONG * ColourMap     = NULL;
  29. ULONG * SColourMap    = NULL;
  30. ULONG * PlanePtrs     = NULL;
  31. STRPTR GraphicsDrawer = NULL;
  32.  
  33. ULONG LoadFileNameSize   = 0;
  34. ULONG SaveFileNameSize   = 0;
  35. ULONG SaveBufferSize     = 0;
  36. ULONG GraphicsDrawerSize = 0;
  37. ULONG ColourMapSize      = 16*4;
  38. ULONG PlanePtrsSize;
  39.  
  40. // Defining protos
  41. void AllocateMemory(void);
  42. BOOL AllocThisMem(APTR *, ULONG, ULONG);
  43. BOOL AllocThisMemNoComplain(APTR *, ULONG, ULONG);
  44. void FreeMemory(void);
  45. void FreeThisMem(APTR, ULONG);
  46.  
  47. /*
  48. **  AllocateMemory()
  49. **
  50. **     Will allocate all the needed memory for IFFConverter to run. When
  51. **     'AllocateMemory' fails, it will notify the user of the problem
  52. **     and waits until the user acknowlege the problem and then it quits.
  53. **
  54. **  pre:  None.
  55. **  post: None.
  56. **
  57. */
  58. void AllocateMemory()
  59. {
  60.    if( AllocThisMem(&ColourMap, ColourMapSize, MEMF_CLEAR) )
  61.       if( AllocThisMem(&SColourMap, ColourMapSize, MEMF_CLEAR) )
  62.          // We know that all necessay memory has been allocated,
  63.          // so let us return to the calling function.
  64.          return;
  65.          
  66.    ErrorHandler( IFFerror_NoMemory, (APTR)ColourMapSize );   // Exit to system.
  67. }
  68.  
  69.  
  70. /*
  71. **  Result = AllocThisMem(MemToAlloc, MemToAllocSize, MemType)
  72. **
  73. **     Alloacates a piece of memory when possible. If it turns out
  74. **     to be impossible, then the users will be notified of the
  75. **     problem and 'AllocThisMem' will wait until the user will
  76. **     acknowlege the problem. The memory will be allocated according
  77. **     to 'MemType'.
  78. **
  79. **  pre:  MemToAlloc - NULL Pointer.
  80. **        MemToAllocSize - Size of the memory to alloacte.
  81. **        MemType - Type of memory to be allocated.
  82. **  post: MemToAlloc - Pointer to allocated memory. Or NULL if failure.
  83. **        Result - TRUE if memory allocation was succesful,
  84. **                 FALSE if unsuccesful.
  85. **
  86. */
  87. BOOL AllocThisMem(APTR *MemToAlloc, ULONG MemToAllocSize, ULONG MemType)
  88. {
  89.    if(!(*MemToAlloc = AllocMem(MemToAllocSize, MemType)))
  90.    {
  91.       ErrorHandler( IFFerror_NoMemoryDoReturn, (APTR)MemToAllocSize );
  92.       return(FALSE);
  93.    }
  94.  
  95.    return(TRUE);
  96. }
  97.  
  98.  
  99. /*
  100. **  Result = AllocThisMemNoComplain( MemToAlloc, MemToAllocSize, MemType)
  101. **
  102. **     Specification are the same as AllocTheMem. The exception however,
  103. **     is that this function will no complain about failure of 'AllocMem()'.
  104. **     It will just return TRUE for a succes and FALSE for failure.
  105. **
  106. */
  107. BOOL AllocThisMemNoComplain(APTR *MemToAlloc, ULONG MemToAllocSize, ULONG MemType)
  108. {
  109.    if(!(*MemToAlloc = AllocMem(MemToAllocSize, MemType)))
  110.       return(FALSE);
  111.    return(TRUE);
  112. }
  113.  
  114.  
  115. /*
  116. **  FreeMemory()
  117. **
  118. **     Will give back all the allocated memory (you gotta keep friends).
  119. **
  120. **  pre:  None.
  121. **  post: None.
  122. **
  123. */
  124. void FreeMemory()
  125. {
  126.    FreeThisMem(&LoadFileName, LoadFileNameSize);
  127.    FreeThisMem(&SaveFileName, SaveFileNameSize);
  128.    FreeThisMem(&ColourMap, ColourMapSize);
  129.    FreeThisMem(&SColourMap, ColourMapSize);
  130.    FreeThisMem(&PlanePtrs, PlanePtrsSize);
  131.    FreeThisMem(&SaveBuffer, SaveBufferSize);
  132.    FreeThisMem(&GraphicsDrawer, GraphicsDrawerSize);
  133. }
  134.  
  135.  
  136. /*
  137. **  FreeThisMem(MemToFree, MemToFreenSize)
  138. **
  139. **     Frees when possible a piece of allocated memory. NOTE: be sure
  140. **     to free allocated memory ONLY!!!! When the memory has been freed,
  141. **     MemToFree will be marked as invalid (NULL).
  142. **
  143. **  pre:  MemToFree - Pointer to Allocated memory or NULL.
  144. **        MemToFreeSize - Size of memory to be freeed.
  145. **  post: MemToFree - NULL.
  146. **
  147. */
  148. void FreeThisMem(APTR *MemToFree, ULONG MemToFreeSize)
  149. {
  150.    if(*MemToFree)
  151.    {
  152.       FreeMem(*MemToFree, MemToFreeSize);
  153.       *MemToFree = NULL;
  154.    }
  155. }
  156.