(file) Return to Buffer.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 mike  1.2 //%2005////////////////////////////////////////////////////////////////////////
  2           //
  3           // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4           // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5           // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6           // IBM Corp.; EMC Corporation, The Open Group.
  7           // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8           // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9           // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10           // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11           //
 12           // Permission is hereby granted, free of charge, to any person obtaining a copy
 13           // of this software and associated documentation files (the "Software"), to
 14           // deal in the Software without restriction, including without limitation the
 15           // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 16           // sell copies of the Software, and to permit persons to whom the Software is
 17           // furnished to do so, subject to the following conditions:
 18           // 
 19           // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 20           // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 21           // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 22 mike  1.2 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 23           // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 24           // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 25           // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 26           // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27           //
 28           //==============================================================================
 29           //
 30           // Author: Michael E. Brasher (mike-brasher@austin.rr.com -- Inova Europe)
 31           //
 32           //%/////////////////////////////////////////////////////////////////////////////
 33           
 34           #include <cstring>
 35           #include "Buffer.h"
 36           
 37           PEGASUS_NAMESPACE_BEGIN
 38           
 39           BufferRep Buffer::_empty_rep = { 0, 0, {0} };
 40           
 41 mike  1.3 static const size_t MIN_CAPACITY = 2048;
 42 mike  1.2 
 43           static Uint32 _next_pow_2(Uint32 x)
 44           {
 45               if (x < MIN_CAPACITY)
 46           	return MIN_CAPACITY;
 47           
 48               x--;
 49               x |= (x >> 1);
 50               x |= (x >> 2);
 51               x |= (x >> 4);
 52               x |= (x >> 8);
 53               x |= (x >> 16);
 54               x++;
 55           
 56               return x;
 57           }
 58           
 59           static inline BufferRep* _allocate(size_t cap)
 60           {
 61               BufferRep* rep = (BufferRep*)malloc(sizeof(BufferRep) + cap);
 62               rep->cap = cap;
 63 mike  1.2     return rep;
 64           }
 65           
 66           static inline BufferRep* _reallocate(BufferRep* rep, size_t cap)
 67           {
 68               rep = (BufferRep*)realloc(rep, sizeof(BufferRep) + cap);
 69               rep->cap = cap;
 70               return rep;
 71           }
 72           
 73           Buffer::Buffer(const Buffer& x)
 74           {
 75               _rep = _allocate(x._rep->cap);
 76               memcpy(_rep->data, x._rep->data, x._rep->size);
 77               _rep->size = x._rep->size;
 78           }
 79           
 80           Buffer::Buffer(const char* data, size_t size)
 81           {
 82               _rep = _allocate(size);
 83               _rep->size = size;
 84 mike  1.2     memcpy(_rep->data, data, size);
 85           }
 86           
 87           Buffer& Buffer::operator=(const Buffer& x)
 88           {
 89               if (&x != this)
 90               {
 91           	if (x._rep->size > _rep->cap)
 92           	{
 93           	    if (_rep != &_empty_rep)
 94           		free(_rep);
 95           
 96           	    _rep = _allocate(x._rep->cap);
 97           	}
 98           
 99           	memcpy(_rep->data, x._rep->data, x._rep->size);
100           	_rep->size = x._rep->size;
101               }
102               return *this;
103           }
104           
105 mike  1.2 void Buffer::_reserve_aux(size_t cap)
106           {
107               if (_rep == &_empty_rep)
108               {
109           	_rep = _allocate(cap);
110           	_rep->size = 0;
111               }
112               else
113           	_rep = _reallocate(_rep, _next_pow_2(cap));
114           }
115           
116           void Buffer::_append_char_aux()
117           {
118               if (_rep == &_empty_rep)
119               {
120           	_rep = _allocate(MIN_CAPACITY);
121           	_rep->size = 0;
122               }
123               else
124           	_rep = _reallocate(_rep, _rep->cap ? (2 * _rep->cap) : MIN_CAPACITY);
125           }
126 mike  1.2 
127           void Buffer::insert(size_t pos, const char* data, size_t size)
128           {
129               if (pos > _rep->size)
130           	return;
131           
132               size_t cap = _rep->size + size;	
133               size_t rem = _rep->size - pos;
134           
135               if (cap > _rep->cap)
136               {
137           	BufferRep* rep = _allocate(cap);
138           	rep->size = cap;
139           
140           	memcpy(rep->data, _rep->data, pos);
141           	memcpy(rep->data + pos, data, size);
142           	memcpy(rep->data + pos + size, _rep->data + pos, rem);
143           
144           	if (_rep != &_empty_rep)
145           	    free(_rep);
146           
147 mike  1.2 	_rep = rep;
148               }
149               else
150               {
151                   memmove(_rep->data + pos + size, _rep->data + pos, rem);
152           	memcpy(_rep->data + pos, data, size);
153           	_rep->size += size;
154               }
155           }
156           
157           void Buffer::remove(size_t pos, size_t size)
158           {
159               if (pos + size > _rep->size)
160           	return;
161           
162               size_t rem = _rep->size - (pos + size);
163           
164               if (rem)
165           	memmove(_rep->data + pos, _rep->data + pos + size, rem);
166           
167               _rep->size -= size;
168 mike  1.2 }
169           
170           PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2