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

Diff for /pegasus/src/Pegasus/Common/Buffer.cpp between version 1.3.2.1 and 1.8

version 1.3.2.1, 2006/02/10 16:09:33 version 1.8, 2006/08/21 17:49:10
Line 35 
Line 35 
  
 #include <cstring> #include <cstring>
 #include "Buffer.h" #include "Buffer.h"
   #include "Pegasus/Common/InternalException.h"
  
 PEGASUS_NAMESPACE_BEGIN PEGASUS_NAMESPACE_BEGIN
  
 BufferRep Buffer::_empty_rep = { 0, 0, {0} };  //
   // Note: _empty_rep is the only BufferRep object that may have a zero capacity.
   // So "_rep->cap == 0" implies "_rep == _empty_rep". But some platforms produce
   // more than one instance of _empty_rep (strangely). Therefore, it is safer to
   // use the former test rather than the latter.
   //
   BufferRep Buffer::_empty_rep =
   {
       0, /* size */
       0, /* cap (zero implies it is the _empty_rep) */
       {0} /* data[0] */
   };
  
 static const size_t MIN_CAPACITY = 2048; static const size_t MIN_CAPACITY = 2048;
  
 static Uint32 _next_pow_2(Uint32 x) static Uint32 _next_pow_2(Uint32 x)
 { {
       // Check for potential overflow in x.
       PEGASUS_CHECK_CAPACITY_OVERFLOW(x);
   
     if (x < MIN_CAPACITY)     if (x < MIN_CAPACITY)
         return MIN_CAPACITY;         return MIN_CAPACITY;
  
Line 60 
Line 75 
  
 static inline BufferRep* _allocate(size_t cap) static inline BufferRep* _allocate(size_t cap)
 { {
     BufferRep* rep = (BufferRep*)malloc(sizeof(BufferRep) + cap);      if (cap < MIN_CAPACITY)
           cap = MIN_CAPACITY;
   
       // Allocate an extra byte for null-termination performed by getData().
       BufferRep* rep = (BufferRep*)malloc(sizeof(BufferRep) + cap + 1);
   
       if (!rep)
       {
           throw PEGASUS_STD(bad_alloc)();
       }
     rep->cap = cap;     rep->cap = cap;
     return rep;     return rep;
 } }
  
 static inline BufferRep* _reallocate(BufferRep* rep, size_t cap) static inline BufferRep* _reallocate(BufferRep* rep, size_t cap)
 { {
     rep = (BufferRep*)realloc(rep, sizeof(BufferRep) + cap);      // Allocate an extra byte for null-termination performed by getData().
       rep = (BufferRep*)realloc(rep, sizeof(BufferRep) + cap + 1);
   
       if (!rep)
       {
           throw PEGASUS_STD(bad_alloc)();
       }
     rep->cap = cap;     rep->cap = cap;
     return rep;     return rep;
 } }
Line 92 
Line 122 
     {     {
         if (x._rep->size > _rep->cap)         if (x._rep->size > _rep->cap)
         {         {
             if (_rep != &_empty_rep)              if (_rep->cap != 0)
                 free(_rep);                 free(_rep);
  
             _rep = _allocate(x._rep->cap);             _rep = _allocate(x._rep->cap);
Line 106 
Line 136 
  
 void Buffer::_reserve_aux(size_t cap) void Buffer::_reserve_aux(size_t cap)
 { {
     if (_rep == &_empty_rep)      if (_rep->cap == 0)
     {     {
         _rep = _allocate(cap);         _rep = _allocate(cap);
         _rep->size = 0;         _rep->size = 0;
Line 117 
Line 147 
  
 void Buffer::_append_char_aux() void Buffer::_append_char_aux()
 { {
     if (_rep == &_empty_rep)      if (_rep->cap == 0)
     {     {
         _rep = _allocate(MIN_CAPACITY);         _rep = _allocate(MIN_CAPACITY);
         _rep->size = 0;         _rep->size = 0;
     }     }
     else     else
       {
           // Check for potential overflow.
           PEGASUS_CHECK_CAPACITY_OVERFLOW(_rep->cap);
         _rep = _reallocate(_rep, _rep->cap ? (2 * _rep->cap) : MIN_CAPACITY);         _rep = _reallocate(_rep, _rep->cap ? (2 * _rep->cap) : MIN_CAPACITY);
 } }
   }
  
 void Buffer::insert(size_t pos, const char* data, size_t size) void Buffer::insert(size_t pos, const char* data, size_t size)
 { {
Line 143 
Line 177 
         memcpy(rep->data + pos, data, size);         memcpy(rep->data + pos, data, size);
         memcpy(rep->data + pos + size, _rep->data + pos, rem);         memcpy(rep->data + pos + size, _rep->data + pos, rem);
  
         if (_rep != &_empty_rep)          if (_rep->cap != 0)
             free(_rep);             free(_rep);
  
         _rep = rep;         _rep = rep;
Line 169 
Line 203 
     _rep->size -= size;     _rep->size -= size;
 } }
  
   size_t Buffer::appendf(const char* format, ...)
   {
       size_t size = 128;
       va_list ap;
   
       for (;;)
       {
           reserveCapacity(_rep->size + size);
           char* str = _rep->data + _rep->size;
   
           va_start(ap, format);
   
   #if defined(PEGASUS_OS_TYPE_WINDOWS)
           int n = _vsnprintf(str, size, format, ap);
   #else
           int n = vsnprintf(str, size, format, ap);
   #endif
   
           va_end(ap);
   
           if (n > -1 && n < int(size))
           {
               _rep->size += n;
               return size_t(n);
           }
   
           if (n > -1)
               size = n + 1;
           else
               size *= 2;
       }
   
       // Unreachable:
       return 0;
   }
   
 PEGASUS_NAMESPACE_END PEGASUS_NAMESPACE_END


Legend:
Removed from v.1.3.2.1  
changed lines
  Added in v.1.8

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2