libcw/cwMem.cpp

97 lines
1.9 KiB
C++
Raw Normal View History

2019-12-19 03:24:12 +00:00
#include "cwCommon.h"
#include "cwLog.h"
#include "cwCommonImpl.h"
#include "cwMem.h"
void* cw::mem::_alloc( void* p0, unsigned n, bool zeroFl )
2019-12-19 03:24:12 +00:00
{
void* p = nullptr; // ptr to new block
unsigned p0N = 0; // size of existing block
unsigned* p0_1 = nullptr; // pointer to base of existing block
n += sizeof(unsigned); // add space for the size of the block
2019-12-19 03:24:12 +00:00
// if there is no existing block
if( p0 != nullptr )
2019-12-19 03:24:12 +00:00
{
// get a pointer to the base of the exsting block
p0_1 = ((unsigned*)p0) - 1;
p0N = p0_1[0]; // get size of existing block
2019-12-19 03:24:12 +00:00
// if the block is shrinking
if( p0N >= n )
return p0;
}
p = malloc(n); // allocate new memory
// if expanding then copy in data from existing block
if( p0 != nullptr )
{
memcpy(p,p0_1,p0N);
mem::free(p0); // free the existing block
2019-12-19 03:24:12 +00:00
}
// if requested zero the block
if( zeroFl )
memset(((char*)p)+p0N,0,n-p0N);
// get pointer to base of new block
unsigned* p1 = static_cast<unsigned*>(p);
p1[0] = n; // set size of new block
2019-12-19 03:24:12 +00:00
// advance past the block size and return
return p1+1;
}
unsigned cw::mem::byteCount( const void* p )
2019-12-19 03:24:12 +00:00
{
return p==nullptr ? 0 : static_cast<const unsigned*>(p)[-1];
}
char* cw::mem::allocStr( const char* s )
2019-12-19 03:24:12 +00:00
{
char* s1 = nullptr;
if( s != nullptr )
{
unsigned sn = strlen(s);
s1 = static_cast<char*>(_alloc(nullptr,sn+1,false));
2019-12-19 03:24:12 +00:00
memcpy(s1,s,sn);
s1[sn] = 0;
}
return s1;
}
void* cw::mem::allocDupl( const void* p0, unsigned byteN )
2019-12-19 03:24:12 +00:00
{
if( p0 == nullptr || byteN == 0 )
return nullptr;
void* p1 = _alloc(nullptr,byteN,false);
2019-12-19 03:24:12 +00:00
memcpy(p1,p0,byteN);
return p1;
}
void* cw::mem::allocDupl( const void* p )
2019-12-19 03:24:12 +00:00
{
return allocDupl(p,byteCount(p));
2019-12-19 03:24:12 +00:00
}
void cw::mem::free( void* p )
2019-12-19 03:24:12 +00:00
{
if( p != nullptr)
{
::free(static_cast<unsigned*>(p)-1);
2019-12-19 03:24:12 +00:00
}
}