home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Source / OBuffer.cpp < prev    next >
C/C++ Source or Header  |  1996-08-12  |  4KB  |  155 lines

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1995
  3. // All Rights Reserved
  4. // OBuffer.cpp
  5.  
  6. /*
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  13.  *    endorse or promote products derived from this software
  14.  *    without specific prior written permission.
  15.  * 3. See OCL.INF for a detailed copyright notice.
  16.  *
  17.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  18.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27.  * SUCH DAMAGE.
  28.  */
  29.  
  30. // $Header: W:/Projects/OCL/Source/rcs/OBuffer.cpp 1.50 1996/08/11 23:49:08 B.STEIN Release $
  31.  
  32. #define __OCL_SOURCE__
  33.  
  34. #define OINCL_OSTRING
  35. #define OINCL_BASE
  36.  
  37. #include <ocl.hpp>
  38. #include <OBuffer.hpp>
  39. #include <OMessage.hpp>
  40.  
  41. OBuffer::OBuffer(const ULONG sizeInBytes,
  42.                  const ULONG flags)
  43.   : memPtr    (NULL),
  44.     memSize   (sizeInBytes),
  45.     allocSize (0)
  46. {
  47.  APIRET rc;
  48.  
  49.  if ((flags & OBuffer::TILED) &&
  50.      (sizeInBytes > OBuffer::TILESIZE))
  51.    throw OVioException(OCL::error(20), 0);
  52.  
  53.  if (memSize < OBuffer::PAGESIZE)
  54.    throw OVioException(OCL::error(21), 0);
  55.  
  56.  if (flags & OBuffer::COMMIT)
  57.    allocSize = memSize;
  58.  
  59.  rc = DosAllocMem((PPVOID) &memPtr,
  60.                   memSize,
  61.                   flags);
  62.  if (rc != 0) 
  63.    throw OVioException(OMessage(rc, "OSO001.MSG", OCL::error(22)), rc);
  64. }
  65.  
  66.  
  67. OBuffer::~OBuffer()
  68. {
  69.  freeBuffer();
  70. }
  71.  
  72.  
  73. void OBuffer::commit(const ULONG sizeInBytes)
  74. {
  75.  APIRET rc = 0;
  76.  
  77.  if (!memPtr)
  78.    throw OVioException("OBuffer, invalid object.", 0, OException::fatal);
  79.  
  80.  if (sizeInBytes < OBuffer::PAGESIZE)
  81.    throw OVioException("At least OBuffer::PAGESIZE must be requested.", 0);
  82.  
  83.  if ((sizeInBytes + allocSize) > memSize)
  84.    throw OVioException("OBuffer is smaller than requested commit.", 0);
  85.  
  86.  rc = DosSetMem(memPtr, sizeInBytes, OBuffer::COMMIT | OBuffer::DEFAULT);
  87.  
  88.  if (rc != 0) 
  89.    throw OVioException("DosSetMem failed.", rc);
  90.  
  91.  allocSize += sizeInBytes;
  92. }
  93.  
  94.  
  95.  
  96. void OBuffer::decommit(const ULONG sizeInBytes)
  97. {
  98.  APIRET rc = 0;
  99.  
  100.  if (!memPtr)
  101.    throw OVioException("OBuffer, invalid object.", 0, OException::fatal);
  102.  
  103.  if (sizeInBytes < OBuffer::PAGESIZE)
  104.    throw OVioException("At least OBuffer::PAGESIZE must be requested.", 0);
  105.  
  106.  if (sizeInBytes > allocSize)
  107.    throw OVioException("OBuffer is smaller than requested decommit.", 0);
  108.  
  109.  rc = DosSetMem(memPtr, sizeInBytes, OBuffer::DECOMMIT);
  110.  
  111.  if (rc != 0) 
  112.    throw OVioException("DosSetMem failed.", rc);
  113.  
  114.  allocSize -= sizeInBytes;
  115. }
  116.  
  117.  
  118. void OBuffer::freeBuffer()
  119. {
  120.  APIRET rc = 0;
  121.  
  122.  if (memPtr != NULL)
  123.    rc = DosFreeMem(memPtr);
  124.  
  125.  if (rc != 0)
  126.    throw OVioException("DosFreeMem failed.", rc);
  127. }
  128.   
  129.  
  130. ULONG OBuffer::allocated()
  131. {
  132.  return(memSize);
  133. }
  134.  
  135.  
  136. ULONG OBuffer::committed()
  137. {
  138.  return(allocSize);
  139. }
  140.  
  141.  
  142. PBYTE OBuffer::getPtr()
  143. {
  144.  return(memPtr);
  145. }
  146.  
  147.  
  148. PSZ OBuffer::isOfType() const 
  149. {
  150.  return("OBuffer");
  151. }
  152.  
  153.  
  154. // end of source
  155.