home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / business / printcar / printcar.exe / src / Util / MemBuffer.cc < prev    next >
C/C++ Source or Header  |  2000-06-03  |  4KB  |  213 lines

  1. //
  2. //  $Id: MemBuffer.cc,v 1.1.1.1 2000/06/02 22:23:04 sergey Exp $
  3. //
  4.  
  5. #include <MemoryMgr.h>
  6. #include <ErrorMgr.h>
  7. #include "MemBuffer.h"
  8. #include "DataOutputStream.h"
  9. #include "DataInputStream.h"
  10. #include "Assert.h"
  11. #include "Error.h"
  12.  
  13.  
  14. namespace Util
  15. {
  16.     MemBuffer::MemBuffer():
  17.         _handle(0),
  18.         _lockCount(0)
  19.     {}
  20.  
  21.     MemBuffer::MemBuffer(VoidHand handle):
  22.         _handle(handle),
  23.         _lockCount(0)
  24.     {}
  25.  
  26.     MemBuffer::~MemBuffer()
  27.     {
  28.         free();
  29.     }
  30.  
  31.     MemBuffer::MemBuffer(const MemBuffer& other):
  32.         _handle(0),
  33.         _lockCount(0)
  34.     {
  35.         operator =(other);
  36.     }
  37.  
  38.     MemBuffer& MemBuffer::operator =(const MemBuffer& other)
  39.     {
  40.         free();
  41.  
  42.         _handle = other._handle;
  43.         _lockCount = other._lockCount;
  44.  
  45.         const_cast<MemBuffer&>(other).release();
  46.  
  47.         return *this;
  48.     }
  49.  
  50.     // operations
  51.  
  52.     void* MemBuffer::lock()
  53.     {
  54.         if (_handle != 0)
  55.         {
  56.             ++_lockCount;
  57.             return (char*)MemHandleLock(_handle);
  58.         }
  59.  
  60.         return 0;
  61.     }
  62.  
  63.     const void* MemBuffer::lock() const
  64.     {
  65.          const_cast<MemBuffer*>(this)->lock();
  66.     }
  67.  
  68.     void* MemBuffer::lock(int lockSize)
  69.     {
  70.         if (lockSize > 0)
  71.         {
  72.             if (ensureBufferSize(lockSize))
  73.                 return lock();
  74.         }
  75.  
  76.         return 0;
  77.     }
  78.  
  79.     const void* MemBuffer::lock(int lockSize) const
  80.     {
  81.         const_cast<MemBuffer*>(this)->lock(lockSize);
  82.     }
  83.  
  84.     void MemBuffer::unlock() const
  85.     {
  86.         if (_handle != 0 && _lockCount > 0)
  87.         {
  88.             --_lockCount;
  89.             if (MemHandleUnlock(_handle) != 0)
  90.                 assertf(false, "Fail to unlock memory handle (%d)", _lockCount);
  91.         }
  92.     }
  93.  
  94.     bool MemBuffer::allocate(int size)
  95.     {
  96.         assert(_handle == 0);
  97.  
  98.         _handle = MemHandleNew(size);
  99.         if (_handle == 0)
  100.         {
  101.             Error::memoryAllocationError(__FILE__, __LINE__);
  102.             return false;
  103.         }
  104.  
  105.         return true;
  106.     }
  107.  
  108.     bool MemBuffer::resize(int size)
  109.     {
  110.         assert(_handle != 0);
  111.  
  112.         if (MemHandleResize(_handle, size) != 0)
  113.         {
  114.             Error::memoryAllocationError(__FILE__, __LINE__);
  115.             return false;
  116.         }
  117.  
  118.         return true;
  119.     }
  120.  
  121.     void MemBuffer::free()
  122.     {
  123.         if (_handle != 0)
  124.         {
  125.             unlockAll();
  126.  
  127.             if (MemHandleFree(_handle) != 0)
  128.                 assertf(false, "Fail to free memory", 0);
  129.  
  130.             _handle = 0;
  131.         }
  132.     }
  133.  
  134.     VoidHand MemBuffer::release()
  135.     {
  136.         unlockAll();
  137.  
  138.         VoidHand handle = _handle;
  139.         _handle = 0;
  140.  
  141.         return handle;
  142.     }
  143.  
  144.     // attributes
  145.  
  146.     int MemBuffer::size() const
  147.     {
  148.         return _handle != 0? MemHandleSize(_handle) : 0;
  149.     }
  150.  
  151.     // serialization
  152.  
  153.     void MemBuffer::serialize(DataOutputStream& stream) const
  154.     {
  155.         const void* data = lock();
  156.  
  157.         if (data != 0)
  158.         {
  159.             stream.write(size());
  160.             stream.writeData(data, size());
  161.         }
  162.         else
  163.         {
  164.             stream.write(0);
  165.         }
  166.  
  167.         unlock();
  168.     }
  169.  
  170.     void MemBuffer::restore(const DataInputStream& stream)
  171.     {
  172.         int len = stream.readAs<int>();
  173.  
  174.         if (len > 0)
  175.         {
  176.             void* data = lock(len);
  177.             if (data != 0)
  178.             {
  179.                 stream.readData(data, len);
  180.                 unlock();
  181.             }
  182.         }
  183.     }
  184.  
  185.     // implementation
  186.  
  187.     bool MemBuffer::ensureBufferSize(int newSize)
  188.     {
  189.         if (_handle == 0)
  190.         {
  191.             if (!allocate(newSize))
  192.                 return false;
  193.         }
  194.         else
  195.         {
  196.             if (size() < newSize)
  197.             {
  198.                 if (!resize(newSize))
  199.                     return false;
  200.             }
  201.         }
  202.  
  203.         return true;
  204.     }
  205.  
  206.     void MemBuffer::unlockAll() const
  207.     {
  208.         while(_lockCount > 0)
  209.             unlock();
  210.     }
  211. }
  212. // namespace Util
  213.