📄 kwqarrayimpl.cpp
字号:
/*
* Copyright (C) 2004 Apple Computer, Inc. All rights reserved.
* Portions Copyright (c) 2005 Nokia Corporation, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "KWQArrayImpl.h"
#include <string.h>
#define MIN(a,b) (((a)<(b))?(a):(b))
KWQArrayImpl::KWQArrayPrivate::KWQArrayPrivate(size_t pItemSize, size_t pNumItems) :
numItems(pNumItems),
itemSize(pItemSize),
#ifndef __OOM__
data(pNumItems > 0 ? new char[itemSize * numItems] : NULL),
#else
data(pNumItems > 0 ? (char*)MemoryManager::Calloc( numItems, itemSize ) : NULL ),
#endif
refCount(0)
{
}
KWQArrayImpl::KWQArrayPrivate::~KWQArrayPrivate()
{
#ifndef __OOM__
delete[] data;
#else
MemoryManager::Free( data );
#endif
}
KWQArrayImpl::KWQArrayImpl(size_t itemSize, size_t numItems) :
d(new KWQArrayPrivate(itemSize, numItems))
{
}
KWQArrayImpl::KWQArrayImpl(const KWQArrayImpl &a) :
d(a.d)
{
}
KWQArrayImpl::~KWQArrayImpl()
{
}
KWQArrayImpl &KWQArrayImpl::operator=(const KWQArrayImpl &a)
{
d = a.d;
return *this;
}
void *KWQArrayImpl::data() const
{
return d->data;
}
uint KWQArrayImpl::size() const
{
return d->numItems;
}
bool KWQArrayImpl::resize(size_t newSize)
{
if (newSize != d->numItems) {
char *newData;
#ifndef __OOM__
if (newSize != 0) {
newData = new char[newSize * d->itemSize];
if (newData == NULL) {
return false;
}
} else {
newData = NULL;
}
memcpy(newData, d->data, MIN(newSize, d->numItems) * d->itemSize);
delete[] d->data;
#else
// this function is used in a number of places, and we only check large allocations.
unsigned int len = newSize * d->itemSize;
if( len > (1<<12) ) {
OOM_PRE_CHECK( len, len, "KWQArrayImpl::resize" )
newData = (char*)MemoryManager::ReAlloc( d->data, len );
if( newData ) {
d->data = newData;
d->numItems = newSize;
}
OOM_POST_CHECK_FAILED(return false;)
} else
newData = (char*)MemoryManager::ReAlloc( d->data, len );
if (!newData)
return false;
#endif
d->data = newData;
d->numItems = newSize;
}
return true;
}
void KWQArrayImpl::duplicate(const void *data, size_t newSize)
{
if (data == NULL) {
newSize = 0;
}
if (d->refCount > 1) {
d = KWQRefPtr<KWQArrayPrivate>(new KWQArrayPrivate(d->itemSize, newSize));
}
if (d->numItems != newSize) {
resize(newSize);
}
if (d->data == data)
return;
memcpy(d->data, data, newSize * d->itemSize);
}
void KWQArrayImpl::detach()
{
if (d->refCount > 1) {
duplicate(d->data, d->numItems);
}
}
bool KWQArrayImpl::fill(const void *item, int numItems)
{
if (numItems == -1) {
numItems = d->numItems;
}
if ((unsigned)numItems != d->numItems) {
if (!resize(numItems)) {
return false;
}
}
for (int i = 0; i < numItems; i++) {
memcpy(&d->data[i * d->itemSize], item, d->itemSize);
}
return true;
}
bool KWQArrayImpl::operator==(const KWQArrayImpl &a) const
{
return d->numItems == a.d->numItems && d->itemSize == d->itemSize
&& (d->data == a.d->data || memcmp(d->data, a.d->data, d->itemSize*d->numItems) == 0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -