version 1.11, 2006/11/30 13:25:45
|
version 1.14, 2008/12/01 17:49:47
|
|
|
//%2006//////////////////////////////////////////////////////////////////////// |
//%LICENSE//////////////////////////////////////////////////////////////// |
// | // |
// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development |
// Licensed to The Open Group (TOG) under one or more contributor license |
// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems. |
// agreements. Refer to the OpenPegasusNOTICE.txt file distributed with |
// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.; |
// this work for additional information regarding copyright ownership. |
// IBM Corp.; EMC Corporation, The Open Group. |
// Each contributor licenses this file to you under the OpenPegasus Open |
// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.; |
// Source License; you may not use this file except in compliance with the |
// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group. |
// License. |
// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.; |
// |
// EMC Corporation; VERITAS Software Corporation; The Open Group. |
// Permission is hereby granted, free of charge, to any person obtaining a |
// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.; |
// copy of this software and associated documentation files (the "Software"), |
// EMC Corporation; Symantec Corporation; The Open Group. |
// to deal in the Software without restriction, including without limitation |
// |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
// and/or sell copies of the Software, and to permit persons to whom the |
// of this software and associated documentation files (the "Software"), to |
// Software is furnished to do so, subject to the following conditions: |
// deal in the Software without restriction, including without limitation the |
// |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
// The above copyright notice and this permission notice shall be included |
// sell copies of the Software, and to permit persons to whom the Software is |
// in all copies or substantial portions of the Software. |
// furnished to do so, subject to the following conditions: |
// |
// |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
// THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN |
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT |
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
// | // |
//============================================================================== |
////////////////////////////////////////////////////////////////////////// |
// | // |
//%///////////////////////////////////////////////////////////////////////////// | //%///////////////////////////////////////////////////////////////////////////// |
| |
|
|
{0} /* data[0] */ | {0} /* data[0] */ |
}; | }; |
| |
static const size_t MIN_CAPACITY = 2048; |
static Uint32 _next_pow_2(Uint32 x, Uint32 minCap) |
|
|
static Uint32 _next_pow_2(Uint32 x) |
|
{ | { |
// Check for potential overflow in x. | // Check for potential overflow in x. |
PEGASUS_CHECK_CAPACITY_OVERFLOW(x); | PEGASUS_CHECK_CAPACITY_OVERFLOW(x); |
| |
if (x < MIN_CAPACITY) |
if (x < minCap) |
return MIN_CAPACITY; |
return minCap; |
| |
x--; | x--; |
x |= (x >> 1); | x |= (x >> 1); |
|
|
return x; | return x; |
} | } |
| |
static inline BufferRep* _allocate(size_t cap) |
static inline BufferRep* _allocate(Uint32 cap, Uint32 minCap) |
{ | { |
if (cap < MIN_CAPACITY) |
if (cap < minCap) |
cap = MIN_CAPACITY; |
cap = minCap; |
| |
// Allocate an extra byte for null-termination performed by getData(). | // Allocate an extra byte for null-termination performed by getData(). |
BufferRep* rep = (BufferRep*)malloc(sizeof(BufferRep) + cap + 1); | BufferRep* rep = (BufferRep*)malloc(sizeof(BufferRep) + cap + 1); |
|
|
return rep; | return rep; |
} | } |
| |
static inline BufferRep* _reallocate(BufferRep* rep, size_t cap) |
static inline BufferRep* _reallocate(BufferRep* rep, Uint32 cap) |
{ | { |
// Allocate an extra byte for null-termination performed by getData(). | // Allocate an extra byte for null-termination performed by getData(). |
rep = (BufferRep*)realloc(rep, sizeof(BufferRep) + cap + 1); | rep = (BufferRep*)realloc(rep, sizeof(BufferRep) + cap + 1); |
|
|
| |
Buffer::Buffer(const Buffer& x) | Buffer::Buffer(const Buffer& x) |
{ | { |
_rep = _allocate(x._rep->cap); |
_rep = _allocate(x._rep->cap, x._minCap); |
memcpy(_rep->data, x._rep->data, x._rep->size); | memcpy(_rep->data, x._rep->data, x._rep->size); |
_rep->size = x._rep->size; | _rep->size = x._rep->size; |
|
_minCap=x._minCap; |
} | } |
| |
Buffer::Buffer(const char* data, size_t size) |
Buffer::Buffer(const char* data, Uint32 size, Uint32 minCap): _minCap(minCap) |
{ | { |
_rep = _allocate(size); |
_rep = _allocate(size, _minCap); |
_rep->size = size; | _rep->size = size; |
memcpy(_rep->data, data, size); | memcpy(_rep->data, data, size); |
} | } |
|
|
if (_rep->cap != 0) | if (_rep->cap != 0) |
free(_rep); | free(_rep); |
| |
_rep = _allocate(x._rep->cap); |
_rep = _allocate(x._rep->cap, x._minCap); |
} | } |
| |
memcpy(_rep->data, x._rep->data, x._rep->size); | memcpy(_rep->data, x._rep->data, x._rep->size); |
_rep->size = x._rep->size; | _rep->size = x._rep->size; |
|
_minCap = x._minCap; |
} | } |
return *this; | return *this; |
} | } |
| |
void Buffer::_reserve_aux(size_t cap) |
void Buffer::_reserve_aux(Uint32 cap) |
{ | { |
if (_rep->cap == 0) | if (_rep->cap == 0) |
{ | { |
_rep = _allocate(cap); |
_rep = _allocate(cap, _minCap); |
_rep->size = 0; | _rep->size = 0; |
} | } |
else | else |
_rep = _reallocate(_rep, _next_pow_2( (Uint32)cap)); |
_rep = _reallocate(_rep, _next_pow_2(cap, _minCap)); |
} | } |
| |
void Buffer::_append_char_aux() | void Buffer::_append_char_aux() |
{ | { |
if (_rep->cap == 0) | if (_rep->cap == 0) |
{ | { |
_rep = _allocate(MIN_CAPACITY); |
_rep = _allocate(_minCap, _minCap); |
_rep->size = 0; | _rep->size = 0; |
} | } |
else | else |
{ | { |
// Check for potential overflow. | // Check for potential overflow. |
PEGASUS_CHECK_CAPACITY_OVERFLOW(_rep->cap); | PEGASUS_CHECK_CAPACITY_OVERFLOW(_rep->cap); |
_rep = _reallocate(_rep, _rep->cap ? (2 * _rep->cap) : MIN_CAPACITY); |
_rep = _reallocate(_rep, _rep->cap ? (2 * _rep->cap) : _minCap); |
} | } |
} | } |
| |
void Buffer::insert(size_t pos, const char* data, size_t size) |
void Buffer::insert(Uint32 pos, const char* data, Uint32 size) |
{ | { |
if (pos > _rep->size) | if (pos > _rep->size) |
return; | return; |
| |
size_t cap = _rep->size + size; |
Uint32 cap = _rep->size + size; |
size_t rem = _rep->size - pos; |
Uint32 rem = _rep->size - pos; |
| |
if (cap > _rep->cap) | if (cap > _rep->cap) |
{ | { |
BufferRep* rep = _allocate(cap); |
BufferRep* rep = _allocate(cap, _minCap); |
rep->size = cap; | rep->size = cap; |
| |
memcpy(rep->data, _rep->data, pos); | memcpy(rep->data, _rep->data, pos); |
|
|
} | } |
} | } |
| |
void Buffer::remove(size_t pos, size_t size) |
void Buffer::remove(Uint32 pos, Uint32 size) |
{ | { |
if (pos + size > _rep->size) | if (pos + size > _rep->size) |
return; | return; |
| |
size_t rem = _rep->size - (pos + size); |
Uint32 rem = _rep->size - (pos + size); |
| |
if (rem) | if (rem) |
memmove(_rep->data + pos, _rep->data + pos + size, rem); | memmove(_rep->data + pos, _rep->data + pos + size, rem); |