Changeset 1001


Ignore:
Timestamp:
May 13, 2008 2:33:44 PM (16 years ago)
Author:
dkearney
Message:

splitting buffer from simple buffer so i can use simple buffer elsewhere

Location:
trunk/src2/core
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src2/core/Makefile.in

    r951 r1001  
    1414CXX             = @CXX@
    1515
    16 CFLAGS=-g -Wall -fPIC
     16CFLAGS=-g -Wall -pg -fPIC
    1717#CFLAGS=-O
    1818#CFLAGS=-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7
     
    4747OBJS = RpMesh1D.o RpField1D.o RpMeshTri2D.o RpMeshRect3D.o \
    4848       RpFieldRect3D.o RpMeshPrism3D.o RpFieldPrism3D.o RpSerialBuffer.o \
    49        RpSerializer.o RpSerializable.o RpBuffer.o RpEncode.o Ptr.o Outcome.o
     49       RpSerializer.o RpSerializable.o RpSimpleBuffer.o RpBuffer.o \
     50           RpEncode.o Ptr.o Outcome.o
    5051       # Lookup.o
    5152
     
    134135        $(CXX) $(CFLAGS) -o viz $^
    135136
    136 libviz.a: Outcome.o RpMesh1D.o RpField1D.o RpMeshTri2D.o RpMeshRect3D.o RpFieldRect3D.o RpMeshPrism3D.o RpFieldPrism3D.o RpSerialBuffer.o RpSerializer.o RpSerializable.o RpBuffer.o
     137libviz.a: Outcome.o RpMesh1D.o RpField1D.o RpMeshTri2D.o RpMeshRect3D.o RpFieldRect3D.o RpMeshPrism3D.o RpFieldPrism3D.o RpSerialBuffer.o RpSerializer.o RpSerializable.o RpSimpleBuffer.o RpBuffer.o
    137138        $(AR) $@ $^
    138139
     
    204205        $(CXX) $(CFLAGS) -c -o $@ -I ${includedir} RpBuffer_test.cc
    205206
     207RpSimpleBuffer.o: RpSimpleBuffer.cc RpSimpleBuffer.h
     208        $(CXX) $(CFLAGS) -c -o $@ -I ${includedir} RpSimpleBuffer.cc
  • trunk/src2/core/RpBuffer.cc

    r665 r1001  
    55 *  AUTHOR:  Derrick Kearney, Purdue University
    66 *
    7  *  Copyright (c) 2004-2007  Purdue Research Foundation
     7 *  Copyright (c) 2004-2008  Purdue Research Foundation
    88 * ----------------------------------------------------------------------
    99 *  See the file "license.terms" for information on usage and
    1010 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
    11  * ======================================================================
    12  *
    13  *  This code is based on the Tcl_DString facility included in the
    14  *  Tcl source release, which includes the following copyright:
    15  *
    16  *  Copyright (c) 1987-1993 The Regents of the University of California.
    17  *  Copyright (c) 1994-1998 Sun Microsystems, Inc.
    18  *  Copyright (c) 2001 by Kevin B. Kenny.  All rights reserved.
    19  *
    20  *  This software is copyrighted by the Regents of the University of
    21  *  California, Sun Microsystems, Inc., Scriptics Corporation,
    22  *  and other parties.  The following terms apply to all files associated
    23  *  with the software unless explicitly disclaimed in individual files.
    24  *
    25  *  The authors hereby grant permission to use, copy, modify, distribute,
    26  *  and license this software and its documentation for any purpose, provided
    27  *  that existing copyright notices are retained in all copies and that this
    28  *  notice is included verbatim in any distributions. No written agreement,
    29  *  license, or royalty fee is required for any of the authorized uses.
    30  *  Modifications to this software may be copyrighted by their authors
    31  *  and need not follow the licensing terms described here, provided that
    32  *  the new terms are clearly indicated on the first page of each file where
    33  *  they apply.
    34  *
    35  *  IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
    36  *  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
    37  *  ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
    38  *  DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
    39  *  POSSIBILITY OF SUCH DAMAGE.
    40  *
    41  *  THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
    42  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
    43  *  FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
    44  *  IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
    45  *  NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
    46  *  MODIFICATIONS.
    47  *
    48  *  GOVERNMENT USE: If you are acquiring this software on behalf of the
    49  *  U.S. government, the Government shall have only "Restricted Rights"
    50  *  in the software and related documentation as defined in the Federal·
    51  *  Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
    52  *  are acquiring the software on behalf of the Department of Defense, the
    53  *  software shall be classified as "Commercial Computer Software" and the
    54  *  Government shall have only "Restricted Rights" as defined in Clause
    55  *  252.227-7013 (c) (1) of DFARs.  Notwithstanding the foregoing, the
    56  *  authors grant the U.S. Government and others acting in its behalf
    57  *  permission to use and distribute the software in accordance with the
    58  *  terms specified in this license.·
    59  *
    6011 * ======================================================================
    6112 */
     
    7627
    7728/**
    78  * Construct an empty SimpleBuffer.
    79  */
    80 SimpleBuffer::SimpleBuffer()
    81 {
    82     bufferInit();
    83 }
    84 
    85 
    86 /**
    87  * Construct a SimpleBuffer loaded with initial data.
    88  *
    89  * @param bytes pointer to bytes being stored.
    90  * @param nbytes number of bytes being stored.
    91  */
    92 SimpleBuffer::SimpleBuffer(const char* bytes, int nbytes)
    93 {
    94     bufferInit();
    95     append(bytes,nbytes);
    96 }
    97 
    98 
    99 /**
    100  * Copy constructor
    101  * @param SimpleBuffer object to copy
    102  */
    103 SimpleBuffer::SimpleBuffer(const SimpleBuffer& b)
    104 {
    105     bufferInit();
    106     append(b.bytes(),b.size());
    107 }
    108 
    109 
    110 /**
    111  * Assignment operator
    112  * @param SimpleBuffer object to copy
    113  */
    114 SimpleBuffer&
    115 SimpleBuffer::operator=(const SimpleBuffer& b)
    116 {
    117     bufferFree();
    118     bufferInit();
    119     append(b.bytes(),b.size());
    120     return *this;
    121 }
    122 
    123 
    124 /**
    125  * Operator +
    126  * @param SimpleBuffer object to add
    127  */
    128 SimpleBuffer
    129 SimpleBuffer::operator+(const SimpleBuffer& b) const
    130 {
    131     SimpleBuffer newBuffer(*this);
    132     newBuffer.operator+=(b);
    133     return newBuffer;
    134 }
    135 
    136 
    137 /**
    138  * Operator +=
    139  * @param SimpleBuffer object to add
    140  */
    141 SimpleBuffer&
    142 SimpleBuffer::operator+=(const SimpleBuffer& b)
    143 {
    144     append(b.bytes(),b.size());
    145     return *this;
    146 }
    147 
    148 
    149 /**
    150  * Destructor
    151  */
    152 SimpleBuffer::~SimpleBuffer()
    153 {
    154     bufferFree();
    155 }
    156 
    157 
    158 /**
    159  * Get the bytes currently stored in the buffer.  These bytes can
    160  * be stored, and used later to construct another Buffer to
    161  * decode the information.
    162  *
    163  * @return Pointer to the bytes in the buffer.
    164  */
    165 const char*
    166 SimpleBuffer::bytes() const
    167 {
    168     return _buf;
    169 }
    170 
    171 
    172 /**
    173  * Get the number of bytes currently stored in the buffer.
    174  * @return Number of the bytes used in the buffer.
    175  */
    176 unsigned int
    177 SimpleBuffer::size() const
    178 {
    179     return _size;
    180 }
    181 
    182 
    183 /**
    184  * Clear the buffer, making it empty.
    185  * @return Reference to this buffer.
    186  */
    187 SimpleBuffer&
    188 SimpleBuffer::clear()
    189 {
    190     bufferFree();
    191     bufferInit();
    192 
    193     return *this;
    194 }
    195 
    196 
    197 /**
    198  * Append bytes to the end of this buffer
    199  * @param Buffer object to copy
    200  * @return Reference to this buffer.
    201  */
    202 int
    203 SimpleBuffer::append(const char* bytes, int nbytes)
    204 {
    205     unsigned int newSize = 0;
    206     char *newBuffer = NULL;
    207 
    208     // User specified NULL buffer to append
    209     if ( (bytes == NULL) && (nbytes < 1) ) {
    210         return 0;
    211     }
    212 
    213     if (nbytes == -1) {
    214         // user signaled null terminated string
    215         nbytes = strlen(bytes);
    216     }
    217 
    218     if (nbytes <= 0) {
    219         // no data written, invalid option
    220         return nbytes;
    221     }
    222 
    223     // Empty internal buffer, make sure its properly initialized.
    224     if (_buf == NULL) {
    225         bufferInit();
    226     }
    227 
    228     newSize = (unsigned int)(_size + nbytes);
    229 
    230     // ensure that our smallest buffer is 200 bytes
    231     if (newSize < (RP_BUFFER_MIN_SIZE/2)) {
    232         newSize = (RP_BUFFER_MIN_SIZE/2);
    233     }
    234 
    235     /*
    236      * Allocate a larger buffer for the string if the current one isn't
    237      * large enough. Allocate extra space in the new buffer so that there
    238      * will be room to grow before we have to allocate again.
    239      */
    240 
    241     if (newSize >= _spaceAvl) {
    242         _spaceAvl = newSize * 2;
    243         newBuffer = new char[_spaceAvl];
    244         if (newBuffer == NULL) {
    245             // return memory error
    246             return -1;
    247         }
    248         if (_buf != NULL) {
    249             memcpy((void*) newBuffer, (void*) _buf, (size_t) _size);
    250             delete [] _buf;
    251             _buf = NULL;
    252         }
    253         _buf = newBuffer;
    254     }
    255 
    256     memcpy((void*) (_buf + _size), (void*) bytes, (size_t) nbytes);
    257 
    258     _size = _size + (unsigned int) nbytes;
    259 
    260     return nbytes;
    261 }
    262 
    263 
    264 /**
    265  * Read data from the buffer into a memory location provided by caller
    266  * @param Pointer locating where to place read bytes.
    267  * @param Size of the memory location.
    268  * @return Number of bytes written to memory location
    269  */
    270 int
    271 SimpleBuffer::read(const char* bytes, int nbytes)
    272 {
    273     unsigned int bytesRead = 0;
    274 
    275     // SimpleBuffer is empty.
    276     if (_buf == NULL) {
    277         return 0;
    278     }
    279 
    280     // User specified NULL buffer.
    281     if (bytes == NULL) {
    282         return 0;
    283     }
    284 
    285     // User specified negative buffer size
    286     if (nbytes <= 0) {
    287         return 0;
    288     }
    289 
    290     // make sure we don't read off the end of our buffer
    291     if ( (_pos + nbytes) >= _size ) {
    292         bytesRead = (_size - _pos);
    293     }
    294     else {
    295         bytesRead = (unsigned int) nbytes;
    296     }
    297 
    298     if (bytesRead <= 0) {
    299         return 0;
    300     }
    301 
    302     if (bytesRead > 0) {
    303         memcpy((void*) bytes, (void*) (_buf+_pos), (size_t) bytesRead);
    304     }
    305 
    306     _pos = (_pos + bytesRead);
    307 
    308     return (int)bytesRead;
    309 }
    310 
    311 
    312 /**
    313  * Set buffer position indicator to spot within the buffer
    314  * @param Offset from whence location in buffer.
    315  * @param Place from where offset is added or subtracted.
    316  * @return 0 on success, anything else is failure
    317  */
    318 int
    319 SimpleBuffer::seek(int offset, int whence)
    320 {
    321     int retVal = 0;
    322 
    323     if (_buf == NULL) {
    324         return -1 ;
    325     }
    326 
    327     if (whence == SEEK_SET) {
    328         if (offset < 0) {
    329             /* dont go off the beginning of data */
    330             _pos = 0;
    331         }
    332         else if (offset >= (int)_size) {
    333             /* dont go off the end of data */
    334             _pos = _size - 1;
    335         }
    336         else {
    337             _pos = (unsigned int)(_pos + offset);
    338         }
    339     }
    340     else if (whence == SEEK_CUR) {
    341         if ( (_pos + offset) < 0) {
    342             /* dont go off the beginning of data */
    343             _pos = 0;
    344         }
    345         else if ((_pos + offset) >= _size) {
    346             /* dont go off the end of data */
    347             _pos = _size - 1;
    348         }
    349         else {
    350             _pos = (unsigned int)(_pos + offset);
    351         }
    352     }
    353     else if (whence == SEEK_END) {
    354         if (offset <= (-1*((int)_size))) {
    355             /* dont go off the beginning of data */
    356             _pos = 0;
    357         }
    358         else if (offset >= 0) {
    359             /* dont go off the end of data */
    360             _pos = _size - 1;
    361         }
    362         else {
    363             _pos = (unsigned int)((_size - 1) + offset);
    364         }
    365     }
    366     else {
    367         retVal = -1;
    368     }
    369 
    370     return retVal;
    371 }
    372 
    373 
    374 /**
    375  * Tell caller the offset of the position indicator from the start of buffer
    376  * @return Number of bytes the position indicator is from start of buffer
    377  */
    378 int
    379 SimpleBuffer::tell()
    380 {
    381    return (int)_pos;
    382 }
    383 
    384 
    385 /**
    386  * Read data from the buffer into a memory location provided by caller
    387  */
    388 SimpleBuffer&
    389 SimpleBuffer::rewind()
    390 {
    391     _pos = 0;
    392     return *this;
    393 }
    394 
    395 
    396 /**
    397  * Tell if the last file like operation (ie. read()) was successful
    398  * or if there was a failure like eof, or bad memory
    399  * @return True or false boolean value
    400  */
    401 bool
    402 SimpleBuffer::good() const
    403 {
    404     return (_fileState);
    405 }
    406 
    407 
    408 /**
    409  * Tell if the last file like operation (ie. read()) failed
    410  * Opposite of good()
    411  * @return True or false boolean value
    412  */
    413 bool
    414 SimpleBuffer::bad() const
    415 {
    416     return (!_fileState);
    417 }
    418 
    419 
    420 /**
    421  * Tell if the position flag is at the end of the buffer
    422  * @return True or false boolean value
    423  */
    424 bool
    425 SimpleBuffer::eof() const
    426 {
    427     return (_pos >= _size);
    428 }
    429 
    430 
    431 /**
    432  * Move the data from this SimpleBuffer to the SimpleBuffer provided by
    433  * the caller. All data except the _pos is moved and this SimpleBuffer is
    434  * re-initialized with bufferInit().
    435  * @param SimpleBuffer to move the information to
    436  * @return reference to this SimpleBuffer object.
    437  */
    438 SimpleBuffer&
    439 SimpleBuffer::move(SimpleBuffer& b)
    440 {
    441     bufferFree();
    442 
    443     _buf = b._buf;
    444     _pos = b._pos;
    445     _size = b._size;
    446     _spaceAvl = b._spaceAvl;
    447     _fileState = b._fileState;
    448 
    449     b.bufferInit();
    450 
    451     return *this;
    452 }
    453 
    454 
    455  /**
    456   *  Initializes a dynamic buffer, discarding any previous contents
    457   *  of the buffer. bufferFree() should have been called already
    458   *  if the dynamic buffer was previously in use.
    459   */
    460 void
    461 SimpleBuffer::bufferInit()
    462 {
    463     _buf = NULL;
    464     _pos = 0;
    465     _size = 0;
    466     _spaceAvl = 0;
    467     _fileState = true;
    468 }
    469 
    470 
    471 /**
    472  *  Frees up any memory allocated for the dynamic buffer and
    473  *  reinitializes the buffer to an empty state.
    474  */
    475 void
    476 SimpleBuffer::bufferFree()
    477 {
    478     if (_buf != NULL) {
    479         delete [] _buf;
    480         _buf = NULL;
    481     }
    482     bufferInit();
    483 }
    484 
    485 
    486 /**
    48729 * Construct an empty Buffer.
    48830 */
    48931Buffer::Buffer()
    49032  : SimpleBuffer(),
     33    _level(6),
     34    _compressionType(RPCOMPRESS_GZIP),
     35    _windowBits(15)
     36{}
     37
     38
     39/**
     40 * Construct an empty Buffer of specified size.
     41 */
     42Buffer::Buffer(int nbytes)
     43  : SimpleBuffer(nbytes),
    49144    _level(6),
    49245    _compressionType(RPCOMPRESS_GZIP),
  • trunk/src2/core/RpBuffer.h

    r657 r1001  
    55 *  AUTHOR:  Derrick Kearney, Purdue University
    66 *
    7  *  Copyright (c) 2004-2007  Purdue Research Foundation
     7 *  Copyright (c) 2004-2008  Purdue Research Foundation
    88 * ----------------------------------------------------------------------
    99 *  See the file "license.terms" for information on usage and
    1010 *  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
    11  * ======================================================================
    12  *
    13  *  This code is based on the Tcl_DString facility included in the
    14  *  Tcl source release, which includes the following copyright:
    15  *
    16  *  Copyright (c) 1987-1993 The Regents of the University of California.
    17  *  Copyright (c) 1994-1998 Sun Microsystems, Inc.
    18  *  Copyright (c) 2001 by Kevin B. Kenny.  All rights reserved.
    19  *
    20  *  This software is copyrighted by the Regents of the University of
    21  *  California, Sun Microsystems, Inc., Scriptics Corporation,
    22  *  and other parties.  The following terms apply to all files associated
    23  *  with the software unless explicitly disclaimed in individual files.
    24  *
    25  *  The authors hereby grant permission to use, copy, modify, distribute,
    26  *  and license this software and its documentation for any purpose, provided
    27  *  that existing copyright notices are retained in all copies and that this
    28  *  notice is included verbatim in any distributions. No written agreement,
    29  *  license, or royalty fee is required for any of the authorized uses.
    30  *  Modifications to this software may be copyrighted by their authors
    31  *  and need not follow the licensing terms described here, provided that
    32  *  the new terms are clearly indicated on the first page of each file where
    33  *  they apply.
    34  *
    35  *  IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
    36  *  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
    37  *  ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
    38  *  DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
    39  *  POSSIBILITY OF SUCH DAMAGE.
    40  *
    41  *  THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
    42  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
    43  *  FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
    44  *  IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
    45  *  NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
    46  *  MODIFICATIONS.
    47  *
    48  *  GOVERNMENT USE: If you are acquiring this software on behalf of the
    49  *  U.S. government, the Government shall have only "Restricted Rights"
    50  *  in the software and related documentation as defined in the Federal·
    51  *  Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
    52  *  are acquiring the software on behalf of the Department of Defense, the
    53  *  software shall be classified as "Commercial Computer Software" and the
    54  *  Government shall have only "Restricted Rights" as defined in Clause
    55  *  252.227-7013 (c) (1) of DFARs.  Notwithstanding the foregoing, the
    56  *  authors grant the U.S. Government and others acting in its behalf
    57  *  permission to use and distribute the software in accordance with the
    58  *  terms specified in this license.·
    59  *
    6011 * ======================================================================
    6112 */
     
    6415#define RAPPTURE_BUFFER_H
    6516
    66 #define RP_BUFFER_MIN_SIZE    200
    67 
    6817#include "Outcome.h"
     18#include "RpSimpleBuffer.h"
    6919
    7020#ifdef __cplusplus
     
    7929namespace Rappture {
    8030
    81 class SimpleBuffer {
    82 public:
    83     SimpleBuffer();
    84     SimpleBuffer(const char* bytes, int nbytes=-1);
    85     SimpleBuffer(const SimpleBuffer& b);
    86     SimpleBuffer& operator=(const SimpleBuffer& b);
    87     SimpleBuffer  operator+(const SimpleBuffer& b) const;
    88     SimpleBuffer& operator+=(const SimpleBuffer& b);
    89     virtual ~SimpleBuffer();
    90 
    91     const char* bytes() const;
    92     unsigned int size() const;
    93 
    94     SimpleBuffer& clear();
    95     int append(const char* bytes, int nbytes=-1);
    96     int read(const char* bytes, int nbytes);
    97     int seek(int offset, int whence);
    98     int tell();
    99     SimpleBuffer& rewind();
    100 
    101     bool good() const;
    102     bool bad() const;
    103     bool eof() const;
    104 
    105     SimpleBuffer& move(SimpleBuffer& b);
    106 
    107 protected:
    108 
    109     void bufferInit();
    110     void bufferFree();
    111 
    112 private:
    113 
    114     /// Pointer to the memory that holds our buffer's data
    115     char* _buf;
    116 
    117     /// Position offset within the buffer's memory
    118     unsigned int _pos;
    119 
    120     /// Size of the used memory in the buffer
    121     unsigned int _size;
    122 
    123     /// Total space available in the buffer
    124     unsigned int _spaceAvl;
    125 
    126     /// State of the last file like operation.
    127     bool _fileState;
    128 };
    12931
    13032/**
     
    13840public:
    13941    Buffer();
     42    Buffer(int nbytes);
    14043    Buffer(const char* bytes, int nbytes=-1);
    14144    Buffer(const Buffer& buffer);
Note: See TracChangeset for help on using the changeset viewer.