2004-03-13 22:06:53 +08:00
|
|
|
/*
|
|
|
|
* ZoneMinder flexible memory class implementation, $Date$, $Revision$
|
2008-07-25 17:33:23 +08:00
|
|
|
* Copyright (C) 2001-2008 Philip Coombes
|
2004-03-13 22:06:53 +08:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "zm.h"
|
|
|
|
#include "zm_buffer.h"
|
|
|
|
|
2008-10-09 17:15:23 +08:00
|
|
|
unsigned int Buffer::assign( const unsigned char *pStorage, unsigned int pSize )
|
2004-03-13 22:06:53 +08:00
|
|
|
{
|
2008-10-09 17:15:23 +08:00
|
|
|
if ( mAllocation < pSize )
|
2008-07-16 16:35:59 +08:00
|
|
|
{
|
2008-10-09 17:15:23 +08:00
|
|
|
delete[] mStorage;
|
|
|
|
mAllocation = pSize;
|
|
|
|
mHead = mStorage = new unsigned char[pSize];
|
2008-07-16 16:35:59 +08:00
|
|
|
}
|
2008-10-09 17:15:23 +08:00
|
|
|
mSize = pSize;
|
|
|
|
memcpy( mStorage, pStorage, mSize );
|
|
|
|
mHead = mStorage;
|
|
|
|
mTail = mHead + mSize;
|
|
|
|
return( mSize );
|
2004-03-13 22:06:53 +08:00
|
|
|
}
|
|
|
|
|
2008-10-09 17:15:23 +08:00
|
|
|
unsigned int Buffer::expand( unsigned int count )
|
2004-03-13 22:06:53 +08:00
|
|
|
{
|
2008-10-09 17:15:23 +08:00
|
|
|
int spare = mAllocation - mSize;
|
|
|
|
int headSpace = mHead - mStorage;
|
|
|
|
int tailSpace = spare - headSpace;
|
|
|
|
int width = mTail - mHead;
|
2008-07-21 16:46:04 +08:00
|
|
|
if ( spare > count )
|
|
|
|
{
|
2008-10-09 17:15:23 +08:00
|
|
|
if ( tailSpace < count )
|
2008-07-21 16:46:04 +08:00
|
|
|
{
|
2008-10-09 17:15:23 +08:00
|
|
|
memmove( mStorage, mHead, mSize );
|
|
|
|
mHead = mStorage;
|
|
|
|
mTail = mHead + width;
|
2008-07-21 16:46:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-10-09 17:15:23 +08:00
|
|
|
mAllocation += count;
|
|
|
|
unsigned char *newStorage = new unsigned char[mAllocation];
|
|
|
|
if ( mStorage )
|
2008-07-21 16:46:04 +08:00
|
|
|
{
|
2008-10-09 17:15:23 +08:00
|
|
|
memcpy( newStorage, mHead, mSize );
|
|
|
|
delete[] mStorage;
|
2008-07-21 16:46:04 +08:00
|
|
|
}
|
2008-10-09 17:15:23 +08:00
|
|
|
mStorage = newStorage;
|
|
|
|
mHead = mStorage;
|
|
|
|
mTail = mHead + width;
|
2008-07-21 16:46:04 +08:00
|
|
|
}
|
2008-10-09 17:15:23 +08:00
|
|
|
return( mSize );
|
2004-03-13 22:06:53 +08:00
|
|
|
}
|