home *** CD-ROM | disk | FTP | other *** search
/ WADS of WADS / WadsOfWads.1994.zip / OTHER / REJECT / SOURCE.ZIP / MEMHANDL.CPP < prev    next >
C/C++ Source or Header  |  1994-05-31  |  8KB  |  237 lines

  1. //**********************************************************************************
  2. //    REJECT.EXE - Reject data table builder for DOOM
  3. //    Copyright (C) 1994 L.M.WITEK 
  4. //
  5. //    This program is free software; you can redistribute it and/or modify
  6. //    it under the terms of the GNU General Public License as published by
  7. //    the Free Software Foundation; either version 1, or (at your option)
  8. //    any later version.
  9. //
  10. //    This program is distributed in the hope that it will be useful,
  11. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //    GNU General Public License for more details.
  14. //
  15. //    You should have received a copy of the GNU General Public License
  16. //    along with this program; if not, write to the Free Software
  17. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. //**********************************************************************************
  20. #include <string.h>
  21. #include "memhandl.hpp"
  22.  
  23.  
  24. //*********************************************************************************
  25. //*********************************************************************************
  26. //**
  27. //**      Implementation of the MemoryBlock class
  28. //**
  29. //*********************************************************************************
  30. //*********************************************************************************
  31.  
  32. /*********************************************************************************
  33. **
  34. **  CONSTRUCTOR: MemoryBlock::MemoryBlock (size_t size)  
  35. **
  36. **               Allocate buffer & set ref count to 0.
  37. **
  38. *********************************************************************************/
  39.  
  40. MemoryBlock::MemoryBlock (size_t size) 
  41.             : szbuffer (size), count (0), buffer (new unsigned char[size]) 
  42. {
  43. }
  44.  
  45.  
  46. /*********************************************************************************
  47. **
  48. **  CONSTRUCTOR: MemoryBlock::~MemoryBlock ()  
  49. **
  50. **               Delete the block of memory.
  51. **
  52. *********************************************************************************/
  53.  
  54. MemoryBlock::~MemoryBlock () 
  55.      delete [] buffer;
  56. }
  57.  
  58.  
  59.  
  60. //*********************************************************************************
  61. //*********************************************************************************
  62. //**
  63. //**      Implementation of the MemHandle class
  64. //**
  65. //*********************************************************************************
  66. //*********************************************************************************
  67.  
  68.  
  69. /*********************************************************************************
  70. **
  71. **  CONSTRUCTOR: MemHandle::MemHandle (MemHandle &mh)  
  72. **
  73. **              Constructs a memory handle to an existing block of memory 
  74. **              specified by the 'mh' parameter.
  75. **
  76. *********************************************************************************/
  77.  
  78. MemHandle::MemHandle (const MemHandle &mh)
  79. {
  80.      mem = mh.mem; 
  81.      (mem->count)++;
  82. }
  83.  
  84.  
  85. /*********************************************************************************
  86. **
  87. **  CONSTRUCTOR: MemHandle::MemHandle (size_t size)  
  88. **
  89. **              Constructs a memory handle to a new block of memory of 'size' 
  90. **              bytes. The block of memory is not initialised to any specific 
  91. **              value.
  92. **
  93. *********************************************************************************/
  94.  
  95. MemHandle::MemHandle (size_t size)
  96.           :mem (new MemoryBlock (size))
  97. {    
  98.      (mem->count)++;
  99. }
  100.  
  101. /*********************************************************************************
  102. **
  103. **  CONSTRUCTOR: MemHandle::MemHandle (size_t size, unsigned char filler)  
  104. **
  105. **              Constructs a memory handle to a new block of memory of 'size' 
  106. **              bytes and initialises the block to the value specified by
  107. **              the 'filler' parameter.
  108. **
  109. *********************************************************************************/
  110.  
  111. MemHandle::MemHandle (size_t size, unsigned char filler)
  112.           :mem (new MemoryBlock (size))
  113. {
  114.      (mem->count)++;
  115.      memset (mem->buffer, filler, size);
  116. }
  117.  
  118. /*********************************************************************************
  119. **
  120. **  DESTRUCTOR: MemHandle::~MemHandle 
  121. **
  122. **              Destroys the memory handle. If the block of memory associated  
  123. **              with this handle has no other handles referencing it then it 
  124. **              id deleted also.
  125. **
  126. *********************************************************************************/
  127.  
  128. MemHandle::~MemHandle ()
  129. {
  130.      (mem->count)--;
  131.      if (mem->count == 0) delete mem;
  132. }
  133.  
  134. /*********************************************************************************
  135. **
  136. **  FUNCTION: MemHandle::Clone () const  
  137. **
  138. **            Make a copy of the memory block held by the MemHandle object.
  139. **
  140. **  RETURNS:  a MemHandle to the cloned memory block 
  141. **
  142. *********************************************************************************/
  143.  
  144. MemHandle MemHandle::Clone () const
  145. {
  146.      MemHandle mh (Size());                               // make a memory block the same size as me
  147.      memcpy (mh.mem->buffer, mem->buffer, Size());        // copy my data to this new memory block
  148.  
  149.      return (mh);                                         // return the handle to this new memory block
  150. }
  151.  
  152.  
  153. /*********************************************************************************
  154. **
  155. **  FUNCTION: MemHandle& MemHandle::operator= (const MemHandle& mh)
  156. **
  157. **            Assign a new memory block to this handle
  158. **
  159. *********************************************************************************/
  160.  
  161. MemHandle& MemHandle::operator= (const MemHandle& mh)
  162. {
  163.      if (mh.mem != mem)                          // make sure we dont copy ourself
  164.      {
  165.           (mem->count)--;                        // detatch from old memory block 
  166.           if (mem->count == 0) delete mem;
  167.                
  168.           mem = mh.mem;                          // attach to new memory block
  169.           (mem->count)++;
  170.      }
  171.      return *this;
  172. }
  173.  
  174.  
  175. /*********************************************************************************
  176. **
  177. **  FUNCTION: void MemHandle::Shrink (size_t newsz)
  178. **
  179. **            Shrink the size of a memory block. Does not change the memory 
  180. **            the memory allocation just marks the block a being smaller.
  181. **            Useful for creating a buffer to hold some data where the data 
  182. **            size is unknown. Memory block can be shrunk after data is  
  183. **            placed in buffer.
  184. **
  185. *********************************************************************************/
  186.  
  187. void MemHandle::Shrink (size_t newsz)
  188. {
  189.      if (newsz < mem->szbuffer)
  190.           mem->szbuffer = newsz;
  191. }
  192.  
  193.  
  194. //*********************************************************************************
  195. //*********************************************************************************
  196. //**
  197. //**      Test rig for the MemHandle class
  198. //**
  199. //*********************************************************************************
  200. //*********************************************************************************
  201. //#define _TEST_MEMHANDLE_
  202.  
  203. #ifdef _TEST_MEMHANDLE_
  204.  
  205. #include <stdio.h>
  206.  
  207. void main ()
  208. {
  209.      MemHandle mh1 (1024);
  210.      MemHandle mh2 (512);
  211.      MemHandle mh3 (mh2);
  212.  
  213.      strcpy (mh1, "First Block");
  214.      strcpy (mh2, "Second Block");
  215.      
  216. //     memcpy (mh1, mh2, 3);
  217.  
  218.      {    
  219.           MemHandle mhx (mh3);
  220.      }
  221.  
  222.  
  223.      puts (mh1);
  224.      puts (mh2);
  225.  
  226.      mh1 = mh2;
  227.      mh1 = mh2;
  228.  
  229.      puts (mh1);
  230.  
  231.      puts (mh1);  
  232.  
  233. }
  234.  
  235. #endif
  236.