cwMem.h : Expanded allocDupl() interfaces. Added printp(),appendStr()

This commit is contained in:
kevin 2020-08-19 20:08:14 -04:00
parent 721d333a7f
commit ded6a1ef4a
2 changed files with 84 additions and 19 deletions

View File

@ -4,7 +4,7 @@
#include "cwMem.h" #include "cwMem.h"
void* cw::mem::_alloc( void* p0, unsigned n, bool zeroFl ) void* cw::mem::_alloc( void* p0, unsigned n, unsigned flags )
{ {
void* p = nullptr; // ptr to new block void* p = nullptr; // ptr to new block
unsigned p0N = 0; // size of existing block unsigned p0N = 0; // size of existing block
@ -34,8 +34,13 @@ void* cw::mem::_alloc( void* p0, unsigned n, bool zeroFl )
mem::free(p0); // free the existing block mem::free(p0); // free the existing block
} }
// if requested zero the block
if( zeroFl ) // zero all memory
if( cwIsFlag(flags, kZeroAllFl))
memset(((char*)p),0,n);
else
// zero the exapnded memory but leave existing memory unchanged
if( cwIsFlag(flags, kZeroNewFl ))
memset(((char*)p)+p0N,0,n-p0N); memset(((char*)p)+p0N,0,n-p0N);
// get pointer to base of new block // get pointer to base of new block
@ -70,7 +75,7 @@ char* cw::mem::allocStr( const char* s )
return s1; return s1;
} }
void* cw::mem::allocDupl( const void* p0, unsigned byteN ) void* cw::mem::_allocDupl( const void* p0, unsigned byteN )
{ {
if( p0 == nullptr || byteN == 0 ) if( p0 == nullptr || byteN == 0 )
return nullptr; return nullptr;
@ -80,9 +85,9 @@ void* cw::mem::allocDupl( const void* p0, unsigned byteN )
return p1; return p1;
} }
void* cw::mem::allocDupl( const void* p ) void* cw::mem::_allocDupl( const void* p )
{ {
return allocDupl(p,byteCount(p)); return _allocDupl(p,byteCount(p));
} }

76
cwMem.h
View File

@ -6,11 +6,17 @@ namespace cw
namespace mem namespace mem
{ {
void* _alloc( void* p, unsigned n, bool zeroFl ); enum
{
kZeroNewFl = 0x01, // zero only the expanded (new) space during a resize() operation
kZeroAllFl = 0x02 // zero all the space during a resize operation
};
void* _alloc( void* p, unsigned n, unsigned flags );
void* _allocDupl( const void* p, unsigned byteN );
void* _allocDupl( const void* p );
char* allocStr( const char* ); char* allocStr( const char* );
void* allocDupl( const void* p, unsigned byteN );
void* allocDupl( const void* p );
void free( void* ); void free( void* );
unsigned byteCount( const void* p ); unsigned byteCount( const void* p );
@ -19,14 +25,26 @@ namespace cw
void release(T& p) { ::cw::mem::free(p); p=nullptr; } void release(T& p) { ::cw::mem::free(p); p=nullptr; }
template<typename T> template<typename T>
T* allocZ(unsigned n=1) { return static_cast<T*>(_alloc(nullptr,n*sizeof(T),true)); } T* alloc(unsigned n, unsigned flags) { return static_cast<T*>(_alloc(nullptr,n*sizeof(T),flags)); }
template<typename T> template<typename T>
T* alloc(unsigned n=1) { return static_cast<T*>(_alloc(nullptr,n*sizeof(T),false)); } T* allocZ(unsigned n=1) { return alloc<T>(n,kZeroAllFl); }
template<typename T> template<typename T>
T* resizeZ(T* p, unsigned n=1) { return static_cast<T*>(_alloc(p,n*sizeof(T),true)); } T* alloc(unsigned n=1) { return alloc<T>(n,0); }
template<typename T>
T* resize(T* p, unsigned n, unsigned flags) { return static_cast<T*>(_alloc(p,n*sizeof(T),flags)); }
// zero the newly allocated space but leave the initial space unchanged.
template<typename T>
T* resizeZ(T* p, unsigned n=1) { return resize<T>(p,n,kZeroNewFl); }
template<typename T>
T* resize(T* p, unsigned n=1) { return resize<T>(p,n,0); }
template<typename T>
T* allocDupl(const T* p, unsigned eleN ) { return (T*)_allocDupl(p,eleN*sizeof(T)); }
template<typename T> template<typename T>
size_t _textLength(const T* s ) size_t _textLength(const T* s )
@ -105,8 +123,32 @@ namespace cw
return s0; return s0;
} }
template<typename T>
T* appendStr( T* s0, const T* s1 )
{
if( s1 == nullptr || strlen(s1)== 0 )
return s0;
if( s0 == nullptr )
return duplStr(s1);
size_t s0n = _textLength(s0);
size_t s1n = _textLength(s1);
size_t sn = s0n + s1n;
T* s = alloc<T>(sn+1);
strcpy(s,s0);
strcpy(s+s0n,s1);
free(s0);
return s;
}
template<typename C> template<typename C>
C* printf(C* p0, const char* fmt, va_list vl0 ) C* _printf(C* p0, bool appendFl, const char* fmt, va_list vl0 )
{ {
va_list vl1; va_list vl1;
va_copy(vl1,vl0); va_copy(vl1,vl0);
@ -129,9 +171,17 @@ namespace cw
va_end(vl1); va_end(vl1);
return reallocStr(p0,buf); return appendFl ? appendStr(p0,buf) : reallocStr(p0,buf);
} }
template<typename C>
C* printf(C* p0, const char* fmt, va_list vl0 )
{ return _printf(p0,false,fmt,vl0); }
template<typename C>
C* printp(C* p0, const char* fmt, va_list vl0 )
{ return _printf(p0,true,fmt,vl0); }
template<typename C> template<typename C>
C* printf(C* p0, const char* fmt, ... ) C* printf(C* p0, const char* fmt, ... )
@ -143,6 +193,16 @@ namespace cw
return p1; return p1;
} }
template<typename C>
C* printp(C* p0, const char* fmt, ... )
{
va_list vl;
va_start(vl,fmt);
C* p1 = printp(p0,fmt,vl);
va_end(vl);
return p1;
}
} }
} }