2000-05-20 00:04:55 +08:00
|
|
|
/* mem.cpp --
|
|
|
|
|
|
|
|
This file is part of the UPX executable compressor.
|
|
|
|
|
2003-01-14 01:16:39 +08:00
|
|
|
Copyright (C) 1996-2003 Markus Franz Xaver Johannes Oberhumer
|
|
|
|
Copyright (C) 1996-2003 Laszlo Molnar
|
2000-11-13 20:22:40 +08:00
|
|
|
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.
|
|
|
|
|
2002-07-17 00:00:58 +08:00
|
|
|
Markus F.X.J. Oberhumer Laszlo Molnar
|
|
|
|
<mfx@users.sourceforge.net> <ml1050@users.sourceforge.net>
|
2000-05-20 00:04:55 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "conf.h"
|
|
|
|
#include "mem.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
//
|
|
|
|
**************************************************************************/
|
|
|
|
|
2000-09-10 14:23:47 +08:00
|
|
|
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()
|
|
|
|
{
|
2001-02-25 22:02:57 +08:00
|
|
|
this->dealloc();
|
2000-05-20 00:04:55 +08:00
|
|
|
}
|
|
|
|
|
2001-02-25 22:02:57 +08:00
|
|
|
void MemBuffer::dealloc()
|
2000-05-20 00:04:55 +08:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2002-10-16 12:15:18 +08:00
|
|
|
// NOTE: we don't automaticlly free a used buffer
|
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)
|
|
|
|
{
|
2002-09-12 22:39:17 +08:00
|
|
|
//throw bad_alloc();
|
2000-05-20 00:04:55 +08:00
|
|
|
throwCantPack("out of memory");
|
|
|
|
//exit(1);
|
|
|
|
}
|
|
|
|
alloc_size = size;
|
|
|
|
ptr = alloc_ptr + base_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MemBuffer::alloc(unsigned size)
|
|
|
|
{
|
|
|
|
alloc(size, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-15 04:13:33 +08:00
|
|
|
void MemBuffer::allocForCompression(unsigned uncompressed_size, unsigned extra)
|
2000-05-20 00:04:55 +08:00
|
|
|
{
|
2002-10-16 12:15:18 +08:00
|
|
|
assert((int)uncompressed_size > 0);
|
2003-01-15 04:13:33 +08:00
|
|
|
assert((int)extra >= 0);
|
|
|
|
alloc(uncompressed_size + uncompressed_size/8 + 256 + extra, 0);
|
2000-05-20 00:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-15 04:13:33 +08:00
|
|
|
void MemBuffer::allocForUncompression(unsigned uncompressed_size, unsigned extra)
|
2000-05-20 00:04:55 +08:00
|
|
|
{
|
2002-10-16 12:15:18 +08:00
|
|
|
assert((int)uncompressed_size > 0);
|
2003-01-15 04:13:33 +08:00
|
|
|
assert((int)extra >= 0);
|
|
|
|
//alloc(uncompressed_size + 3 + 512 + extra, 0); // 512 safety bytes
|
|
|
|
alloc(uncompressed_size + 3 + extra, 0); // 3 bytes for asm_fast decompresion
|
2000-05-20 00:04:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
vi:ts=4:et
|
|
|
|
*/
|
|
|
|
|