From 5a62d884dd4f440dc5c3ff065fdb1743db41625c Mon Sep 17 00:00:00 2001 From: kpl Date: Fri, 27 Dec 2019 21:51:28 -0500 Subject: [PATCH] Standardized filesys and mem namespaces. Removed use of global variable in cwAudioBuf. --- cwAudioBuf.cpp | 337 ++++++++++++++++++++++++------------------ cwAudioBuf.h | 71 +++++---- cwAudioDevice.cpp | 8 +- cwAudioDevice.h | 2 +- cwAudioDeviceAlsa.cpp | 51 ++++--- cwAudioDeviceTest.cpp | 269 +++------------------------------ cwFile.cpp | 52 +++---- cwFileSys.cpp | 24 +-- cwFileSys.h | 66 +++++---- cwLex.cpp | 32 ++-- cwLog.cpp | 4 +- cwMem.cpp | 27 ++-- cwMem.h | 226 ++++++++++++++-------------- cwMidiAlsa.cpp | 32 ++-- cwMidiPort.cpp | 14 +- cwMpScNbQueue.h | 8 +- cwObject.cpp | 10 +- cwSerialPort.cpp | 8 +- cwSerialPortSrv.cpp | 4 +- cwTcpSocket.cpp | 4 +- cwTcpSocketSrv.cpp | 8 +- cwTextBuf.cpp | 22 +-- cwThread.cpp | 4 +- cwWebSock.cpp | 50 +++---- cwWebSockSvr.cpp | 4 +- main.cpp | 8 +- 26 files changed, 580 insertions(+), 765 deletions(-) diff --git a/cwAudioBuf.cpp b/cwAudioBuf.cpp index 583eb41..20691e2 100644 --- a/cwAudioBuf.cpp +++ b/cwAudioBuf.cpp @@ -75,12 +75,12 @@ namespace cw { unsigned chCnt; cmApCh* chArray; - unsigned n; // length of b[] (multiple of dspFrameCnt) bufCnt*framesPerCycle - double srate; // device sample rate; + unsigned n; // length of b[] (multiple of dspFrameCnt) bufCnt*framesPerCycle + double srate; // device sample rate; unsigned faultCnt; unsigned framesPerCycle; unsigned dspFrameCnt; - time::spec_t timeStamp; // base (starting) time stamp for this device + time::spec_t timeStamp; // base (starting) time stamp for this device std::atomic ioFrameCnt; // count of frames input or output for this device } cmApIO; @@ -88,30 +88,30 @@ namespace cw typedef struct { // ioArray[] always contains 2 elements - one for input the other for output. - cmApIO ioArray[kIoApCnt]; + cmApIO ioArray[kIoApCnt]; } cmApDev; - typedef struct + typedef struct audioBuf_str { - cmApDev* devArray; - unsigned devCnt; - unsigned meterMs; + cmApDev* devArray; + unsigned devCnt; + unsigned meterMs; sample_t* zeroBuf; // buffer of zeros unsigned zeroBufCnt; // max of all dspFrameCnt for all devices. - } cmApBuf; + } audioBuf_t; - cmApBuf _theBuf; + inline audioBuf_t* _handleToPtr( handle_t h ) { return handleToPtr(h); } sample_t _cmApMeterValue( const cmApCh* cp ) { - double sum = 0; + double sum = 0; unsigned i; for(i=0; imn; ++i) - sum += cp->m[i]; + sum += cp->m[i]; - return cp->mn==0 ? 0 : (sample_t)sqrt(sum/cp->mn); + return cp->mn == 0 ? 0 : (sample_t)sqrt(sum/cp->mn); } void _cmApSine( cmApCh* cp, sample_t* b0, unsigned n0, sample_t* b1, unsigned n1, unsigned stride, float srate ) @@ -130,24 +130,24 @@ namespace cw const sample_t* ep = b + bn; sample_t sum = 0; - for(; bb ); - memRelease( chPtr->m ); + mem::release( chPtr->b ); + mem::release( chPtr->m ); } - // n=buf sample cnt mn=meter buf smp cnt + // n = buf sample cnt mn=meter buf smp cnt void _cmApChInitialize( cmApCh* chPtr, unsigned n, unsigned mn ) { _cmApChFinalize(chPtr); - chPtr->b = n==0 ? NULL : memAllocZ( n ); + chPtr->b = n==0 ? NULL : mem::allocZ( n ); chPtr->ii = 0; chPtr->oi = 0; chPtr->fn = 0; @@ -155,7 +155,7 @@ namespace cw chPtr->hz = 1000; chPtr->gain = 1.0; chPtr->mn = mn; - chPtr->m = memAllocZ(mn); + chPtr->m = mem::allocZ(mn); chPtr->mi = 0; } @@ -165,7 +165,7 @@ namespace cw for(i=0; ichCnt; ++i) _cmApChFinalize( ioPtr->chArray + i ); - memRelease(ioPtr->chArray); + mem::release(ioPtr->chArray); ioPtr->chCnt = 0; ioPtr->n = 0; } @@ -178,7 +178,7 @@ namespace cw n += (n % dspFrameCnt); // force buffer size to be a multiple of dspFrameCnt - ioPtr->chArray = chCnt==0 ? NULL : memAllocZ(chCnt ); + ioPtr->chArray = chCnt==0 ? NULL : mem::allocZ(chCnt ); ioPtr->chCnt = chCnt; ioPtr->n = n; ioPtr->faultCnt = 0; @@ -189,7 +189,7 @@ namespace cw ioPtr->timeStamp.tv_nsec = 0; ioPtr->ioFrameCnt = 0; - for(i=0; ichArray + i, n, meterBufN ); } @@ -197,7 +197,7 @@ namespace cw void _cmApDevFinalize( cmApDev* dp ) { unsigned i; - for(i=0; iioArray+i); } @@ -207,7 +207,7 @@ namespace cw _cmApDevFinalize(dp); - for(i=0; itv_nsec = floor(baseTimeStamp->tv_nsec + frac_secs * 1000000000); - retTimeStamp->tv_sec = baseTimeStamp->tv_sec + int_secs; + retTimeStamp->tv_sec = baseTimeStamp->tv_sec + int_secs; if( retTimeStamp->tv_nsec > 1000000000 ) { @@ -242,34 +242,48 @@ void _theBufCalcTimeStamp( double srate, const time::spec_t* baseTimeStamp, unsi } } -cw::rc_t cw::audio::buf::initialize( unsigned devCnt, unsigned meterMs ) +cw::rc_t cw::audio::buf::create( handle_t& hRef, unsigned devCnt, unsigned meterMs ) { rc_t rc; - if((rc = finalize()) != kOkRC ) + if((rc = destroy(hRef)) != kOkRC ) + return rc; + + audioBuf_t* p = mem::allocZ(); + + p->devArray = mem::allocZ(devCnt ); + p->devCnt = devCnt; + + hRef.set(p); + + setMeterMs(hRef,meterMs); + + return kOkRC; +} + +cw::rc_t cw::audio::buf::destroy( handle_t& hRef ) +{ + rc_t rc = kOkRC; + + if( !hRef.isValid() ) return rc; - _theBuf.devArray = memAllocZ(devCnt ); - _theBuf.devCnt = devCnt; - setMeterMs(meterMs); - return kOkRC; -} - -cw::rc_t cw::audio::buf::finalize() -{ - unsigned i; - for(i=0; i<_theBuf.devCnt; ++i) - _cmApDevFinalize(_theBuf.devArray + i); - - memRelease( _theBuf.devArray ); - memRelease( _theBuf.zeroBuf ); + audioBuf_t* p = _handleToPtr(hRef); - _theBuf.devCnt = 0; + unsigned i; + for(i=0; idevCnt; ++i) + _cmApDevFinalize(p->devArray + i); + + mem::release( p->devArray ); + mem::release( p->zeroBuf ); + + p->devCnt = 0; return kOkRC; } -cw::rc_t cw::audio::buf::setup( +cw::rc_t cw::audio::buf::setup( + handle_t h, unsigned devIdx, double srate, unsigned dspFrameCnt, @@ -279,25 +293,27 @@ cw::rc_t cw::audio::buf::setup( unsigned outChCnt, unsigned outFramesPerCycle) { - cmApDev* devPtr = _theBuf.devArray + devIdx; + audioBuf_t* p = _handleToPtr(h); + cmApDev* devPtr = p->devArray + devIdx; unsigned iBufN = bufCnt * inFramesPerCycle; unsigned oBufN = bufCnt * outFramesPerCycle; - unsigned meterBufN = std::max(1.0,floor(srate * _theBuf.meterMs / (1000.0 * outFramesPerCycle))); + unsigned meterBufN = std::max(1.0,floor(srate * p->meterMs / (1000.0 * outFramesPerCycle))); _cmApDevInitialize( devPtr, srate, inFramesPerCycle, inChCnt, iBufN, outFramesPerCycle, outChCnt, oBufN, meterBufN, dspFrameCnt ); - if( inFramesPerCycle > _theBuf.zeroBufCnt || outFramesPerCycle > _theBuf.zeroBufCnt ) + if( inFramesPerCycle > p->zeroBufCnt || outFramesPerCycle > p->zeroBufCnt ) { - _theBuf.zeroBufCnt = std::max(inFramesPerCycle,outFramesPerCycle); - _theBuf.zeroBuf = memResizeZ(_theBuf.zeroBuf,_theBuf.zeroBufCnt); + p->zeroBufCnt = std::max(inFramesPerCycle,outFramesPerCycle); + p->zeroBuf = mem::resizeZ(p->zeroBuf,p->zeroBufCnt); } return kOkRC; } -cw::rc_t cw::audio::buf::primeOutput( unsigned devIdx, unsigned audioCycleCnt ) +cw::rc_t cw::audio::buf::primeOutput( handle_t h, unsigned devIdx, unsigned audioCycleCnt ) { - cmApIO* iop = _theBuf.devArray[devIdx].ioArray + kOutApIdx; + audioBuf_t* p = _handleToPtr(h); + cmApIO* iop = p->devArray[devIdx].ioArray + kOutApIdx; unsigned i; for(i=0; ichCnt; ++i) @@ -313,17 +329,18 @@ cw::rc_t cw::audio::buf::primeOutput( unsigned devIdx, unsigned audioCycleCnt ) return kOkRC; } -void cw::audio::buf::onPortEnable( unsigned devIdx, bool enableFl ) +void cw::audio::buf::onPortEnable( handle_t h, unsigned devIdx, bool enableFl ) { + audioBuf_t* p = _handleToPtr(h); if( devIdx == kInvalidIdx || enableFl==false) return; - cmApIO* iop = _theBuf.devArray[devIdx].ioArray + kOutApIdx; + cmApIO* iop = p->devArray[devIdx].ioArray + kOutApIdx; iop->timeStamp.tv_sec = 0; iop->timeStamp.tv_nsec = 0; iop->ioFrameCnt = 0; - iop = _theBuf.devArray[devIdx].ioArray + kInApIdx; + iop = p->devArray[devIdx].ioArray + kInApIdx; iop->timeStamp.tv_sec = 0; iop->timeStamp.tv_nsec = 0; iop->ioFrameCnt = 0; @@ -332,12 +349,14 @@ void cw::audio::buf::onPortEnable( unsigned devIdx, bool enableFl ) } cw::rc_t cw::audio::buf::update( + handle_t h, device::audioPacket_t* inPktArray, unsigned inPktCnt, device::audioPacket_t* outPktArray, unsigned outPktCnt ) { unsigned i,j; + audioBuf_t* p = _handleToPtr(h); // copy samples from the packet to the buffer if( inPktArray != NULL ) @@ -345,7 +364,7 @@ cw::rc_t cw::audio::buf::update( for(i=0; idevIdx].ioArray + kInApIdx; // dest io recd + cmApIO* ip = p->devArray[pp->devIdx].ioArray + kInApIdx; // dest io recd // if the base time stamp has not yet been set - then set it if( ip->timeStamp.tv_sec==0 && ip->timeStamp.tv_nsec==0 ) @@ -375,7 +394,7 @@ cw::rc_t cw::audio::buf::update( n0 = pp->audioFramesCnt; bool enaFl = cwIsFlag(cp->fl,kChFl) && cwIsFlag(cp->fl,kMuteFl)==false; - const sample_t* sp = enaFl ? ((sample_t*)pp->audioBytesPtr) + j : _theBuf.zeroBuf; + const sample_t* sp = enaFl ? ((sample_t*)pp->audioBytesPtr) + j : p->zeroBuf; unsigned ssn = enaFl ? pp->chCnt : 1; // stride (packet samples are interleaved) sample_t* dp = cp->b + cp->ii; const sample_t* ep = dp + n0; @@ -426,7 +445,7 @@ cw::rc_t cw::audio::buf::update( for(i=0; idevIdx].ioArray + kOutApIdx; // dest io recd + cmApIO* op = p->devArray[pp->devIdx].ioArray + kOutApIdx; // dest io recd // if the base timestamp has not yet been set then set it. if( op->timeStamp.tv_sec==0 && op->timeStamp.tv_nsec==0 ) @@ -474,7 +493,7 @@ cw::rc_t cw::audio::buf::update( } else // otherwise copy samples from the output buffer to the packet { - const sample_t* sp = enaFl ? cp->b + cp->oi : _theBuf.zeroBuf; + const sample_t* sp = enaFl ? cp->b + cp->oi : p->zeroBuf; const sample_t* ep = sp + n0; // copy the first segment @@ -485,7 +504,7 @@ cw::rc_t cw::audio::buf::update( if( n1 > 0 ) { // copy the second segment - sp = enaFl ? cp->b : _theBuf.zeroBuf; + sp = enaFl ? cp->b : p->zeroBuf; ep = sp + n1; for(; spchCnt ) *dp = cp->gain * *sp++; @@ -511,119 +530,136 @@ cw::rc_t cw::audio::buf::update( return kOkRC; } -unsigned cw::audio::buf::meterMs() -{ return _theBuf.meterMs; } - -void cw::audio::buf::setMeterMs( unsigned meterMs ) -{ _theBuf.meterMs = std::min(1000u,std::max(10u,meterMs)); } - -unsigned cw::audio::buf::channelCount( unsigned devIdx, unsigned flags ) +unsigned cw::audio::buf::meterMs( handle_t h ) { - if( devIdx == kInvalidIdx ) - return 0; - - unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx; - return _theBuf.devArray[devIdx].ioArray[ idx ].chCnt; + audioBuf_t* p = _handleToPtr(h); + return p->meterMs; } -void cw::audio::buf::setFlag( unsigned devIdx, unsigned chIdx, unsigned flags ) +void cw::audio::buf::setMeterMs( handle_t h, unsigned meterMs ) { + audioBuf_t* p = _handleToPtr(h); + + p->meterMs = std::min(1000u,std::max(10u,meterMs)); +} + +unsigned cw::audio::buf::channelCount( handle_t h, unsigned devIdx, unsigned flags ) +{ + + if( devIdx == kInvalidIdx ) + return 0; + audioBuf_t* p = _handleToPtr(h); + + unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx; + return p->devArray[devIdx].ioArray[ idx ].chCnt; +} + +void cw::audio::buf::setFlag( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ) +{ + if( devIdx == kInvalidIdx ) return; + audioBuf_t* p = _handleToPtr(h); unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx; bool enableFl = flags & kEnableFl ? true : false; unsigned i = chIdx != kInvalidIdx ? chIdx : 0; - unsigned n = chIdx != kInvalidIdx ? chIdx+1 : _theBuf.devArray[devIdx].ioArray[idx].chCnt; + unsigned n = chIdx != kInvalidIdx ? chIdx+1 : p->devArray[devIdx].ioArray[idx].chCnt; for(; idevArray[devIdx].ioArray[idx].chArray + i; cp->fl = cwEnaFlag(cp->fl, flags & (kChFl|kToneFl|kMeterFl|kMuteFl|kPassFl), enableFl ); } } -bool cw::audio::buf::isFlag( unsigned devIdx, unsigned chIdx, unsigned flags ) +bool cw::audio::buf::isFlag( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ) { if( devIdx == kInvalidIdx ) return false; + audioBuf_t* p = _handleToPtr(h); unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx; - return cwIsFlag(_theBuf.devArray[devIdx].ioArray[idx].chArray[chIdx].fl,flags); + return cwIsFlag(p->devArray[devIdx].ioArray[idx].chArray[chIdx].fl,flags); } -void cw::audio::buf::enableChannel( unsigned devIdx, unsigned chIdx, unsigned flags ) -{ setFlag(devIdx,chIdx,flags | kChFl); } +void cw::audio::buf::enableChannel( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ) +{ setFlag(h,devIdx,chIdx,flags | kChFl); } -bool cw::audio::buf::isChannelEnabled( unsigned devIdx, unsigned chIdx, unsigned flags ) -{ return isFlag(devIdx, chIdx, flags | kChFl); } +bool cw::audio::buf::isChannelEnabled( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ) +{ return isFlag(h,devIdx, chIdx, flags | kChFl); } -void cw::audio::buf::enableTone( unsigned devIdx, unsigned chIdx, unsigned flags ) -{ setFlag(devIdx,chIdx,flags | kToneFl); } +void cw::audio::buf::enableTone( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ) +{ setFlag(h,devIdx,chIdx,flags | kToneFl); } -bool cw::audio::buf::isToneEnabled( unsigned devIdx, unsigned chIdx, unsigned flags ) -{ return isFlag(devIdx,chIdx,flags | kToneFl); } +bool cw::audio::buf::isToneEnabled( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ) +{ return isFlag(h,devIdx,chIdx,flags | kToneFl); } -void cw::audio::buf::enableMute( unsigned devIdx, unsigned chIdx, unsigned flags ) -{ setFlag(devIdx,chIdx,flags | kMuteFl); } +void cw::audio::buf::enableMute( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ) +{ setFlag(h,devIdx,chIdx,flags | kMuteFl); } -bool cw::audio::buf::isMuteEnabled( unsigned devIdx, unsigned chIdx, unsigned flags ) -{ return isFlag(devIdx,chIdx,flags | kMuteFl); } +bool cw::audio::buf::isMuteEnabled( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ) +{ return isFlag(h,devIdx,chIdx,flags | kMuteFl); } -void cw::audio::buf::enablePass( unsigned devIdx, unsigned chIdx, unsigned flags ) -{ setFlag(devIdx,chIdx,flags | kPassFl); } +void cw::audio::buf::enablePass( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ) +{ setFlag(h,devIdx,chIdx,flags | kPassFl); } -bool cw::audio::buf::isPassEnabled( unsigned devIdx, unsigned chIdx, unsigned flags ) -{ return isFlag(devIdx,chIdx,flags | kPassFl); } +bool cw::audio::buf::isPassEnabled( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ) +{ return isFlag(h,devIdx,chIdx,flags | kPassFl); } -void cw::audio::buf::enableMeter( unsigned devIdx, unsigned chIdx, unsigned flags ) -{ setFlag(devIdx,chIdx,flags | kMeterFl); } +void cw::audio::buf::enableMeter( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ) +{ setFlag(h,devIdx,chIdx,flags | kMeterFl); } -bool cw::audio::buf::isMeterEnabled(unsigned devIdx, unsigned chIdx, unsigned flags ) -{ return isFlag(devIdx,chIdx,flags | kMeterFl); } +bool cw::audio::buf::isMeterEnabled(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ) +{ return isFlag(h,devIdx,chIdx,flags | kMeterFl); } -cw::audio::buf::sample_t cw::audio::buf::meter(unsigned devIdx, unsigned chIdx, unsigned flags ) +cw::audio::buf::sample_t cw::audio::buf::meter(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ) { if( devIdx == kInvalidIdx ) return 0; + audioBuf_t* p = _handleToPtr(h); + unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx; - const cmApCh* cp = _theBuf.devArray[devIdx].ioArray[idx].chArray + chIdx; + const cmApCh* cp = p->devArray[devIdx].ioArray[idx].chArray + chIdx; return _cmApMeterValue(cp); } -void cw::audio::buf::setGain( unsigned devIdx, unsigned chIdx, unsigned flags, double gain ) +void cw::audio::buf::setGain( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags, double gain ) { if( devIdx == kInvalidIdx ) return; + audioBuf_t* p = _handleToPtr(h); unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx; unsigned i = chIdx != kInvalidIdx ? chIdx : 0; - unsigned n = i + (chIdx != kInvalidIdx ? 1 : _theBuf.devArray[devIdx].ioArray[idx].chCnt); + unsigned n = i + (chIdx != kInvalidIdx ? 1 : p->devArray[devIdx].ioArray[idx].chCnt); for(; idevArray[devIdx].ioArray[idx].chArray[i].gain = gain; } -double cw::audio::buf::gain( unsigned devIdx, unsigned chIdx, unsigned flags ) +double cw::audio::buf::gain( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ) { if( devIdx == kInvalidIdx ) return 0; + audioBuf_t* p = _handleToPtr(h); unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx; - return _theBuf.devArray[devIdx].ioArray[idx].chArray[chIdx].gain; + return p->devArray[devIdx].ioArray[idx].chArray[chIdx].gain; } -unsigned cw::audio::buf::getStatus( unsigned devIdx, unsigned flags, double* meterArray, unsigned meterCnt, unsigned* faultCntPtr ) +unsigned cw::audio::buf::getStatus( handle_t h, unsigned devIdx, unsigned flags, double* meterArray, unsigned meterCnt, unsigned* faultCntPtr ) { if( devIdx == kInvalidIdx ) return 0; + audioBuf_t* p = _handleToPtr(h); unsigned ioIdx = cwIsFlag(flags,kInFl) ? kInApIdx : kOutApIdx; - cmApIO* iop = _theBuf.devArray[devIdx].ioArray + ioIdx; + cmApIO* iop = p->devArray[devIdx].ioArray + ioIdx; unsigned chCnt = std::min(iop->chCnt, meterCnt ); unsigned i; @@ -636,18 +672,19 @@ unsigned cw::audio::buf::getStatus( unsigned devIdx, unsigned flags, double* met } -bool cw::audio::buf::isDeviceReady( unsigned devIdx, unsigned flags ) -{ +bool cw::audio::buf::isDeviceReady( handle_t h, unsigned devIdx, unsigned flags ) +{ //bool iFl = true; //bool oFl = true; unsigned i = 0; if( devIdx == kInvalidIdx ) return false; + audioBuf_t* p = _handleToPtr(h); if( flags & kInFl ) { - const cmApIO* ioPtr = _theBuf.devArray[devIdx].ioArray + kInApIdx; + const cmApIO* ioPtr = p->devArray[devIdx].ioArray + kInApIdx; for(i=0; ichCnt; ++i) if( ioPtr->chArray[i].fn < ioPtr->dspFrameCnt ) return false; @@ -657,7 +694,7 @@ bool cw::audio::buf::isDeviceReady( unsigned devIdx, unsigned flags ) if( flags & kOutFl ) { - const cmApIO* ioPtr = _theBuf.devArray[devIdx].ioArray + kOutApIdx; + const cmApIO* ioPtr = p->devArray[devIdx].ioArray + kOutApIdx; for(i=0; ichCnt; ++i) if( (ioPtr->n - ioPtr->chArray[i].fn) < ioPtr->dspFrameCnt ) @@ -674,8 +711,9 @@ bool cw::audio::buf::isDeviceReady( unsigned devIdx, unsigned flags ) // Note that his function returns audio samples but does NOT // change any internal states. -void cw::audio::buf::get( unsigned devIdx, unsigned flags, sample_t* bufArray[], unsigned bufChCnt ) +void cw::audio::buf::get( handle_t h, unsigned devIdx, unsigned flags, sample_t* bufArray[], unsigned bufChCnt ) { + audioBuf_t* p = _handleToPtr(h); unsigned i; if( devIdx == kInvalidIdx ) { @@ -685,7 +723,7 @@ void cw::audio::buf::get( unsigned devIdx, unsigned flags, sample_t* bufArray[], } unsigned idx = flags & kInFl ? kInApIdx : kOutApIdx; - const cmApIO* ioPtr = _theBuf.devArray[devIdx].ioArray + idx; + const cmApIO* ioPtr = p->devArray[devIdx].ioArray + idx; unsigned n = bufChCnt < ioPtr->chCnt ? bufChCnt : ioPtr->chCnt; //unsigned offs = flags & kInFl ? ioPtr->oi : ioPtr->ii; cmApCh* cp = ioPtr->chArray; @@ -698,17 +736,18 @@ void cw::audio::buf::get( unsigned devIdx, unsigned flags, sample_t* bufArray[], } -void cw::audio::buf::getIO( unsigned iDevIdx, sample_t* iBufArray[], unsigned iBufChCnt, time::spec_t* iTimeStampPtr, unsigned oDevIdx, sample_t* oBufArray[], unsigned oBufChCnt, time::spec_t* oTimeStampPtr ) +void cw::audio::buf::getIO( handle_t h, unsigned iDevIdx, sample_t* iBufArray[], unsigned iBufChCnt, time::spec_t* iTimeStampPtr, unsigned oDevIdx, sample_t* oBufArray[], unsigned oBufChCnt, time::spec_t* oTimeStampPtr ) { - get( iDevIdx, kInFl, iBufArray, iBufChCnt ); - get( oDevIdx, kOutFl,oBufArray, oBufChCnt ); + audioBuf_t* p = _handleToPtr(h); + get( h, iDevIdx, kInFl, iBufArray, iBufChCnt ); + get( h, oDevIdx, kOutFl,oBufArray, oBufChCnt ); unsigned i = 0; if( iDevIdx != kInvalidIdx && oDevIdx != kInvalidIdx ) { - const cmApIO* ip = _theBuf.devArray[iDevIdx].ioArray + kInApIdx; - const cmApIO* op = _theBuf.devArray[oDevIdx].ioArray + kOutApIdx; + const cmApIO* ip = p->devArray[iDevIdx].ioArray + kInApIdx; + const cmApIO* op = p->devArray[oDevIdx].ioArray + kOutApIdx; unsigned minChCnt = std::min(iBufChCnt,oBufChCnt); unsigned frmCnt = std::min(ip->dspFrameCnt,op->dspFrameCnt); unsigned byteCnt = frmCnt * sizeof(sample_t); @@ -742,7 +781,7 @@ void cw::audio::buf::getIO( unsigned iDevIdx, sample_t* iBufArray[], unsigned if( oDevIdx != kInvalidIdx ) { - const cmApIO* op = _theBuf.devArray[oDevIdx].ioArray + kOutApIdx; + const cmApIO* op = p->devArray[oDevIdx].ioArray + kOutApIdx; unsigned byteCnt = op->dspFrameCnt * sizeof(sample_t); _theBufCalcTimeStamp(op->srate, &op->timeStamp, op->ioFrameCnt, oTimeStampPtr ); @@ -753,16 +792,17 @@ void cw::audio::buf::getIO( unsigned iDevIdx, sample_t* iBufArray[], unsigned } } -void cw::audio::buf::advance( unsigned devIdx, unsigned flags ) +void cw::audio::buf::advance( handle_t h, unsigned devIdx, unsigned flags ) { unsigned i; if( devIdx == kInvalidIdx ) return; + audioBuf_t* p = _handleToPtr(h); if( flags & kInFl ) { - cmApIO* ioPtr = _theBuf.devArray[devIdx].ioArray + kInApIdx; + cmApIO* ioPtr = p->devArray[devIdx].ioArray + kInApIdx; for(i=0; ichCnt; ++i) { @@ -782,7 +822,7 @@ void cw::audio::buf::advance( unsigned devIdx, unsigned flags ) if( flags & kOutFl ) { - cmApIO* ioPtr = _theBuf.devArray[devIdx].ioArray + kOutApIdx; + cmApIO* ioPtr = p->devArray[devIdx].ioArray + kOutApIdx; for(i=0; ichCnt; ++i) { cmApCh* cp = ioPtr->chArray + i; @@ -800,13 +840,14 @@ void cw::audio::buf::advance( unsigned devIdx, unsigned flags ) } -void cw::audio::buf::inputToOutput( unsigned iDevIdx, unsigned oDevIdx ) +void cw::audio::buf::inputToOutput( handle_t h, unsigned iDevIdx, unsigned oDevIdx ) { if( iDevIdx == kInvalidIdx || oDevIdx == kInvalidIdx ) return; + audioBuf_t* p = _handleToPtr(h); - unsigned iChCnt = channelCount( iDevIdx, kInFl ); - unsigned oChCnt = channelCount( oDevIdx, kOutFl ); + unsigned iChCnt = channelCount( h, iDevIdx, kInFl ); + unsigned oChCnt = channelCount( h, oDevIdx, kOutFl ); unsigned chCnt = iChCnt < oChCnt ? iChCnt : oChCnt; unsigned i; @@ -815,17 +856,17 @@ void cw::audio::buf::inputToOutput( unsigned iDevIdx, unsigned oDevIdx ) sample_t* oBufPtrArray[ oChCnt ]; - while( isDeviceReady( iDevIdx, kInFl ) && isDeviceReady( oDevIdx, kOutFl ) ) + while( isDeviceReady( h, iDevIdx, kInFl ) && isDeviceReady( h, oDevIdx, kOutFl ) ) { - get( iDevIdx, kInFl, iBufPtrArray, iChCnt ); - get( oDevIdx, kOutFl, oBufPtrArray, oChCnt ); + get( h, iDevIdx, kInFl, iBufPtrArray, iChCnt ); + get( h, oDevIdx, kOutFl, oBufPtrArray, oChCnt ); // Warning: buffer pointers to disabled channels will be set to NULL for(i=0; idevArray[iDevIdx].ioArray + kInApIdx; + cmApIO* op = p->devArray[oDevIdx].ioArray + kOutApIdx; cwAssert( ip->dspFrameCnt == op->dspFrameCnt ); @@ -842,20 +883,21 @@ void cw::audio::buf::inputToOutput( unsigned iDevIdx, unsigned oDevIdx ) } } - advance( iDevIdx, kInFl ); - advance( oDevIdx, kOutFl ); + advance( h, iDevIdx, kInFl ); + advance( h, oDevIdx, kOutFl ); } } -void cw::audio::buf::report() +void cw::audio::buf::report(handle_t h) { + audioBuf_t* p = _handleToPtr(h); unsigned i,j,k; - for(i=0; i<_theBuf.devCnt; ++i) + for(i=0; idevCnt; ++i) { for(j=0; jdevArray[i].ioArray + j; unsigned ii = 0; unsigned oi = 0; @@ -902,7 +944,8 @@ void cw::audio::buf::test() sample_t* os = oSig; device::audioPacket_t pkt; unsigned i,j; - + handle_t h; + // create a simulated signal for(i=0; i handle_t; + // Allocate and initialize an audio buffer. // devCnt - count of devices this buffer will handle. // meterMs - length of the meter buffers in milliseconds (automatically limit to the range:10 to 1000) - rc_t initialize( unsigned devCnt, unsigned meterMs ); + rc_t create( handle_t& hRef, unsigned devCnt, unsigned meterMs ); // Deallocate and release any resource held by an audio buffer allocated via initialize(). - rc_t finalize(); + rc_t destroy( handle_t& hRef ); // Configure a buffer for a given device. - rc_t setup( + rc_t setup( + handle_t h, unsigned devIdx, //< device to setup double srate, //< device sample rate (only required for synthesizing the correct test-tone frequency) unsigned dspFrameCnt, // dspFrameCnt - count of samples in channel buffers returned via get() @@ -69,10 +67,10 @@ namespace cw ); // Prime the buffer with 'audioCycleCnt' * outFramesPerCycle samples ready to be played - rc_t primeOutput( unsigned devIdx, unsigned audioCycleCnt ); + rc_t primeOutput( handle_t h, unsigned devIdx, unsigned audioCycleCnt ); // Notify the audio buffer that a device is being enabled or disabled. - void onPortEnable( unsigned devIdx, bool enabelFl ); + void onPortEnable( handle_t h, unsigned devIdx, bool enabelFl ); // This function is called asynchronously by the audio device driver to transfer incoming samples to the // the buffer and to send outgoing samples to the DAC. This function is @@ -91,6 +89,7 @@ namespace cw // The enable flag has higher precedence than the tone flag therefore disabled channels // will be set to zero even if the tone flag is set. rc_t update( + handle_t h, device::audioPacket_t* inPktArray, //< full audio packets from incoming audio (from ADC) unsigned inPktCnt, //< count of incoming audio packets device::audioPacket_t* outPktArray, //< empty audio packet for outgoing audio (to DAC) @@ -112,78 +111,78 @@ namespace cw }; // Return the meter window period as set by initialize() - unsigned meterMs(); + unsigned meterMs(handle_t h); // Set the meter update period. THis function limits the value to between 10 and 1000. - void setMeterMs( unsigned meterMs ); + void setMeterMs( handle_t h, unsigned meterMs ); // Returns the channel count set via setup(). - unsigned channelCount( unsigned devIdx, unsigned flags ); + unsigned channelCount( handle_t h, unsigned devIdx, unsigned flags ); // Set chIdx to -1 to enable all channels on this device. // Set flags to {kInFl | kOutFl} | {kChFl | kToneFl | kMeterFl} | { kEnableFl=on | 0=off } - void setFlag( unsigned devIdx, unsigned chIdx, unsigned flags ); + void setFlag( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ); // Return true if the the flags is set. - bool isFlag( unsigned devIdx, unsigned chIdx, unsigned flags ); + bool isFlag( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ); // Set chIdx to -1 to enable all channels on this device. - void enableChannel( unsigned devIdx, unsigned chIdx, unsigned flags ); + void enableChannel( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ); // Returns true if an input/output channel is enabled on the specified device. - bool isChannelEnabled(unsigned devIdx, unsigned chIdx, unsigned flags ); + bool isChannelEnabled(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ); // Set the state of the tone generator on the specified channel. // Set chIdx to -1 to apply the change to all channels on this device. // Set flags to {kInFl | kOutFl} | { kEnableFl=on | 0=off } - void enableTone( unsigned devIdx, unsigned chIdx, unsigned flags ); + void enableTone( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ); // Returns true if an input/output tone is enabled on the specified device. - bool isToneEnabled(unsigned devIdx, unsigned chIdx, unsigned flags ); + bool isToneEnabled(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ); // Mute a specified channel. // Set chIdx to -1 to apply the change to all channels on this device. // Set flags to {kInFl | kOutFl} | { kEnableFl=on | 0=off } - void enableMute( unsigned devIdx, unsigned chIdx, unsigned flags ); + void enableMute( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ); // Returns true if an input/output channel is muted on the specified device. - bool isMuteEnabled(unsigned devIdx, unsigned chIdx, unsigned flags ); + bool isMuteEnabled(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ); // Set the specified channel to pass through. // Set chIdx to -1 to apply the change to all channels on this device. // Set flags to {kInFl | kOutFl} | { kEnableFl=on | 0=off } - void enablePass( unsigned devIdx, unsigned chIdx, unsigned flags ); + void enablePass( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ); // Returns true if pass through is enabled on the specified channel. - bool isPassEnabled(unsigned devIdx, unsigned chIdx, unsigned flags ); + bool isPassEnabled(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ); // Turn meter data collection on and off. // Set chIdx to -1 to apply the change to all channels on this device. // Set flags to {kInFl | kOutFl} | { kEnableFl=on | 0=off } - void enableMeter( unsigned devIdx, unsigned chIdx, unsigned flags ); + void enableMeter( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ); // Returns true if an input/output tone is enabled on the specified device. - bool isMeterEnabled(unsigned devIdx, unsigned chIdx, unsigned flags ); + bool isMeterEnabled(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ); // Return the meter value for the requested channel. // Set flags to kInFl | kOutFl. - sample_t meter(unsigned devIdx, unsigned chIdx, unsigned flags ); + sample_t meter(handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ); // Set chIdx to -1 to apply the gain to all channels on the specified device. - void setGain( unsigned devIdx, unsigned chIdx, unsigned flags, double gain ); + void setGain( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags, double gain ); // Return the current gain seting for the specified channel. - double gain( unsigned devIdx, unsigned chIdx, unsigned flags ); + double gain( handle_t h, unsigned devIdx, unsigned chIdx, unsigned flags ); // Get the meter and fault status of the channel input or output channel array of a device. // Set 'flags' to { kInFl | kOutFl }. // The returns value is the count of channels actually written to meterArray. // If 'faultCntPtr' is non-NULL then it is set to the faultCnt of the associated devices input or output buffer. - unsigned getStatus( unsigned devIdx, unsigned flags, double* meterArray, unsigned meterCnt, unsigned* faultCntPtr ); + unsigned getStatus( handle_t h, unsigned devIdx, unsigned flags, double* meterArray, unsigned meterCnt, unsigned* faultCntPtr ); // Do all enabled input/output channels on this device have samples available? // 'flags' can be set to either or both kInFl and kOutFl - bool isDeviceReady( unsigned devIdx, unsigned flags ); + bool isDeviceReady( handle_t h, unsigned devIdx, unsigned flags ); // This function is called by the application to get full incoming sample buffers and // to fill empty outgoing sample buffers. @@ -195,7 +194,7 @@ namespace cw // (see initialize()). // Note that this function just returns audio information it does not // change any internal states. - void get( unsigned devIdx, unsigned flags, sample_t* bufArray[], unsigned bufChCnt ); + void get( handle_t h, unsigned devIdx, unsigned flags, sample_t* bufArray[], unsigned bufChCnt ); // This function replaces calls to get() and implements pass-through and output // buffer zeroing: @@ -212,22 +211,22 @@ namespace cw // 3) This function just returns audio information it does not // change any internal states. // 4) The timestamp pointers are optional. - void getIO( unsigned iDevIdx, sample_t* iBufArray[], unsigned iBufChCnt, time::spec_t* iTimeStampPtr, + void getIO( handle_t h, unsigned iDevIdx, sample_t* iBufArray[], unsigned iBufChCnt, time::spec_t* iTimeStampPtr, unsigned oDevIdx, sample_t* oBufArray[], unsigned oBufChCnt, time::spec_t* oTimeStampPtr ); // The application calls this function each time it completes processing of a bufArray[] // returned from get(). 'flags' can be set to either or both kInFl and kOutFl. // This function should only be called from the client thread. - void advance( unsigned devIdx, unsigned flags ); + void advance( handle_t h, unsigned devIdx, unsigned flags ); // Copy all available samples incoming samples from an input device to an output device. // The source code for this example is a good example of how an application should use get() // and advance(). - void inputToOutput( unsigned inDevIdx, unsigned outDevIdx ); + void inputToOutput( handle_t h, unsigned inDevIdx, unsigned outDevIdx ); // Print the current buffer state. - void report(); + void report( handle_t h ); // Run a buffer usage simulation to test the class. cmAudioPortTest.c calls this function. void test(); diff --git a/cwAudioDevice.cpp b/cwAudioDevice.cpp index 614fb17..cbd2b7e 100644 --- a/cwAudioDevice.cpp +++ b/cwAudioDevice.cpp @@ -34,11 +34,11 @@ namespace cw while( d != nullptr ) { drv_t* d0 = d->link; - memRelease(d); + mem::release(d); d = d0; } - memRelease(p); + mem::release(p); return kOkRC; } @@ -70,7 +70,7 @@ cw::rc_t cw::audio::device::create( handle_t& hRef ) if((rc = destroy(hRef)) != kOkRC) return rc; - device_t* p = memAllocZ(); + device_t* p = mem::allocZ(); hRef.set(p); return rc; @@ -99,7 +99,7 @@ cw::rc_t cw::audio::device::registerDriver( handle_t h, driver_t* drv ) unsigned n = drv->deviceCount( drv ); if( n > 0 ) { - drv_t* d = memAllocZ(); + drv_t* d = mem::allocZ(); d->begIdx = p->nextDrvIdx; p->nextDrvIdx += n; diff --git a/cwAudioDevice.h b/cwAudioDevice.h index de01ef1..a6c30fa 100644 --- a/cwAudioDevice.h +++ b/cwAudioDevice.h @@ -70,7 +70,7 @@ namespace cw // interacts with data structures also handled by the application. The audio buffer class (\see cwAudioBuf.h) // is designed to provide a safe and efficient way to communicate between // the audio thread and the application. - typedef void (*cbFunc_t)( audioPacket_t* inPktArray, unsigned inPktCnt, audioPacket_t* outPktArray, unsigned outPktCnt ); + typedef void (*cbFunc_t)( void* cbArg, audioPacket_t* inPktArray, unsigned inPktCnt, audioPacket_t* outPktArray, unsigned outPktCnt ); typedef struct driver_str { diff --git a/cwAudioDeviceAlsa.cpp b/cwAudioDeviceAlsa.cpp index 5aba227..945b85c 100644 --- a/cwAudioDeviceAlsa.cpp +++ b/cwAudioDeviceAlsa.cpp @@ -331,7 +331,7 @@ namespace cw const int reallocN = 5; if( p->devCnt == p->devAllocCnt ) { - p->devArray = memResizeZ( p->devArray, p->devAllocCnt + reallocN ); + p->devArray = mem::resizeZ( p->devArray, p->devAllocCnt + reallocN ); p->devAllocCnt += reallocN; } @@ -397,21 +397,21 @@ namespace cw _devShutdown(p,p->devArray+i,true); _devShutdown(p,p->devArray+i,false); - memRelease(p->devArray[i].iBuf); - memRelease(p->devArray[i].oBuf); - memRelease(p->devArray[i].nameStr); - memRelease(p->devArray[i].descStr); + mem::release(p->devArray[i].iBuf); + mem::release(p->devArray[i].oBuf); + mem::release(p->devArray[i].nameStr); + mem::release(p->devArray[i].descStr); } - memRelease(p->pollfds); - memRelease(p->pollfdsDesc); + mem::release(p->pollfds); + mem::release(p->pollfdsDesc); - memRelease(p->devArray); + mem::release(p->devArray); p->devAllocCnt = 0; p->devCnt = 0; - memRelease(p); + mem::release(p); return rc; } @@ -759,7 +759,7 @@ namespace cw { pkt.audioFramesCnt = err; - drp->cbFunc(&pkt,1,NULL,0 ); // send the samples to the application + drp->cbFunc(drp->cbArg,&pkt,1,NULL,0 ); // send the samples to the application } } @@ -767,7 +767,7 @@ namespace cw else { // callback to fill the buffer - drp->cbFunc(NULL,0,&pkt,1); + drp->cbFunc(drp->cbArg,NULL,0,&pkt,1); // note that the application may return fewer samples than were requested err = _devWriteBuf(drp, pcmH, pkt.audioFramesCnt < frmCnt ? NULL : drp->oBuf,chCnt,frmCnt,drp->oBits,drp->oSigBits); @@ -900,7 +900,7 @@ namespace cw if((err = _devReadBuf(drp,pcmH,drp->iBuf,chCnt,frmCnt,drp->iBits,drp->oBits)) > 0 ) { pkt.audioFramesCnt = err; - drp->cbFunc(&pkt,1,NULL,0 ); // send the samples to the application + drp->cbFunc(drp->cbArg,&pkt,1,NULL,0 ); // send the samples to the application } } @@ -909,7 +909,7 @@ namespace cw { // callback to fill the buffer - drp->cbFunc(NULL,0,&pkt,1); + drp->cbFunc(drp->cbArg,NULL,0,&pkt,1); // note that the application may return fewer samples than were requested err = _devWriteBuf(drp, pcmH, pkt.audioFramesCnt < frmCnt ? NULL : drp->oBuf,chCnt,frmCnt,drp->oBits,drp->oSigBits); @@ -1092,7 +1092,7 @@ namespace cw drp->iSignFl = signFl; drp->iSwapFl = swapFl; drp->iPcmH = pcmH; - drp->iBuf = memResizeZ( drp->iBuf, actFpC * drp->iChCnt ); + drp->iBuf = mem::resizeZ( drp->iBuf, actFpC * drp->iChCnt ); drp->iFpC = actFpC; } else @@ -1102,7 +1102,7 @@ namespace cw drp->oSignFl = signFl; drp->oSwapFl = swapFl; drp->oPcmH = pcmH; - drp->oBuf = memResizeZ( drp->oBuf, actFpC * drp->oChCnt ); + drp->oBuf = mem::resizeZ( drp->oBuf, actFpC * drp->oChCnt ); drp->oFpC = actFpC; } @@ -1131,8 +1131,8 @@ namespace cw // if the pollfds[] needs more memroy if( p->pollfdsCnt + fdsCnt > p->pollfdsAllocCnt ) { - p->pollfds = memResizeZ( p->pollfds, p->pollfdsCnt + fdsCnt ); - p->pollfdsDesc = memResizeZ( p->pollfdsDesc, p->pollfdsCnt + fdsCnt ); + p->pollfds = mem::resizeZ( p->pollfds, p->pollfdsCnt + fdsCnt ); + p->pollfdsDesc = mem::resizeZ( p->pollfdsDesc, p->pollfdsCnt + fdsCnt ); p->pollfdsAllocCnt += fdsCnt; } } @@ -1174,7 +1174,7 @@ cw::rc_t cw::audio::device::alsa::create( handle_t& hRef, struct driver_str*& dr if((rc = destroy(hRef)) != kOkRC ) return rc; - alsa_t* p = memAllocZ(); + alsa_t* p = mem::allocZ(); // for each sound card while(1) @@ -1277,8 +1277,8 @@ cw::rc_t cw::audio::device::alsa::create( handle_t& hRef, struct driver_str*& dr continue; // form the device name and desc. string - dr.nameStr = memPrintf(dr.nameStr,"hw:%i,%i,%i",cardNum,devNum,i); - dr.descStr = memPrintf(dr.descStr,"%s %s",cardNamePtr,snd_pcm_info_get_name(info)); + dr.nameStr = mem::printf(dr.nameStr,"hw:%i,%i,%i",cardNum,devNum,i); + dr.descStr = mem::printf(dr.descStr,"%s %s",cardNamePtr,snd_pcm_info_get_name(info)); // attempt to open the sub-device if((err = _devOpen(&pcmH,dr.nameStr,inputFl)) < 0 ) @@ -1320,8 +1320,8 @@ cw::rc_t cw::audio::device::alsa::create( handle_t& hRef, struct driver_str*& dr _devAppend(p,&dr); else { - memRelease(dr.nameStr); - memRelease(dr.descStr); + mem::release(dr.nameStr); + mem::release(dr.descStr); } } // sub-dev loop @@ -1343,8 +1343,8 @@ cw::rc_t cw::audio::device::alsa::create( handle_t& hRef, struct driver_str*& dr { p->pollfdsCnt = 0; p->pollfdsAllocCnt = 2*p->devCnt; - p->pollfds = memAllocZ( p->pollfdsAllocCnt ); - p->pollfdsDesc = memAllocZ(p->pollfdsAllocCnt ); + p->pollfds = mem::allocZ( p->pollfdsAllocCnt ); + p->pollfdsDesc = mem::allocZ(p->pollfdsAllocCnt ); if((rc = thread::create(p->thH,_threadFunc,p)) != kOkRC ) { @@ -1448,8 +1448,7 @@ cw::rc_t cw::audio::device::alsa::deviceSetup( struct driver_str* drv, unsigned drp->cbArg = cbArg; } - return rc; - + return rc; } cw::rc_t cw::audio::device::alsa::deviceStart( struct driver_str* drv, unsigned devIdx ) diff --git a/cwAudioDeviceTest.cpp b/cwAudioDeviceTest.cpp index 6007722..a5d087a 100644 --- a/cwAudioDeviceTest.cpp +++ b/cwAudioDeviceTest.cpp @@ -34,229 +34,14 @@ namespace cw unsigned outDevIdx; // output device index double srate; // audio sample rate unsigned meterMs; // audio meter buffer length - - // param's and state for cmApSynthSine() - unsigned phase; // sine synth phase - double frqHz; // sine synth frequency in Hz - - // buffer and state for cmApCopyIn/Out() - sample_t* buf; // buf[bufSmpCnt] - circular interleaved audio buffer - unsigned bufInIdx; // next input buffer index - unsigned bufOutIdx; // next output buffer index - unsigned bufFullCnt; // count of full samples - - // debugging log data arrays - unsigned logCnt; // count of elements in log[] and ilong[] - char* log; // log[logCnt] - unsigned* ilog; // ilog[logCnt] - unsigned logIdx; // current log index - + unsigned iCbCnt; // count the callback unsigned oCbCnt; + + buf::handle_t audioBufH; } cmApPortTestRecd; -#ifdef NOT_DEF - // The application can request any block of channels from the device. The packets are provided with the starting - // device channel and channel count. This function converts device channels and channel counts to buffer - // channel indexes and counts. - // - // Example: - // input output - // i,n i n - // App: 0,4 0 1 2 3 -> 2 2 - // Pkt 2,8 2 3 4 5 6 7 8 -> 0 2 - // - // The return value is the count of application requested channels located in this packet. - // - // input: *appChIdxPtr and appChCnt describe a block of device channels requested by the application. - // *pktChIdxPtr and pktChCnt describe a block of device channels provided to the application - // - // output:*appChIdxPtr and describe a block of app buffer channels which will send/recv samples. - // *pktChIdxPtr and describe a block of pkt buffer channels which will send/recv samples - // - unsigned _cmApDeviceToBuffer( unsigned* appChIdxPtr, unsigned appChCnt, unsigned* pktChIdxPtr, unsigned pktChCnt ) - { - unsigned abi = *appChIdxPtr; - unsigned aei = abi+appChCnt-1; - - unsigned pbi = *pktChIdxPtr; - unsigned pei = pbi+pktChCnt-1; - - // if the ch's rqstd by the app do not overlap with this packet - return false. - if( aei < pbi || abi > pei ) - return 0; - - // if the ch's rqstd by the app overlap with the beginning of the pkt channel block - if( abi < pbi ) - { - appChCnt -= pbi - abi; - *appChIdxPtr = pbi - abi; - *pktChIdxPtr = 0; - } - else - { - // the rqstd ch's begin inside the pkt channel block - pktChCnt -= abi - pbi; - *pktChIdxPtr = abi - pbi; - *appChIdxPtr = 0; - } - - // if the pkt channels extend beyond the rqstd ch block - if( aei < pei ) - pktChCnt -= pei - aei; - else - appChCnt -= aei - pei; // the rqstd ch's extend beyond or coincide with the pkt block - - // the returned channel count must always be the same for both the rqstd and pkt - return cmMin(appChCnt,pktChCnt); - - } - - - // synthesize a sine signal into an interleaved audio buffer - unsigned _cmApSynthSine( cmApPortTestRecd* r, float* p, unsigned chIdx, unsigned chCnt, unsigned frmCnt, unsigned phs, double hz ) - { - long ph = 0; - unsigned i; - unsigned bufIdx = r->chIdx; - unsigned bufChCnt; - - if( (bufChCnt = _cmApDeviceToBuffer( &bufIdx, r->chCnt, &chIdx, chCnt )) == 0) - return phs; - - - //if( r->cbCnt < 50 ) - // printf("ch:%i cnt:%i ch:%i cnt:%i bi:%i bcn:%i\n",r->chIdx,r->chCnt,chIdx,chCnt,bufIdx,bufChCnt); - - - for(i=bufIdx; israte )); - } - } - - return ph; - } - - // Copy the audio samples in the interleaved audio buffer sp[srcChCnt*srcFrameCnt] - // to the internal record buffer. - void _cmApCopyIn( cmApPortTestRecd* r, const sample_t* sp, unsigned srcChIdx, unsigned srcChCnt, unsigned srcFrameCnt ) - { - unsigned i,j; - - unsigned chCnt = cmMin(r->chCnt,srcChCnt); - - for(i=0; ibuf[ r->bufInIdx + j ] = sp[ (i*srcChCnt) + j ]; - - for(; jchCnt; ++j) - r->buf[ r->bufInIdx + j ] = 0; - - r->bufInIdx = (r->bufInIdx+r->chCnt) % r->bufFrmCnt; - } - - //r->bufFullCnt = (r->bufFullCnt + srcFrameCnt) % r->bufFrmCnt; - r->bufFullCnt += srcFrameCnt; - } - - // Copy audio samples out of the internal record buffer into dp[dstChCnt*dstFrameCnt]. - void _cmApCopyOut( cmApPortTestRecd* r, sample_t* dp, unsigned dstChIdx, unsigned dstChCnt, unsigned dstFrameCnt ) - { - // if there are not enough samples available to fill the destination buffer then zero the dst buf. - if( r->bufFullCnt < dstFrameCnt ) - { - printf("Empty Output Buffer\n"); - memset( dp, 0, dstFrameCnt*dstChCnt*sizeof(sample_t) ); - } - else - { - unsigned i,j; - unsigned chCnt = cmMin(dstChCnt, r->chCnt); - - // for each output frame - for(i=0; ibuf[ r->bufOutIdx + j ]; - - // zero any output ch's for which there is no internal buf channel - for(; jbufOutIdx = (r->bufOutIdx + r->chCnt) % r->bufFrmCnt; - } - - r->bufFullCnt -= dstFrameCnt; - } - } - - // Audio port callback function called from the audio device thread. - void _cmApPortCb( cmApAudioPacket_t* inPktArray, unsigned inPktCnt, cmApAudioPacket_t* outPktArray, unsigned outPktCnt ) - { - unsigned i; - - // for each incoming audio packet - for(i=0; iinDevIdx ) - { - // copy the incoming audio into an internal buffer where it can be picked up by _cpApCopyOut(). - _cmApCopyIn( r, (sample_t*)inPktArray[i].audioBytesPtr, inPktArray[i].begChIdx, inPktArray[i].chCnt, inPktArray[i].audioFramesCnt ); - } - ++r->iCbCnt; - - //printf("i %4i in:%4i out:%4i\n",r->bufFullCnt,r->bufInIdx,r->bufOutIdx); - } - - unsigned hold_phase = 0; - - // for each outgoing audio packet - for(i=0; ioutDevIdx ) - { - // zero the output buffer - memset(outPktArray[i].audioBytesPtr,0,outPktArray[i].chCnt * outPktArray[i].audioFramesCnt * sizeof(sample_t) ); - - // if the synth is enabled - if( r->synthFl ) - { - unsigned tmp_phase = _cmApSynthSine( r, outPktArray[i].audioBytesPtr, outPktArray[i].begChIdx, outPktArray[i].chCnt, outPktArray[i].audioFramesCnt, r->phase, r->frqHz ); - - // the phase will only change on packets that are actually used - if( tmp_phase != r->phase ) - hold_phase = tmp_phase; - } - else - { - // copy the any audio in the internal record buffer to the playback device - _cmApCopyOut( r, (sample_t*)outPktArray[i].audioBytesPtr, outPktArray[i].begChIdx, outPktArray[i].chCnt, outPktArray[i].audioFramesCnt ); - } - } - - r->phase = hold_phase; - - //printf("o %4i in:%4i out:%4i\n",r->bufFullCnt,r->bufInIdx,r->bufOutIdx); - // count callbacks - ++r->oCbCnt; - } - } -#endif // print the usage message for cmAudioPortTest.c void _cmApPrintUsage() @@ -302,17 +87,19 @@ namespace cw unsigned _cmGlobalInDevIdx = 0; unsigned _cmGlobalOutDevIdx = 0; - void _cmApPortCb2( audioPacket_t* inPktArray, unsigned inPktCnt, audioPacket_t* outPktArray, unsigned outPktCnt ) + void _cmApPortCb2( void* arg, audioPacket_t* inPktArray, unsigned inPktCnt, audioPacket_t* outPktArray, unsigned outPktCnt ) { + cmApPortTestRecd* p = static_cast(arg); + for(unsigned i=0; i(inPktArray[i].cbArg)->iCbCnt++; for(unsigned i=0; i(outPktArray[i].cbArg)->oCbCnt++; - buf::inputToOutput( _cmGlobalInDevIdx, _cmGlobalOutDevIdx ); + buf::inputToOutput( p->audioBufH, _cmGlobalInDevIdx, _cmGlobalOutDevIdx ); - buf::update( inPktArray, inPktCnt, outPktArray, outPktCnt ); + buf::update( p->audioBufH, inPktArray, inPktCnt, outPktArray, outPktCnt ); } } } @@ -341,25 +128,11 @@ cw::rc_t cw::audio::device::test( int argc, const char** argv ) r.framesPerCycle = _cmApGetOpt(argc,argv,"-f",512); //r.bufFrmCnt = (r.bufCnt*r.framesPerCycle); //r.bufSmpCnt = (r.chCnt * r.bufFrmCnt); - r.logCnt = 100; + //r.logCnt = 100; r.meterMs = 50; - sample_t buf[r.bufSmpCnt]; - char log[r.logCnt]; - unsigned ilog[r.logCnt]; - r.inDevIdx = _cmGlobalInDevIdx = _cmApGetOpt(argc,argv,"-i",0); r.outDevIdx = _cmGlobalOutDevIdx = _cmApGetOpt(argc,argv,"-o",0); - r.phase = 0; - r.frqHz = 2000; - r.bufInIdx = 0; - r.bufOutIdx = 0; - r.bufFullCnt = 0; - r.logIdx = 0; - - r.buf = buf; - r.log = log; - r.ilog = ilog; r.iCbCnt = 0; r.oCbCnt = 0; @@ -399,14 +172,14 @@ cw::rc_t cw::audio::device::test( int argc, const char** argv ) if( runFl ) { // initialize the audio bufer - buf::initialize( deviceCount(h), r.meterMs ); + buf::create( r.audioBufH, deviceCount(h), r.meterMs ); // setup the buffer for the output device - buf::setup( r.outDevIdx, r.srate, r.framesPerCycle, r.bufCnt, deviceChannelCount(h,r.outDevIdx,true), r.framesPerCycle, deviceChannelCount(h,r.outDevIdx,false), r.framesPerCycle ); + buf::setup( r.audioBufH, r.outDevIdx, r.srate, r.framesPerCycle, r.bufCnt, deviceChannelCount(h,r.outDevIdx,true), r.framesPerCycle, deviceChannelCount(h,r.outDevIdx,false), r.framesPerCycle ); // setup the buffer for the input device //if( r.inDevIdx != r.outDevIdx ) - buf::setup( r.inDevIdx, r.srate, r.framesPerCycle, r.bufCnt, deviceChannelCount(h,r.inDevIdx,true), r.framesPerCycle, deviceChannelCount(h,r.inDevIdx,false), r.framesPerCycle ); + buf::setup( r.audioBufH, r.inDevIdx, r.srate, r.framesPerCycle, r.bufCnt, deviceChannelCount(h,r.inDevIdx,true), r.framesPerCycle, deviceChannelCount(h,r.inDevIdx,false), r.framesPerCycle ); // setup an output device if(deviceSetup(h, r.outDevIdx,r.srate,r.framesPerCycle,_cmApPortCb2,&r) != kOkRC ) @@ -429,8 +202,8 @@ cw::rc_t cw::audio::device::test( int argc, const char** argv ) cwLogInfo("q=quit O/o output tone, I/i input tone, P/p pass M/m meter s=buf report"); - //buf::enableTone( r.outDevIdx,-1,buf::kOutFl | buf::kEnableFl); - //buf::enableMeter(r.outDevIdx,-1,buf::kOutFl | buf::kEnableFl); + // turn on the meters + buf::enableMeter(r.audioBufH, r.outDevIdx,-1,buf::kOutFl | buf::kEnableFl); char c; while((c=getchar()) != 'q') @@ -441,27 +214,27 @@ cw::rc_t cw::audio::device::test( int argc, const char** argv ) { case 'i': case 'I': - buf::enableTone(r.inDevIdx,-1,buf::kInFl | (c=='I'?buf::kEnableFl:0)); + buf::enableTone(r.audioBufH, r.inDevIdx,-1,buf::kInFl | (c=='I'?buf::kEnableFl:0)); break; case 'o': case 'O': - buf::enableTone(r.outDevIdx,-1,buf::kOutFl | (c=='O'?buf::kEnableFl:0)); + buf::enableTone(r.audioBufH, r.outDevIdx,-1,buf::kOutFl | (c=='O'?buf::kEnableFl:0)); break; case 'p': case 'P': - buf::enablePass(r.outDevIdx,-1,buf::kOutFl | (c=='P'?buf::kEnableFl:0)); + buf::enablePass(r.audioBufH, r.outDevIdx,-1,buf::kOutFl | (c=='P'?buf::kEnableFl:0)); break; case 'M': case 'm': - buf::enableMeter( r.inDevIdx, -1, buf::kInFl | (c=='M'?buf::kEnableFl:0)); - buf::enableMeter( r.outDevIdx, -1, buf::kOutFl | (c=='M'?buf::kEnableFl:0)); + buf::enableMeter( r.audioBufH, r.inDevIdx, -1, buf::kInFl | (c=='M'?buf::kEnableFl:0)); + buf::enableMeter( r.audioBufH, r.outDevIdx, -1, buf::kOutFl | (c=='M'?buf::kEnableFl:0)); break; case 's': - buf::report(); + buf::report(r.audioBufH); break; } @@ -486,7 +259,7 @@ cw::rc_t cw::audio::device::test( int argc, const char** argv ) // release any resources held by the audio port interface rc_t rc1 = destroy(h); - rc_t rc2 = buf::finalize(); + rc_t rc2 = buf::destroy(r.audioBufH); //cmApNrtFree(); //cmApFileFree(); diff --git a/cwFile.cpp b/cwFile.cpp index 18738c2..42c3235 100644 --- a/cwFile.cpp +++ b/cwFile.cpp @@ -43,7 +43,7 @@ namespace cw } // allocate the read target buffer - if((buf = memAlloc(n+nn)) == nullptr) + if((buf = mem::alloc(n+nn)) == nullptr) { cwLogError(kMemAllocFailRC,"Read buffer allocation failed."); goto errLabel; @@ -65,7 +65,7 @@ namespace cw if( bufByteCntPtr != nullptr ) *bufByteCntPtr = 0; - memRelease(buf); + mem::release(buf); return nullptr; @@ -157,7 +157,7 @@ cw::rc_t cw::file::open( handle_t& hRef, const char* fn, unsigned flags ) unsigned byteCnt = sizeof(this_t) + strlen(fn) + 1; - if((p = memAllocZ(byteCnt)) == nullptr ) + if((p = mem::allocZ(byteCnt)) == nullptr ) return cwLogError(kOpFailRC,"File object allocation failed for file '%s'.",cwStringNullGuard(fn)); p->fnStr = (char*)(p+1); @@ -171,7 +171,7 @@ cw::rc_t cw::file::open( handle_t& hRef, const char* fn, unsigned flags ) if((p->fp = fopen(fn,mode)) == nullptr ) { rc_t rc = cwLogSysError(kOpenFailRC,errno,"File open failed on file:'%s'.",cwStringNullGuard(fn)); - memRelease(p); + mem::release(p); return rc; } } @@ -193,7 +193,7 @@ cw::rc_t cw::file::close( handle_t& hRef ) if( fclose(p->fp) != 0 ) return cwLogSysError(kCloseFailRC,errno,"File close failed on '%s'.", cwStringNullGuard(p->fnStr)); - memRelease(p); + mem::release(p); hRef.clear(); return kOkRC; @@ -386,21 +386,21 @@ cw::rc_t cw::file::copy( char* dstPathFn = nullptr; // form the source path fn - if((srcPathFn = fileSysMakeFn(srcDir,srcFn,srcExt,nullptr)) == nullptr ) + if((srcPathFn = filesys::makeFn(srcDir,srcFn,srcExt,nullptr)) == nullptr ) { rc = cwLogError(kOpFailRC,"The soure file name for dir:%s name:%s ext:%s could not be formed.",cwStringNullGuard(srcDir),cwStringNullGuard(srcFn),cwStringNullGuard(srcExt)); goto errLabel; } // form the dest path fn - if((dstPathFn = fileSysMakeFn(dstDir,dstFn,dstExt,nullptr)) == nullptr ) + if((dstPathFn = filesys::makeFn(dstDir,dstFn,dstExt,nullptr)) == nullptr ) { rc = cwLogError(kOpFailRC,"The destination file name for dir:%s name:%s ext:%s could not be formed.",cwStringNullGuard(dstDir),cwStringNullGuard(dstFn),cwStringNullGuard(dstExt)); goto errLabel; } // verify that the source exists - if( fileSysIsFile(srcPathFn) == false ) + if( filesys::isFile(srcPathFn) == false ) { rc = cwLogError(kOpenFailRC,"The source file '%s' does not exist.",cwStringNullGuard(srcPathFn)); goto errLabel; @@ -418,9 +418,9 @@ cw::rc_t cw::file::copy( errLabel: // free the buffer - memRelease(buf); - memRelease(srcPathFn); - memRelease(dstPathFn); + mem::release(buf); + mem::release(srcPathFn); + mem::release(dstPathFn); return rc; } @@ -432,21 +432,21 @@ cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext ) char* newFn = nullptr; unsigned n = 0; char* srcFn = nullptr; - fileSysPathPart_t* pp = nullptr; + filesys::pathPart_t* pp = nullptr; // form the name of the backup file - if((srcFn = fileSysMakeFn(dir,name,ext,nullptr)) == nullptr ) + if((srcFn = filesys::makeFn(dir,name,ext,nullptr)) == nullptr ) { rc = cwLogError(kOpFailRC,"Backup source file name formation failed."); goto errLabel; } // if the src file does not exist then there is nothing to do - if( fileSysIsFile(srcFn) == false ) + if( filesys::isFile(srcFn) == false ) return rc; // break the source file name up into dir/fn/ext. - if((pp = fileSysPathParts(srcFn)) == nullptr || pp->fnStr==nullptr) + if((pp = filesys::pathParts(srcFn)) == nullptr || pp->fnStr==nullptr) { rc = cwLogError(kOpFailRC,"The file name '%s' could not be parsed into its parts.",cwStringNullGuard(srcFn)); goto errLabel; @@ -455,20 +455,20 @@ cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext ) // iterate until a unique file name is found for(n=0; 1; ++n) { - memRelease(newFn); + mem::release(newFn); // generate a new file name - newName = memPrintf(newName,"%s_%i",pp->fnStr,n); + newName = mem::printf(newName,"%s_%i",pp->fnStr,n); // form the new file name into a complete path - if((newFn = fileSysMakeFn(pp->dirStr,newName,pp->extStr,nullptr)) == nullptr ) + if((newFn = filesys::makeFn(pp->dirStr,newName,pp->extStr,nullptr)) == nullptr ) { rc = cwLogError(kOpFailRC,"A backup file name could not be formed for the file '%s'.",cwStringNullGuard(newName)); goto errLabel; } // if the new file name is not already in use ... - if( fileSysIsFile(newFn) == false ) + if( filesys::isFile(newFn) == false ) { // .. then duplicate the file if((rc = copy(srcFn,nullptr,nullptr,newFn,nullptr,nullptr)) != kOkRC ) @@ -482,10 +482,10 @@ cw::rc_t cw::file::backup( const char* dir, const char* name, const char* ext ) errLabel: - memRelease(srcFn); - memRelease(newFn); - memRelease(newName); - memRelease(pp); + mem::release(srcFn); + mem::release(newFn); + mem::release(newName); + mem::release(pp); return rc; @@ -635,12 +635,12 @@ cw::rc_t cw::file::getLineAuto( handle_t h, char** bufPtrPtr, unsigned* bufByteC break; case kBufTooSmallRC: - buf = memResizeZ(buf,*bufByteCntPtr); + buf = mem::resizeZ(buf,*bufByteCntPtr); fl = true; break; default: - memRelease(buf); + mem::release(buf); break; } } @@ -757,7 +757,7 @@ cw::rc_t cw::file::readStr( handle_t h, char** sRef, unsigned maxCharN ) } // allocate a read buffer - char* s = memAllocZ(n+1); + char* s = mem::allocZ(n+1); // fill the buffer from the file if((rc = readChar(h,s,n)) != kOkRC ) diff --git a/cwFileSys.cpp b/cwFileSys.cpp index 6cf3d43..43b50ec 100644 --- a/cwFileSys.cpp +++ b/cwFileSys.cpp @@ -54,7 +54,7 @@ namespace cw } } -bool cw::fileSysIsDir( const char* dirStr ) +bool cw::filesys::isDir( const char* dirStr ) { struct stat s; @@ -73,7 +73,7 @@ bool cw::fileSysIsDir( const char* dirStr ) return S_ISDIR(s.st_mode); } -bool cw::fileSysIsFile( const char* fnStr ) +bool cw::filesys::isFile( const char* fnStr ) { struct stat s; errno = 0; @@ -93,7 +93,7 @@ bool cw::fileSysIsFile( const char* fnStr ) } -bool cw::fileSysIsLink( const char* fnStr ) +bool cw::filesys::isLink( const char* fnStr ) { struct stat s; errno = 0; @@ -113,7 +113,7 @@ bool cw::fileSysIsLink( const char* fnStr ) -char* cw::fileSysVMakeFn( const char* dir, const char* fn, const char* ext, va_list vl ) +char* cw::filesys::vMakeFn( const char* dir, const char* fn, const char* ext, va_list vl ) { rc_t rc = kOkRC; char* rp = nullptr; @@ -142,7 +142,7 @@ char* cw::fileSysVMakeFn( const char* dir, const char* fn, const char* ext, va_l // add 1 for terminating zero and allocate memory - if((rp = memAllocZ( n+1 )) == nullptr ) + if((rp = mem::allocZ( n+1 )) == nullptr ) { rc = cwLogError(kMemAllocFailRC,"Unable to allocate file name memory."); goto errLabel; @@ -187,31 +187,31 @@ char* cw::fileSysVMakeFn( const char* dir, const char* fn, const char* ext, va_l errLabel: if( rc != kOkRC && rp != nullptr ) - memRelease( rp ); + mem::release( rp ); return rp; } -char* cw::fileSysMakeFn( const char* dir, const char* fn, const char* ext, ... ) +char* cw::filesys::makeFn( const char* dir, const char* fn, const char* ext, ... ) { va_list vl; va_start(vl,ext); - char* fnOut = fileSysVMakeFn(dir,fn,ext,vl); + char* fnOut = filesys::vMakeFn(dir,fn,ext,vl); va_end(vl); return fnOut; } -cw::fileSysPathPart_t* cw::fileSysPathParts( const char* pathStr ) +cw::filesys::pathPart_t* cw::filesys::pathParts( const char* pathStr ) { unsigned n = 0; // char's in pathStr unsigned dn = 0; // char's in the dir part unsigned fn = 0; // char's in the name part unsigned en = 0; // char's in the ext part char* cp = nullptr; - fileSysPathPart_t* rp = nullptr; + pathPart_t* rp = nullptr; if( pathStr==nullptr ) @@ -283,10 +283,10 @@ cw::fileSysPathPart_t* cw::fileSysPathParts( const char* pathStr ) dn = strlen(cp); // get the total size of the returned memory. (add 3 for ecmh possible terminating zero) - n = sizeof(fileSysPathPart_t) + dn + fn + en + 3; + n = sizeof(pathPart_t) + dn + fn + en + 3; // alloc memory - if((rp = memAllocZ( n )) == nullptr ) + if((rp = mem::allocZ( n )) == nullptr ) { cwLogError( kMemAllocFailRC, "Unable to allocate the file system path part record for '%s'.",pathStr); return nullptr; diff --git a/cwFileSys.h b/cwFileSys.h index 39a4c42..85bca35 100644 --- a/cwFileSys.h +++ b/cwFileSys.h @@ -3,41 +3,43 @@ namespace cw { - - // Test the type of a file system object: - // - bool fileSysIsDir( const char* dirStr ); //< Return true if 'dirStr' refers to an existing directory. - bool fileSysIsFile( const char* fnStr ); //< Return true if 'fnStr' refers to an existing file. - bool fileSysIsLink( const char* fnStr ); //< Return true if 'fnStr' refers to a symbolic link. - - // Create File Names: - // - // Create a file name by concatenating sub-strings. - // - // Variable arg's. entries are directories inserted between - // 'dirPrefixStr' and the file name. - // Terminate var arg's directory list with a nullptr. - // - // The returned string must be released by a call to memRelease() or memFree(). - char* fileSysVMakeFn( const char* dir, const char* fn, const char* ext, va_list vl ); - char* fileSysMakeFn( const char* dir, const char* fn, const char* ext, ... ); - - - // Parse a path into its parts: - // - // Return record used by fileSysParts() - typedef struct + namespace filesys { - const char* dirStr; - const char* fnStr; - const char* extStr; - } fileSysPathPart_t; - // Given a file name decompose it into a directory string, file name string and file extension string. - // The returned record and the strings it points to are contained in a single block of - // memory which must be released by a call to memRelease() or memFree() - fileSysPathPart_t* fileSysPathParts( const char* pathNameStr ); + // Test the type of a file system object: + // + bool isDir( const char* dirStr ); //< Return true if 'dirStr' refers to an existing directory. + bool isFile( const char* fnStr ); //< Return true if 'fnStr' refers to an existing file. + bool isLink( const char* fnStr ); //< Return true if 'fnStr' refers to a symbolic link. + + // Create File Names: + // + // Create a file name by concatenating sub-strings. + // + // Variable arg's. entries are directories inserted between + // 'dirPrefixStr' and the file name. + // Terminate var arg's directory list with a nullptr. + // + // The returned string must be released by a call to memRelease() or memFree(). + char* vMakeFn( const char* dir, const char* fn, const char* ext, va_list vl ); + char* makeFn( const char* dir, const char* fn, const char* ext, ... ); + + // Parse a path into its parts: + // + // Return record used by pathParts() + typedef struct + { + const char* dirStr; + const char* fnStr; + const char* extStr; + } pathPart_t; + + // Given a file name decompose it into a directory string, file name string and file extension string. + // The returned record and the strings it points to are contained in a single block of + // memory which must be released by a call to memRelease() or memFree() + pathPart_t* pathParts( const char* pathNameStr ); + } } diff --git a/cwLex.cpp b/cwLex.cpp index c8a0043..1245e00 100644 --- a/cwLex.cpp +++ b/cwLex.cpp @@ -362,9 +362,9 @@ namespace cw if( p->mfi == p->mfn ) { int incr_cnt = 10; - lexMatcher* np = memAllocZ( p->mfn + incr_cnt ); + lexMatcher* np = mem::allocZ( p->mfn + incr_cnt ); memcpy(np,p->mfp,p->mfi*sizeof(lexMatcher)); - memRelease(p->mfp); + mem::release(p->mfp); p->mfp = np; p->mfn += incr_cnt; } @@ -378,7 +378,7 @@ namespace cw if( keyStr != nullptr ) { // allocate space for the token string and store it - p->mfp[p->mfi].tokenStr = memDuplStr(keyStr); + p->mfp[p->mfi].tokenStr = mem::duplStr(keyStr); } @@ -442,20 +442,20 @@ cw::rc_t cw::lex::create( handle_t& hRef, const char* cp, unsigned cn, unsigned if((rc = lex::destroy(hRef)) != kOkRC ) return rc; - p = memAllocZ(); + p = mem::allocZ(); p->flags = flags; _lexSetTextBuffer( p, cp, cn ); int init_mfn = 10; - p->mfp = memAllocZ( init_mfn ); + p->mfp = mem::allocZ( init_mfn ); p->mfn = init_mfn; p->mfi = 0; - p->lineCmtStr = memDuplStr( dfltLineCmt ); - p->blockBegCmtStr = memDuplStr( dfltBlockBegCmt ); - p->blockEndCmtStr = memDuplStr( dfltBlockEndCmt ); + p->lineCmtStr = mem::duplStr( dfltLineCmt ); + p->blockBegCmtStr = mem::duplStr( dfltBlockBegCmt ); + p->blockEndCmtStr = mem::duplStr( dfltBlockEndCmt ); _lexInstallMatcher( p, kSpaceLexTId, _lexSpaceMatcher, nullptr, nullptr ); _lexInstallMatcher( p, kRealLexTId, _lexRealMatcher, nullptr, nullptr ); @@ -493,21 +493,21 @@ cw::rc_t cw::lex::destroy( handle_t& hRef ) // free the user token strings for(; imfi; ++i) if( p->mfp[i].tokenStr != nullptr ) - memRelease(p->mfp[i].tokenStr); + mem::release(p->mfp[i].tokenStr); // free the matcher array - memRelease(p->mfp); + mem::release(p->mfp); p->mfi = 0; p->mfn = 0; } - memRelease(p->lineCmtStr); - memRelease(p->blockBegCmtStr); - memRelease(p->blockEndCmtStr); - memRelease(p->textBuf); + mem::release(p->lineCmtStr); + mem::release(p->blockBegCmtStr); + mem::release(p->blockEndCmtStr); + mem::release(p->textBuf); // free the lexer object - memRelease(p); + mem::release(p); hRef.set(nullptr); } @@ -556,7 +556,7 @@ cw::rc_t cw::lex::setFile( handle_t h, const char* fn ) return rc; // allocate the text buffer - if((p->textBuf = memResizeZ(p->textBuf, n+1)) == nullptr ) + if((p->textBuf = mem::resizeZ(p->textBuf, n+1)) == nullptr ) { rc = cwLogError(kMemAllocFailRC,"Unable to allocate the text file buffer for:'%s'.",fn); goto errLabel; diff --git a/cwLog.cpp b/cwLog.cpp index b559ad7..366d0b8 100644 --- a/cwLog.cpp +++ b/cwLog.cpp @@ -43,7 +43,7 @@ cw::rc_t cw::log::create( handle_t& hRef, unsigned level, logOutputCbFunc_t out if((rc = destroy(hRef)) != kOkRC) return rc; - log_t* p = memAllocZ(); + log_t* p = mem::allocZ(); p->outCbFunc = outCbFunc == nullptr ? defaultOutput : outCbFunc; p->outCbArg = outCbArg; p->fmtCbFunc = fmtCbFunc == nullptr ? defaultFormatter : fmtCbFunc; @@ -61,7 +61,7 @@ cw::rc_t cw::log::destroy( handle_t& hRef ) if( hRef.p == nullptr ) return rc; - memRelease( hRef.p ); + mem::release( hRef.p ); return rc; } diff --git a/cwMem.cpp b/cwMem.cpp index 2900b4b..a7f01b8 100644 --- a/cwMem.cpp +++ b/cwMem.cpp @@ -4,10 +4,7 @@ #include "cwMem.h" -namespace cw -{ - - void* _memAlloc( void* p0, unsigned n, bool zeroFl ) +void* cw::mem::_alloc( void* p0, unsigned n, bool zeroFl ) { void* p = nullptr; // ptr to new block unsigned p0N = 0; // size of existing block @@ -34,7 +31,7 @@ namespace cw if( p0 != nullptr ) { memcpy(p,p0_1,p0N); - memFree(p0); // free the existing block + mem::free(p0); // free the existing block } // if requested zero the block @@ -50,22 +47,22 @@ namespace cw return p1+1; } -} -unsigned cw::memByteCount( const void* p ) + +unsigned cw::mem::byteCount( const void* p ) { return p==nullptr ? 0 : static_cast(p)[-1]; } -char* cw::memAllocStr( const char* s ) +char* cw::mem::allocStr( const char* s ) { char* s1 = nullptr; if( s != nullptr ) { unsigned sn = strlen(s); - s1 = static_cast(_memAlloc(nullptr,sn+1,false)); + s1 = static_cast(_alloc(nullptr,sn+1,false)); memcpy(s1,s,sn); s1[sn] = 0; } @@ -73,27 +70,27 @@ char* cw::memAllocStr( const char* s ) return s1; } -void* cw::memAllocDupl( const void* p0, unsigned byteN ) +void* cw::mem::allocDupl( const void* p0, unsigned byteN ) { if( p0 == nullptr || byteN == 0 ) return nullptr; - void* p1 = _memAlloc(nullptr,byteN,false); + void* p1 = _alloc(nullptr,byteN,false); memcpy(p1,p0,byteN); return p1; } -void* cw::memAllocDupl( const void* p ) +void* cw::mem::allocDupl( const void* p ) { - return memAllocDupl(p,memByteCount(p)); + return allocDupl(p,byteCount(p)); } -void cw::memFree( void* p ) +void cw::mem::free( void* p ) { if( p != nullptr) { - free(static_cast(p)-1); + ::free(static_cast(p)-1); } } diff --git a/cwMem.h b/cwMem.h index 4dbcc2e..65e7ede 100644 --- a/cwMem.h +++ b/cwMem.h @@ -4,144 +4,146 @@ namespace cw { - void* _memAlloc( void* p, unsigned n, bool zeroFl ); + namespace mem + { + void* _alloc( void* p, unsigned n, bool zeroFl ); - char* memAllocStr( const char* ); - void* memAllocDupl( const void* p, unsigned byteN ); - void* memAllocDupl( const void* p ); - void memFree( void* ); + char* allocStr( const char* ); + void* allocDupl( const void* p, unsigned byteN ); + void* allocDupl( const void* p ); + void free( void* ); - unsigned memByteCount( const void* p ); + unsigned byteCount( const void* p ); - template - void memRelease(T& p) { memFree(p); p=nullptr; } + template + void release(T& p) { ::cw::mem::free(p); p=nullptr; } - template - T* memAllocZ(unsigned n=1) { return static_cast(_memAlloc(nullptr,n*sizeof(T),true)); } + template + T* allocZ(unsigned n=1) { return static_cast(_alloc(nullptr,n*sizeof(T),true)); } template - T* memAlloc(unsigned n=1) { return static_cast(_memAlloc(nullptr,n*sizeof(T),false)); } + T* alloc(unsigned n=1) { return static_cast(_alloc(nullptr,n*sizeof(T),false)); } - template - T* memResizeZ(T* p, unsigned n=1) { return static_cast(_memAlloc(p,n*sizeof(T),true)); } + template + T* resizeZ(T* p, unsigned n=1) { return static_cast(_alloc(p,n*sizeof(T),true)); } - template - size_t _memTextLength(const T* s ) - { - if( s == nullptr ) - return 0; - - // get length of source string - size_t n=0; - - for(; s[n]; ++n) - {} - - return n; - } - - template - T* memDuplStr( const T* s, size_t n ) - { - if( s == nullptr ) - return nullptr; - - n+=1; // add one for terminating zero - - // allocate space for new string - T* s1 = memAlloc(n); - - // copy in new string - for(size_t i=0; i - T* memDuplStr( const T* s ) - { - if( s == nullptr ) - return nullptr; - - - return memDuplStr(s,_memTextLength(s)); - } - - template - T* memReallocStr( T* s0, const T* s1 ) - { - if( s1 == nullptr ) + template + size_t _textLength(const T* s ) { - memFree(s0); - return nullptr; + if( s == nullptr ) + return 0; + + // get length of source string + size_t n=0; + + for(; s[n]; ++n) + {} + + return n; + } + + template + T* duplStr( const T* s, size_t n ) + { + if( s == nullptr ) + return nullptr; + + n+=1; // add one for terminating zero + + // allocate space for new string + T* s1 = alloc(n); + + // copy in new string + for(size_t i=0; i s0n ) + template + T* duplStr( const T* s ) { - memFree(s0); - return memDuplStr(s1); + if( s == nullptr ) + return nullptr; + + + return duplStr(s,_textLength(s)); } - // s1n is <= s0n - // copy s1[] into s0[] - size_t i=0; - for(; s1[i]; ++i) - s0[i] = s1[i]; - s0[i] = 0; - - return s0; - } - - template - C* memPrintf(C* p0, const char* fmt, va_list vl0 ) - { - va_list vl1; - va_copy(vl1,vl0); - - size_t bufN = vsnprintf(nullptr,0,fmt,vl0); - - if( bufN == 0) + template + T* reallocStr( T* s0, const T* s1 ) { - memFree(p0); - return nullptr; + if( s1 == nullptr ) + { + free(s0); + return nullptr; + } + + if( s0 == nullptr ) + return duplStr(s1); + + + size_t s0n = _textLength(s0); + size_t s1n = _textLength(s1); + + // if s1[] can't fit in space of s0[] + if( s1n > s0n ) + { + free(s0); + return duplStr(s1); + } + + // s1n is <= s0n + // copy s1[] into s0[] + size_t i=0; + for(; s1[i]; ++i) + s0[i] = s1[i]; + s0[i] = 0; + + return s0; } + template + C* printf(C* p0, const char* fmt, va_list vl0 ) + { + va_list vl1; + va_copy(vl1,vl0); - C buf[ bufN + 1 ]; - size_t n = vsnprintf(buf,bufN+1,fmt,vl1); + size_t bufN = vsnprintf(nullptr,0,fmt,vl0); - cwAssert(n <= bufN); + if( bufN == 0) + { + free(p0); + return nullptr; + } - buf[bufN] = 0; - va_end(vl1); + C buf[ bufN + 1 ]; + size_t n = vsnprintf(buf,bufN+1,fmt,vl1); - return memReallocStr(p0,buf); - } + cwAssert(n <= bufN); + + buf[bufN] = 0; + + va_end(vl1); + + return reallocStr(p0,buf); + } - template - C* memPrintf(C* p0, const char* fmt, ... ) - { - va_list vl; - va_start(vl,fmt); - C* p1 = memPrintf(p0,fmt,vl); - va_end(vl); - return p1; - } + template + C* printf(C* p0, const char* fmt, ... ) + { + va_list vl; + va_start(vl,fmt); + C* p1 = printf(p0,fmt,vl); + va_end(vl); + return p1; + } - + } } diff --git a/cwMidiAlsa.cpp b/cwMidiAlsa.cpp index ac9e24d..42cfcdf 100644 --- a/cwMidiAlsa.cpp +++ b/cwMidiAlsa.cpp @@ -358,7 +358,7 @@ namespace cw p->devCnt += 1; // allocate the device array - p->devArray = memAllocZ(p->devCnt); + p->devArray = mem::allocZ(p->devCnt); // fill in each device record snd_seq_client_info_set_client(cip, -1); @@ -370,7 +370,7 @@ namespace cw const char* name = snd_seq_client_info_get_name(cip); // initalize the device record - p->devArray[i].nameStr = memDuplStr(cwStringNullGuard(name)); + p->devArray[i].nameStr = mem::duplStr(cwStringNullGuard(name)); p->devArray[i].iPortCnt = 0; p->devArray[i].oPortCnt = 0; p->devArray[i].iPortArray = NULL; @@ -396,10 +396,10 @@ namespace cw // allocate the device port arrays if( p->devArray[i].iPortCnt > 0 ) - p->devArray[i].iPortArray = memAllocZ(p->devArray[i].iPortCnt); + p->devArray[i].iPortArray = mem::allocZ(p->devArray[i].iPortCnt); if( p->devArray[i].oPortCnt > 0 ) - p->devArray[i].oPortArray = memAllocZ(p->devArray[i].oPortCnt); + p->devArray[i].oPortArray = mem::allocZ(p->devArray[i].oPortCnt); snd_seq_port_info_set_client(pip,client); // set the ports client id @@ -417,7 +417,7 @@ namespace cw { assert(jdevArray[i].iPortCnt); p->devArray[i].iPortArray[j].inputFl = true; - p->devArray[i].iPortArray[j].nameStr = memDuplStr(cwStringNullGuard(port)); + p->devArray[i].iPortArray[j].nameStr = mem::duplStr(cwStringNullGuard(port)); p->devArray[i].iPortArray[j].alsa_type = type; p->devArray[i].iPortArray[j].alsa_cap = caps; p->devArray[i].iPortArray[j].alsa_addr = addr; @@ -439,7 +439,7 @@ namespace cw { assert(kdevArray[i].oPortCnt); p->devArray[i].oPortArray[k].inputFl = false; - p->devArray[i].oPortArray[k].nameStr = memDuplStr(cwStringNullGuard(port)); + p->devArray[i].oPortArray[k].nameStr = mem::duplStr(cwStringNullGuard(port)); p->devArray[i].oPortArray[k].alsa_type = type; p->devArray[i].oPortArray[k].alsa_cap = caps; p->devArray[i].oPortArray[k].alsa_addr = addr; @@ -513,7 +513,7 @@ cw::rc_t cw::midi::device::initialize( cbFunc_t cbFunc, void* cbArg, unsigned p return rc; // allocate the global root object - _cmMpRoot = p = memAllocZ(1); + _cmMpRoot = p = mem::allocZ(1); p->h = NULL; p->alsa_queue = -1; @@ -541,7 +541,7 @@ cw::rc_t cw::midi::device::initialize( cbFunc_t cbFunc, void* cbArg, unsigned p // allocate the file descriptors used for polling p->alsa_fdCnt = snd_seq_poll_descriptors_count(p->h, POLLIN); - p->alsa_fd = memAllocZ(p->alsa_fdCnt); + p->alsa_fd = mem::allocZ(p->alsa_fdCnt); snd_seq_poll_descriptors(p->h, p->alsa_fd, p->alsa_fdCnt, POLLIN); p->cbFunc = cbFunc; @@ -620,25 +620,25 @@ cw::rc_t cw::midi::device::finalize() for(j=0; jdevArray[i].iPortCnt; ++j) { parser::destroy(p->devArray[i].iPortArray[j].parserH); - memRelease( p->devArray[i].iPortArray[j].nameStr ); + mem::release( p->devArray[i].iPortArray[j].nameStr ); } for(j=0; jdevArray[i].oPortCnt; ++j) { - memRelease( p->devArray[i].oPortArray[j].nameStr ); + mem::release( p->devArray[i].oPortArray[j].nameStr ); } - memRelease(p->devArray[i].iPortArray); - memRelease(p->devArray[i].oPortArray); - memRelease(p->devArray[i].nameStr); + mem::release(p->devArray[i].iPortArray); + mem::release(p->devArray[i].oPortArray); + mem::release(p->devArray[i].nameStr); } - memRelease(p->devArray); + mem::release(p->devArray); - memFree(p->alsa_fd); + mem::free(p->alsa_fd); - memRelease(_cmMpRoot); + mem::release(_cmMpRoot); } diff --git a/cwMidiPort.cpp b/cwMidiPort.cpp index 0e60cb1..de53c08 100644 --- a/cwMidiPort.cpp +++ b/cwMidiPort.cpp @@ -144,17 +144,17 @@ namespace cw void _destroy( parser_t* p ) { - memRelease(p->buf); + mem::release(p->buf); cbRecd_t* c = p->cbChain; while(c != NULL) { cbRecd_t* nc = c->linkPtr; - memRelease(c); + mem::release(c); c = nc; } - memRelease(p); + mem::release(p); } @@ -165,7 +165,7 @@ namespace cw cw::rc_t cw::midi::parser::create( handle_t& hRef, unsigned devIdx, unsigned portIdx, cbFunc_t cbFunc, void* cbDataPtr, unsigned bufByteCnt ) { rc_t rc = kOkRC; - parser_t* p = memAllocZ( 1 ); + parser_t* p = mem::allocZ( 1 ); p->pkt.devIdx = devIdx; @@ -176,7 +176,7 @@ cw::rc_t cw::midi::parser::create( handle_t& hRef, unsigned devIdx, unsigned por //p->cbChain->cbDataPtr = cbDataPtr; //p->cbChain->linkPtr = NULL; p->cbChain = NULL; - p->buf = memAllocZ( bufByteCnt ); + p->buf = mem::allocZ( bufByteCnt ); p->bufByteCnt = bufByteCnt; p->bufIdx = 0; p->msgCnt = 0; @@ -409,7 +409,7 @@ cw::rc_t cw::midi::parser::transmit( handle_t h ) cw::rc_t cw::midi::parser::installCallback( handle_t h, cbFunc_t cbFunc, void* cbDataPtr ) { parser_t* p = _handleToPtr(h); - cbRecd_t* newCbPtr = memAllocZ( 1 ); + cbRecd_t* newCbPtr = mem::allocZ( 1 ); cbRecd_t* c = p->cbChain; newCbPtr->cbFunc = cbFunc; @@ -456,7 +456,7 @@ cw::rc_t cw::midi::parser::removeCallback( handle_t h, cbFunc_t cbFunc, vo else c0->linkPtr = c1->linkPtr; - memRelease(c1); + mem::release(c1); return kOkRC; } diff --git a/cwMpScNbQueue.h b/cwMpScNbQueue.h index 01f9109..dae916b 100644 --- a/cwMpScNbQueue.h +++ b/cwMpScNbQueue.h @@ -18,13 +18,13 @@ namespace cw MpScNbQueue() { - node_t* stub = memAllocZ(); + node_t* stub = mem::allocZ(); _head = stub; _tail = stub; } virtual ~MpScNbQueue() - { memFree(_tail); } + { mem::free(_tail); } MpScNbQueue( const MpScNbQueue& ) = delete; MpScNbQueue( const MpScNbQueue&& ) = delete; @@ -34,7 +34,7 @@ namespace cw void push( T* payload ) { - node_t* new_node = memAllocZ(1); + node_t* new_node = mem::allocZ(1); new_node->payload = payload; new_node->next.store(nullptr); node_t* prev = _head.exchange(new_node,std::memory_order_acq_rel); // append the new node to the list (aquire-release) @@ -51,7 +51,7 @@ namespace cw { _tail = next; payload = next->payload; - memFree(t); + mem::free(t); } return payload; diff --git a/cwObject.cpp b/cwObject.cpp index 0aaab00..790c043 100644 --- a/cwObject.cpp +++ b/cwObject.cpp @@ -45,11 +45,11 @@ namespace cw void _objTypeFree( object_t* o ) - { memRelease(o); } + { mem::release(o); } void _objTypeFreeString( object_t* o ) { - memRelease( o->u.str ); + mem::release( o->u.str ); _objTypeFree(o); } @@ -237,7 +237,7 @@ namespace cw } } - object_t* o = memAllocZ(); + object_t* o = mem::allocZ(); o->type = type; o->parent = parent; return o; @@ -482,7 +482,7 @@ cw::rc_t cw::objectFromString( const char* s, object_t*& objRef ) cnp = _objAppendLeftMostNode( cnp, _objAllocate( kPairTId, cnp )); - char* v = memDuplStr(lex::tokenText(lexH),lex::tokenCharCount(lexH)); + char* v = mem::duplStr(lex::tokenText(lexH),lex::tokenCharCount(lexH)); unsigned identFl = lexId == lex::kIdentLexTId ? kIdentFl : 0; @@ -523,7 +523,7 @@ cw::rc_t cw::objectFromFile( const char* fn, object_t*& objRef ) if(( buf = file::fnToStr(fn, &bufByteCnt)) != NULL ) { rc = objectFromString( buf, objRef ); - memRelease(buf); + mem::release(buf); } return rc; diff --git a/cwSerialPort.cpp b/cwSerialPort.cpp index 7e84242..b18b556 100644 --- a/cwSerialPort.cpp +++ b/cwSerialPort.cpp @@ -32,7 +32,7 @@ namespace cw void _setClosedState( port_t* p ) { if( p->_deviceStr != nullptr ) - cw::memFree(const_cast(p->_deviceStr)); + cw::mem::free(const_cast(p->_deviceStr)); p->_deviceH = -1; p->_deviceStr = nullptr; @@ -103,7 +103,7 @@ namespace cw _setClosedState(p); } - memRelease(p); + mem::release(p); errLabel: return rc; @@ -123,7 +123,7 @@ cw::rc_t cw::serialPort::create( handle_t& h, const char* deviceStr, unsigned ba if((rc = destroy(h)) != kOkRC ) return rc; - port_t* p = memAllocZ(); + port_t* p = mem::allocZ(); p->_deviceH = -1; @@ -239,7 +239,7 @@ cw::rc_t cw::serialPort::create( handle_t& h, const char* deviceStr, unsigned ba p->_pollfd.fd = p->_deviceH; p->_pollfd.events = POLLIN; - p->_deviceStr = cw::memAllocStr( deviceStr ); + p->_deviceStr = cw::mem::allocStr( deviceStr ); p->_baudRate = baudRate; p->_cfgFlags = cfgFlags; p->_cbFunc = cbFunc; diff --git a/cwSerialPortSrv.cpp b/cwSerialPortSrv.cpp index c09b6eb..3741226 100644 --- a/cwSerialPortSrv.cpp +++ b/cwSerialPortSrv.cpp @@ -30,7 +30,7 @@ namespace cw if((rc = thread::destroy(p->threadH)) != kOkRC ) return rc; - memRelease(p); + mem::release(p); return rc; } @@ -55,7 +55,7 @@ cw::rc_t cw::serialPortSrv::create( handle_t& h, const char* deviceStr, unsigned { rc_t rc = kOkRC; - this_t* p = memAllocZ(); + this_t* p = mem::allocZ(); if((rc = serialPort::create( p->portH, deviceStr, baudRate, cfgFlags, cbFunc, cbArg )) != kOkRC ) goto errLabel; diff --git a/cwTcpSocket.cpp b/cwTcpSocket.cpp index 1de1a33..e5be0f7 100644 --- a/cwTcpSocket.cpp +++ b/cwTcpSocket.cpp @@ -56,7 +56,7 @@ namespace cw p->sockH = cwSOCKET_NULL_SOCK; } - memRelease(p); + mem::release(p); return kOkRC; } @@ -122,7 +122,7 @@ cw::rc_t cw::net::socket::create( if((rc = destroy(hRef)) != kOkRC ) return rc; - socket_t* p = memAllocZ(); + socket_t* p = mem::allocZ(); p->sockH = cwSOCKET_NULL_SOCK; // get a handle to the socket diff --git a/cwTcpSocketSrv.cpp b/cwTcpSocketSrv.cpp index 58bec48..302f337 100644 --- a/cwTcpSocketSrv.cpp +++ b/cwTcpSocketSrv.cpp @@ -35,8 +35,8 @@ namespace cw if((rc = socket::destroy(p->sockH)) != kOkRC ) return rc; - memRelease(p->recvBuf); - memRelease(p); + mem::release(p->recvBuf); + mem::release(p); return rc; } @@ -72,7 +72,7 @@ cw::rc_t cw::net::srv::create( if((rc = destroy(hRef)) != kOkRC ) return rc; - socksrv_t* p = memAllocZ(); + socksrv_t* p = mem::allocZ(); if((rc = socket::create( p->sockH, port, socket::kNonBlockingFl, 0, remoteAddr, remotePort )) != kOkRC ) goto errLabel; @@ -80,7 +80,7 @@ cw::rc_t cw::net::srv::create( if((rc = thread::create( p->threadH, _threadFunc, p )) != kOkRC ) goto errLabel; - p->recvBuf = memAllocZ( recvBufByteCnt ); + p->recvBuf = mem::allocZ( recvBufByteCnt ); p->recvBufByteCnt = recvBufByteCnt; p->cbFunc = cbFunc; p->cbArg = cbArg; diff --git a/cwTextBuf.cpp b/cwTextBuf.cpp index 22e8585..df1c60c 100644 --- a/cwTextBuf.cpp +++ b/cwTextBuf.cpp @@ -32,12 +32,12 @@ cw::rc_t cw::textBuf::create( handle_t& hRef, unsigned initCharN, unsigned expan if((rc = destroy(hRef)) != kOkRC ) return rc; - this_t* p = memAllocZ(); - p->buf = memAllocZ(initCharN); + this_t* p = mem::allocZ(); + p->buf = mem::allocZ(initCharN); p->expandCharN = expandCharN; p->allocCharN = initCharN; - p->boolTrueText = memDuplStr("true"); - p->boolFalseText = memDuplStr("false"); + p->boolTrueText = mem::duplStr("true"); + p->boolFalseText = mem::duplStr("false"); hRef.set(p); return rc; } @@ -62,10 +62,10 @@ cw::rc_t cw::textBuf::destroy(handle_t& hRef ) this_t* p = _handleToPtr(hRef); - memRelease(p->buf); - memRelease(p->boolTrueText); - memRelease(p->boolFalseText); - memRelease(p); + mem::release(p->buf); + mem::release(p->boolTrueText); + mem::release(p->boolFalseText); + mem::release(p); hRef.clear(); return rc; } @@ -100,7 +100,7 @@ cw::rc_t cw::textBuf::print( handle_t h, const char* fmt, va_list vl ) unsigned minExpandCharN = (p->endN + n) - p->allocCharN; unsigned expandCharN = std::max(minExpandCharN,p->expandCharN); p->allocCharN += expandCharN; - p->buf = memResizeZ( p->buf, p->allocCharN ); + p->buf = mem::resizeZ( p->buf, p->allocCharN ); } int m = vsnprintf(p->buf + p->endN, n, fmt, vl ); @@ -142,9 +142,9 @@ cw::rc_t cw::textBuf::setBoolFormat( handle_t h, bool v, const char* s) this_t* p = _handleToPtr(h); if( v ) - p->boolTrueText = memReallocStr(p->boolTrueText,s); + p->boolTrueText = mem::reallocStr(p->boolTrueText,s); else - p->boolFalseText = memReallocStr(p->boolFalseText,s); + p->boolFalseText = mem::reallocStr(p->boolFalseText,s); return kOkRC; } diff --git a/cwThread.cpp b/cwThread.cpp index 034f72f..7a923e0 100644 --- a/cwThread.cpp +++ b/cwThread.cpp @@ -109,7 +109,7 @@ cw::rc_t cw::thread::create( handle_t& hRef, cbFunc_t func, void* funcArg, int s if((rc = destroy(hRef)) != kOkRC ) return rc; - thread_t* p = memAllocZ(); + thread_t* p = mem::allocZ(); p->func = func; p->funcArg = funcArg; @@ -155,7 +155,7 @@ cw::rc_t cw::thread::destroy( handle_t& hRef ) if((rc = _waitForState(p,kExitedThId)) != kOkRC ) return cwLogError(rc,"Thread timed out waiting for destroy."); - memRelease(p); + mem::release(p); hRef.clear(); return rc; diff --git a/cwWebSock.cpp b/cwWebSock.cpp index 2214f0c..1d4da1e 100644 --- a/cwWebSock.cpp +++ b/cwWebSock.cpp @@ -203,8 +203,8 @@ namespace cw msg_t* t = m1->link; - memFree(m1->msg); - memFree(m1); + mem::free(m1->msg); + mem::free(m1); m1 = t; @@ -232,8 +232,8 @@ namespace cw while((m = p->_q->pop()) != nullptr) { - memFree(m->msg); - memFree(m); + mem::free(m->msg); + mem::free(m); } delete p->_q; @@ -241,7 +241,7 @@ namespace cw for(int i=0; p->_protocolA!=nullptr and p->_protocolA[i].callback != nullptr; ++i) { - memFree(const_cast(p->_protocolA[i].name)); + mem::free(const_cast(p->_protocolA[i].name)); // TODO: delete any msgs in the protocol state here auto ps = static_cast(p->_protocolA[i].user); @@ -251,28 +251,28 @@ namespace cw { msg_t* tmp = m->link; - memFree(m->msg); - memFree(m); + mem::free(m->msg); + mem::free(m); m = tmp; } - memFree(ps); + mem::free(ps); } - memRelease(p->_protocolA); + mem::release(p->_protocolA); p->_protocolN = 0; if( p->_mount != nullptr ) { - memFree(const_cast(p->_mount->origin)); - memFree(const_cast(p->_mount->def)); - memRelease(p->_mount); + mem::free(const_cast(p->_mount->origin)); + mem::free(const_cast(p->_mount->def)); + mem::release(p->_mount); } p->_nextSessionId = 0; p->_connSessionN = 0; - memRelease(p); + mem::release(p); return kOkRC; @@ -296,21 +296,21 @@ cw::rc_t cw::websock::create( if((rc = destroy(h)) != kOkRC ) return rc; - websock_t* p = memAllocZ(); + websock_t* p = mem::allocZ(); int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE; lws_set_log_level(logs, NULL); // Allocate one extra record to act as the end-of-list sentinel. p->_protocolN = protocolN + 1; - p->_protocolA = memAllocZ(p->_protocolN); + p->_protocolA = mem::allocZ(p->_protocolN); // Setup the websocket internal protocol state array for(unsigned i=0; i(1); - auto dummy = memAllocZ(1); + auto protocolState = mem::allocZ(1); + auto dummy = mem::allocZ(1); protocolState->thisPtr = p; protocolState->begMsg = dummy; @@ -318,7 +318,7 @@ cw::rc_t cw::websock::create( // Setup the interal lws_protocols record struct lws_protocols* pr = p->_protocolA + i; - pr->name = memAllocStr(protocolArgA[i].label); + pr->name = mem::allocStr(protocolArgA[i].label); pr->id = protocolArgA[i].id; pr->rx_buffer_size = protocolArgA[i].rcvBufByteN; pr->tx_packet_size = 0; //protocolArgA[i].xmtBufByteN; @@ -328,11 +328,11 @@ cw::rc_t cw::websock::create( } static const char* slash = {"/"}; - p->_mount = memAllocZ(1); + p->_mount = mem::allocZ(1); p->_mount->mountpoint = slash; p->_mount->mountpoint_len = strlen(slash); - p->_mount->origin = memAllocStr(physRootDir); // physical directory assoc'd with http "/" - p->_mount->def = memAllocStr(dfltHtmlPageFn); + p->_mount->origin = mem::allocStr(physRootDir); // physical directory assoc'd with http "/" + p->_mount->def = mem::allocStr(dfltHtmlPageFn); p->_mount->origin_protocol= LWSMPRO_FILE; memset(&info,0,sizeof(info)); @@ -381,8 +381,8 @@ cw::rc_t cw::websock::send(handle_t h, unsigned protocolId, const void* msg, un { rc_t rc = kOkRC; - msg_t* m = memAllocZ(1); - m->msg = memAllocZ(byteN); + msg_t* m = mem::allocZ(1); + m->msg = mem::allocZ(byteN); memcpy(m->msg,msg,byteN); m->msgByteN = byteN; m->protocolId = protocolId; @@ -442,10 +442,10 @@ cw::rc_t cw::websock::exec( handle_t h, unsigned timeOutMs ) // add the pre-padding bytes to the msg - unsigned char* msg = memAllocZ(LWS_PRE + m->msgByteN); + unsigned char* msg = mem::allocZ(LWS_PRE + m->msgByteN); memcpy( msg+LWS_PRE, m->msg, m->msgByteN ); - memFree(m->msg); // free the original msg buffer + mem::free(m->msg); // free the original msg buffer m->msg = msg; m->msgId = ps->nextNewMsgId; // set the msg id diff --git a/cwWebSockSvr.cpp b/cwWebSockSvr.cpp index 0f09631..4ea0e53 100644 --- a/cwWebSockSvr.cpp +++ b/cwWebSockSvr.cpp @@ -30,7 +30,7 @@ namespace cw if((rc = websock::destroy(p->_websockH)) != kOkRC ) return rc; - memRelease(p); + mem::release(p); return rc; } @@ -60,7 +60,7 @@ cw::rc_t cw::websockSrv::create( if((rc = destroy(h)) != kOkRC ) return rc; - websockSrv_t* p = memAllocZ(); + websockSrv_t* p = mem::allocZ(); if((rc = websock::create( p->_websockH, cbFunc, cbArg, physRootDir, dfltHtmlPageFn, port, protocolA, protocolN )) != kOkRC ) goto errLabel; diff --git a/main.cpp b/main.cpp index 0521106..2e89a28 100644 --- a/main.cpp +++ b/main.cpp @@ -72,18 +72,18 @@ void variadicTplTest( cw::object_t* cfg, int argc, const char* argv[] ) void fileSysTest( cw::object_t* cfg, int argc, const char* argv[] ) { - cw::fileSysPathPart_t* pp = cw::fileSysPathParts(__FILE__); + cw::filesys::pathPart_t* pp = cw::filesys::pathParts(__FILE__); cwLogInfo("dir:%s",pp->dirStr); cwLogInfo("fn: %s",pp->fnStr); cwLogInfo("ext:%s",pp->extStr); - char* fn = cw::fileSysMakeFn( pp->dirStr, pp->fnStr, pp->extStr, nullptr ); + char* fn = cw::filesys::makeFn( pp->dirStr, pp->fnStr, pp->extStr, nullptr ); cwLogInfo("fn: %s",fn); - cw::memRelease(pp); - cw::memRelease(fn); + cw::mem::release(pp); + cw::mem::release(fn); }