upx/src/mem.cpp

114 lines
2.6 KiB
C++
Raw Normal View History

2000-05-20 00:04:55 +08:00
/* mem.cpp --
This file is part of the UPX executable compressor.
Copyright (C) 1996-2001 Markus Franz Xaver Johannes Oberhumer
Copyright (C) 1996-2001 Laszlo Molnar
All Rights Reserved.
2000-05-20 00:04:55 +08:00
UPX and the UCL library are free software; you can redistribute them
and/or modify them 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; see the file COPYING.
If not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Markus F.X.J. Oberhumer Laszlo Molnar
markus.oberhumer@jk.uni-linz.ac.at ml1050@cdata.tvnet.hu
*/
#include "conf.h"
#include "mem.h"
/*************************************************************************
//
**************************************************************************/
MemBuffer::MemBuffer(unsigned size) :
2000-05-20 00:04:55 +08:00
ptr(NULL), alloc_ptr(NULL), alloc_size(0)
{
if (size > 0)
alloc(size, 0);
}
MemBuffer::~MemBuffer()
{
this->free();
2000-05-20 00:04:55 +08:00
}
void MemBuffer::free()
{
if (alloc_ptr)
::free(alloc_ptr);
alloc_ptr = ptr = NULL;
alloc_size = 0;
}
unsigned MemBuffer::getSize() const
{
if (!alloc_ptr)
return 0;
unsigned size = alloc_size - (ptr - alloc_ptr);
assert((int)size > 0);
return size;
}
void MemBuffer::alloc(unsigned size, unsigned base_offset)
{
#if 0
this->free();
#else
// don't automaticlly free a used buffer
#endif
2000-05-20 00:04:55 +08:00
assert(alloc_ptr == NULL);
assert((int)size > 0);
size = base_offset + size;
alloc_ptr = (unsigned char *) malloc(size);
if (!alloc_ptr)
{
throwCantPack("out of memory");
//exit(1);
}
alloc_size = size;
ptr = alloc_ptr + base_offset;
}
void MemBuffer::alloc(unsigned size)
{
alloc(size, 0);
}
void MemBuffer::allocForCompression(unsigned uncompressed_size)
{
alloc(uncompressed_size + uncompressed_size/8 + 256, 0);
2000-05-20 00:04:55 +08:00
}
void MemBuffer::allocForUncompression(unsigned uncompressed_size)
{
//alloc(uncompressed_size + 3 + 512, 0); // 512 safety bytes
alloc(uncompressed_size + 3, 0); // 3 bytes for asm_fast decompresion
2000-05-20 00:04:55 +08:00
}
/*
vi:ts=4:et
*/