home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / c / alloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-04  |  3.0 KB  |  116 lines

  1. /*
  2. ** Unix-style hassle free memory allocation
  3. ** via Intuition's Alloc/FreeRemember() functions
  4. ** which keep track of memory allocations for ease
  5. ** of deallocation.
  6. ** Copyright (C) 1998 David Benn
  7. ** 
  8. ** This program is free software; you can redistribute it and/or
  9. ** modify it under the terms of the GNU General Public License
  10. ** as published by the Free Software Foundation; either version 2
  11. ** of the License, or (at your option) any later version.
  12. **
  13. ** This program is distributed in the hope that it will be useful,
  14. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ** GNU General Public License for more details.
  17. **
  18. ** You should have received a copy of the GNU General Public License
  19. ** along with this program; if not, write to the Free Software
  20. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21. **
  22. ** Call alloc() or ACEalloc() for each memory allocation 
  23. ** request.
  24. **
  25. ** Call free_alloc() once at the end of a program
  26. ** to free ALL allocated chunks.
  27. **
  28. ** Call clear_alloc() whenever ACEalloc() allocations 
  29. ** must be cleared before the end of a program run. 
  30. ** Calls to ACEalloc() may be freely made subsequently.
  31. **
  32. ** Author: David J Benn
  33. **   Date: 30th June 1993, 1st July 1993,
  34. **       4th April 1994,
  35. **       2nd September 1994,
  36. **       11th March 1995
  37. */
  38.  
  39. #include <exec/types.h>
  40. #include <exec/memory.h>
  41. #include <intuition/intuition.h>
  42.  
  43. /* Allocation lists for db.lib functions and ACE programs. */
  44. struct Remember *RememberList = NULL;
  45. struct Remember *AceAllocList = NULL;
  46.  
  47. /* Functions */
  48. ULONG TheFlags(MemType,bytes)
  49. LONG MemType,bytes;
  50. {
  51. ULONG flags;
  52.  
  53.      switch(MemType)
  54.      {
  55.           case 0L : flags = MEMF_CHIP; break;
  56.           case 1L : flags = MEMF_FAST; break;
  57.           case 2L : flags = MEMF_PUBLIC; break;
  58.  
  59.           case 3L : flags = MEMF_CHIP | MEMF_CLEAR; break;
  60.           case 4L : flags = MEMF_FAST | MEMF_CLEAR; break;
  61.           case 5L : flags = MEMF_PUBLIC | MEMF_CLEAR; break;
  62.  
  63.         case 6L : flags = MEMF_ANY; break;
  64.         case 7L : flags = MEMF_ANY | MEMF_CLEAR; break;
  65.  
  66.         /* see basfun.c */
  67.         case 9L : flags = MEMF_ANY | MEMF_CLEAR; break;    
  68.  
  69.         /* if all else fails... */
  70.           default : flags = MEMF_ANY | MEMF_CLEAR; break;
  71.      }
  72.  
  73.     return flags;
  74. }
  75.  
  76. ULONG alloc(MemType,bytes)
  77. LONG MemType,bytes;
  78. {
  79. /* 
  80. ** Allocate memory as requested (for db.lib functions). 
  81. */    
  82.      return((ULONG)AllocRemember(&RememberList,bytes,TheFlags(MemType,bytes)));     
  83. }
  84.  
  85. ULONG ACEalloc(MemType,bytes)
  86. LONG MemType,bytes;
  87. {
  88. /* 
  89. ** Allocate memory as requested (for ACE programs). 
  90. */    
  91.      return((ULONG)AllocRemember(&AceAllocList,bytes,TheFlags(MemType,bytes)));     
  92. }
  93.  
  94. void free_alloc()
  95. {
  96. /* 
  97. ** Free all memory allocated by AllocRemember().
  98. */
  99.     if (RememberList != NULL) FreeRemember(&RememberList,TRUE);
  100.     if (AceAllocList != NULL) FreeRemember(&AceAllocList,TRUE);
  101. }
  102.  
  103. void clear_alloc()
  104. {
  105. /* 
  106. ** Free all memory allocated by ACEalloc() thus far
  107. ** and prepare for subsequent calls to ACEalloc(). 
  108. */
  109.  
  110.     if (AceAllocList != NULL)
  111.     {
  112.         FreeRemember(&AceAllocList,TRUE);
  113.         AceAllocList = NULL;
  114.     }
  115. }
  116.