home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / disk / misc / TransADF.lha / TransADF / Source / mem_chunks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-05  |  2.4 KB  |  102 lines

  1. /* mem_chunks.c - Simple routines that keep track of memory allocation.
  2. ** Copyright (C) 1998 Karl J. Ots
  3. ** 
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. ** 
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ** GNU General Public License for more details.
  13. ** 
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17. */
  18.  
  19. #include <exec/types.h>
  20. #include <exec/nodes.h>
  21. #include <exec/lists.h>
  22. #include <exec/memory.h>
  23. #include <clib/alib_protos.h>
  24. #include <clib/exec_protos.h>
  25.  
  26. struct List MemChunkList;
  27.  
  28. struct MemChunk {
  29.   struct Node mc_Node;
  30.   
  31.   ULONG  mc_ByteSize;
  32.   void  *mc_Chunk;
  33. };
  34.  
  35.  
  36. /*
  37. ** Initalise the Memory Chunk List ready for use
  38. */
  39. void
  40. initMemChunkList (void)
  41. {
  42.   NewList (&MemChunkList);
  43. }  
  44.  
  45.  
  46. /*
  47. ** Clear the Memory Chunk List of all entries
  48. */
  49. void
  50. deleteMemChunkList (void)
  51. {
  52.   struct MemChunk *chunk;
  53.   
  54.   while (( chunk = (struct MemChunk *) RemTail (&MemChunkList) ))
  55.     FreeMem ((void *)chunk, (chunk->mc_ByteSize + sizeof (struct MemChunk)));
  56. }
  57.  
  58.  
  59. /*
  60. ** Allocate a block of memory and add it to the Memory Chunk List
  61. */
  62. void *
  63. myAllocMem (ULONG byteSize, ULONG attributes)
  64. {
  65.   struct MemChunk *chunk;
  66.   UBYTE *mem;
  67.   
  68.   mem = (UBYTE *)AllocMem ((byteSize + sizeof (struct MemChunk)), attributes);
  69.   if (mem)
  70.   {
  71.     chunk = (struct MemChunk *)mem;
  72.     mem += sizeof (struct MemChunk);
  73.     
  74.     chunk->mc_ByteSize = byteSize;
  75.     chunk->mc_Chunk = (void *)mem;
  76.     
  77.     AddTail (&MemChunkList, (struct Node *)chunk);
  78.   }
  79.   
  80.   return (void *)mem;
  81. }
  82.  
  83.  
  84. /*
  85. ** Remove a block from the Memory Chunk List and free it
  86. */
  87. void
  88. myFreeMem (void *memoryBlock)
  89. {
  90.   struct MemChunk *chunk;
  91.   
  92.   chunk = (struct MemChunk *)
  93.           (((UBYTE *)memoryBlock) - sizeof (struct MemChunk));
  94.   
  95.   /* Make sure this memory pointer is part of the chunk list */
  96.   if (memoryBlock == chunk->mc_Chunk)
  97.   {
  98.     Remove ((struct Node *)chunk);
  99.     FreeMem ((void *)chunk, (chunk->mc_ByteSize + sizeof (struct MemChunk)));
  100.   }
  101. }
  102.