Rewritten.
committer: mfx <mfx> 1043693646 +0000
This commit is contained in:
+41
-28
@@ -59,12 +59,12 @@ static int mcheck_init()
|
|||||||
|
|
||||||
|
|
||||||
MemBuffer::MemBuffer() :
|
MemBuffer::MemBuffer() :
|
||||||
ptr(NULL), psize(0)
|
b(NULL), b_size(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
MemBuffer::MemBuffer(unsigned size) :
|
MemBuffer::MemBuffer(unsigned size) :
|
||||||
ptr(NULL), psize(0)
|
b(NULL), b_size(0)
|
||||||
{
|
{
|
||||||
alloc(size);
|
alloc(size);
|
||||||
}
|
}
|
||||||
@@ -77,26 +77,26 @@ MemBuffer::~MemBuffer()
|
|||||||
|
|
||||||
void MemBuffer::dealloc()
|
void MemBuffer::dealloc()
|
||||||
{
|
{
|
||||||
if (ptr)
|
if (b)
|
||||||
{
|
{
|
||||||
checkState();
|
checkState();
|
||||||
if (use_mcheck)
|
if (use_mcheck)
|
||||||
{
|
{
|
||||||
// clear magic constants
|
// remove magic constants
|
||||||
set_be32(ptr - 8, 0);
|
set_be32(b - 8, 0);
|
||||||
set_be32(ptr - 4, 0);
|
set_be32(b - 4, 0);
|
||||||
set_be32(ptr + psize, 0);
|
set_be32(b + b_size, 0);
|
||||||
set_be32(ptr + psize + 4, 0);
|
set_be32(b + b_size + 4, 0);
|
||||||
//
|
//
|
||||||
::free(ptr - 8);
|
::free(b - 8);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
::free(ptr);
|
::free(b);
|
||||||
ptr = NULL;
|
b = NULL;
|
||||||
psize = 0;
|
b_size = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
assert(psize == 0);
|
assert(b_size == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -123,6 +123,19 @@ void MemBuffer::allocForUncompression(unsigned uncompressed_size, unsigned extra
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MemBuffer::fill(unsigned off, unsigned len, int value)
|
||||||
|
{
|
||||||
|
checkState();
|
||||||
|
assert((int)off >= 0);
|
||||||
|
assert((int)len >= 0);
|
||||||
|
assert(off <= b_size);
|
||||||
|
assert(len <= b_size);
|
||||||
|
assert(off + len <= b_size);
|
||||||
|
if (len > 0)
|
||||||
|
memset(b + off, value, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
//
|
//
|
||||||
**************************************************************************/
|
**************************************************************************/
|
||||||
@@ -136,18 +149,18 @@ unsigned MemBuffer::global_alloc_counter = 0;
|
|||||||
|
|
||||||
void MemBuffer::checkState() const
|
void MemBuffer::checkState() const
|
||||||
{
|
{
|
||||||
if (!ptr)
|
if (!b)
|
||||||
throwInternalError("block not allocated");
|
throwInternalError("block not allocated");
|
||||||
if (use_mcheck)
|
if (use_mcheck)
|
||||||
{
|
{
|
||||||
if (get_be32(ptr - 4) != MAGIC1(ptr))
|
if (get_be32(b - 4) != MAGIC1(b))
|
||||||
throwInternalError("memory clobbered before allocated block 1");
|
throwInternalError("memory clobbered before allocated block 1");
|
||||||
if (get_be32(ptr - 8) != psize)
|
if (get_be32(b - 8) != b_size)
|
||||||
throwInternalError("memory clobbered before allocated block 2");
|
throwInternalError("memory clobbered before allocated block 2");
|
||||||
if (get_be32(ptr + psize) != MAGIC2(ptr))
|
if (get_be32(b + b_size) != MAGIC2(b))
|
||||||
throwInternalError("memory clobbered past end of allocated block");
|
throwInternalError("memory clobbered past end of allocated block");
|
||||||
}
|
}
|
||||||
assert((int)psize > 0);
|
assert((int)b_size > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -157,8 +170,8 @@ void MemBuffer::alloc(unsigned size)
|
|||||||
mcheck_init();
|
mcheck_init();
|
||||||
|
|
||||||
// NOTE: we don't automatically free a used buffer
|
// NOTE: we don't automatically free a used buffer
|
||||||
assert(ptr == NULL);
|
assert(b == NULL);
|
||||||
assert(psize == 0);
|
assert(b_size == 0);
|
||||||
//
|
//
|
||||||
assert((int)size > 0);
|
assert((int)size > 0);
|
||||||
unsigned total = use_mcheck ? size + 16 : size;
|
unsigned total = use_mcheck ? size + 16 : size;
|
||||||
@@ -167,21 +180,21 @@ void MemBuffer::alloc(unsigned size)
|
|||||||
if (!p)
|
if (!p)
|
||||||
{
|
{
|
||||||
//throw bad_alloc();
|
//throw bad_alloc();
|
||||||
throwCantPack("out of memory");
|
throw OutOfMemoryException("out of memory");
|
||||||
//exit(1);
|
//exit(1);
|
||||||
}
|
}
|
||||||
psize = size;
|
b_size = size;
|
||||||
if (use_mcheck)
|
if (use_mcheck)
|
||||||
{
|
{
|
||||||
ptr = p + 8;
|
b = p + 8;
|
||||||
// store magic constants to detect buffer overruns
|
// store magic constants to detect buffer overruns
|
||||||
set_be32(ptr - 8, psize);
|
set_be32(b - 8, b_size);
|
||||||
set_be32(ptr - 4, MAGIC1(ptr));
|
set_be32(b - 4, MAGIC1(b));
|
||||||
set_be32(ptr + psize, MAGIC2(ptr));
|
set_be32(b + b_size, MAGIC2(b));
|
||||||
set_be32(ptr + psize + 4, global_alloc_counter++);
|
set_be32(b + b_size + 4, global_alloc_counter++);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
ptr = p ;
|
b = p ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -49,16 +49,20 @@ public:
|
|||||||
|
|
||||||
void checkState() const;
|
void checkState() const;
|
||||||
|
|
||||||
unsigned getSize() const { return psize; }
|
unsigned getSize() const { return b_size; }
|
||||||
|
|
||||||
operator unsigned char * () { return ptr; }
|
operator unsigned char * () { return b; }
|
||||||
//operator const unsigned char * () const { return ptr; }
|
//operator const unsigned char * () const { return b; }
|
||||||
void *getVoidPtr() { return (void *) ptr; }
|
void *getVoidPtr() { return (void *) b; }
|
||||||
const void *getVoidPtr() const { return (const void *) ptr; }
|
const void *getVoidPtr() const { return (const void *) b; }
|
||||||
|
|
||||||
|
void fill(unsigned off, unsigned len, int value);
|
||||||
|
void clear(unsigned off, unsigned len) { fill(off, len, 0); }
|
||||||
|
void clear() { fill(0, b_size, 0); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned char *ptr;
|
unsigned char *b;
|
||||||
unsigned psize;
|
unsigned b_size;
|
||||||
|
|
||||||
static unsigned global_alloc_counter;
|
static unsigned global_alloc_counter;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user