diff --git a/cwMem.cpp b/cwMem.cpp index a7f01b8..c782313 100644 --- a/cwMem.cpp +++ b/cwMem.cpp @@ -4,7 +4,7 @@ #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 unsigned p0N = 0; // size of existing block @@ -34,9 +34,14 @@ void* cw::mem::_alloc( void* p0, unsigned n, bool zeroFl ) mem::free(p0); // free the existing block } - // if requested zero the block - if( zeroFl ) - memset(((char*)p)+p0N,0,n-p0N); + + // 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); // get pointer to base of new block unsigned* p1 = static_cast(p); @@ -70,7 +75,7 @@ char* cw::mem::allocStr( const char* s ) 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 ) return nullptr; @@ -80,9 +85,9 @@ void* cw::mem::allocDupl( const void* p0, unsigned byteN ) 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)); } diff --git a/cwMem.h b/cwMem.h index 65e7ede..e3166c5 100644 --- a/cwMem.h +++ b/cwMem.h @@ -6,27 +6,45 @@ namespace cw 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* ); - void* allocDupl( const void* p, unsigned byteN ); - void* allocDupl( const void* p ); void free( void* ); unsigned byteCount( const void* p ); template void release(T& p) { ::cw::mem::free(p); p=nullptr; } - - template - T* allocZ(unsigned n=1) { return static_cast(_alloc(nullptr,n*sizeof(T),true)); } template - T* alloc(unsigned n=1) { return static_cast(_alloc(nullptr,n*sizeof(T),false)); } + T* alloc(unsigned n, unsigned flags) { return static_cast(_alloc(nullptr,n*sizeof(T),flags)); } + + template + T* allocZ(unsigned n=1) { return alloc(n,kZeroAllFl); } template - T* resizeZ(T* p, unsigned n=1) { return static_cast(_alloc(p,n*sizeof(T),true)); } + T* alloc(unsigned n=1) { return alloc(n,0); } + template + T* resize(T* p, unsigned n, unsigned flags) { return static_cast(_alloc(p,n*sizeof(T),flags)); } + + // zero the newly allocated space but leave the initial space unchanged. + template + T* resizeZ(T* p, unsigned n=1) { return resize(p,n,kZeroNewFl); } + + template + T* resize(T* p, unsigned n=1) { return resize(p,n,0); } + + template + T* allocDupl(const T* p, unsigned eleN ) { return (T*)_allocDupl(p,eleN*sizeof(T)); } template size_t _textLength(const T* s ) @@ -105,8 +123,32 @@ namespace cw return s0; } + template + 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(sn+1); + strcpy(s,s0); + strcpy(s+s0n,s1); + + free(s0); + + return s; + } + + template - 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_copy(vl1,vl0); @@ -129,10 +171,18 @@ namespace cw va_end(vl1); - return reallocStr(p0,buf); + return appendFl ? appendStr(p0,buf) : reallocStr(p0,buf); } - + template + C* printf(C* p0, const char* fmt, va_list vl0 ) + { return _printf(p0,false,fmt,vl0); } + + template + C* printp(C* p0, const char* fmt, va_list vl0 ) + { return _printf(p0,true,fmt,vl0); } + + template C* printf(C* p0, const char* fmt, ... ) { @@ -142,7 +192,17 @@ namespace cw va_end(vl); return p1; } - + + template + 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; + } + } }