//开始录音 BOOL CAudioRec::Start() { BOOL bRet=FALSE; if(!m_hIn) goto RET; if(!AllocBuffer()) goto RET; m_mmr=waveInStart(m_hIn); if(m_mmr) goto RET; bRet=TRUE; RET: return bRet; }
// must be called before replacing contents of this string bool wxStringImpl::AllocBeforeWrite(size_t nLen) { wxASSERT( nLen != 0 ); // doesn't make any sense // must not share string and must have enough space wxStringData* pData = GetStringData(); if ( pData->IsShared() || pData->IsEmpty() ) { // can't work with old buffer, get new one pData->Unlock(); if ( !AllocBuffer(nLen) ) { // allocation failures are handled by the caller return false; } } else { if ( nLen > pData->nAllocLength ) { // realloc the buffer instead of calling malloc() again, this is more // efficient STATISTICS_ADD(Length, nLen); nLen += EXTRA_ALLOC; pData = (wxStringData*) realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(wxStringCharType)); if ( pData == NULL ) { // allocation failures are handled by the caller // keep previous data since reallocation failed return false; } pData->nAllocLength = nLen; m_pchData = pData->data(); } } wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner // it doesn't really matter what the string length is as it's going to be // overwritten later but, for extra safety, set it to 0 for now as we may // have some junk in m_pchData GetStringData()->nDataLength = 0; return true; }
int STR_String::Serialize(pCStream stream) { if (stream->GetAccess() == CStream::Access_Read) { int ln; stream->Read(&ln, sizeof(ln)); AllocBuffer(ln, false); stream->Read(this->m_data, ln); this->m_data[ln] = '\0'; this->m_len = ln; } else { stream->Write(&this->m_len, sizeof(this->m_len)); stream->Write(this->m_data, this->m_len); } return this->m_len + sizeof(this->m_len); }
// must be called before changing this string bool wxStringImpl::CopyBeforeWrite() { wxStringData* pData = GetStringData(); if ( pData->IsShared() ) { pData->Unlock(); // memory not freed because shared size_t nLen = pData->nDataLength; if ( !AllocBuffer(nLen) ) { // allocation failures are handled by the caller return false; } wxStringMemcpy(m_pchData, pData->data(), nLen); } wxASSERT( !GetStringData()->IsShared() ); // we must be the only owner return true; }
// // Replace a character in this string with another string // void STR_String::Replace(int pos, rcSTR_String str) { //bounds(pos, 0, Length()-1); if (str.Length() < 1) { // Remove one character from the string memcpy(this->m_data + pos, this->m_data + pos + 1, this->m_len - pos); } else { // Insert zero or more characters into the string AllocBuffer(this->m_len + str.Length() - 1, true); if (str.Length() != 1) memcpy(this->m_data + pos + str.Length(), this->m_data + pos + 1, Length() - pos); memcpy(this->m_data + pos, str.ReadPtr(), str.Length()); } this->m_len += str.Length() - 1; }
/* * @implemented */ void CHString::FreeExtra() throw (CHeap_Exception) { CHStringData* OldData; // No extra? Do nothing if (GetData()->nDataLength == GetData()->nAllocLength) { return; } // Get old buffer OldData = GetData(); // Allocate a new one, at the right size (with no place for \0 :-)) AllocBuffer(GetData()->nDataLength); // Copy old and release it wcsncpy(m_pchData, OldData->data(), OldData->nDataLength); Release(OldData); }
TMdbPacketInfo::TMdbPacketInfo(char* pcBuffer, MDB_UINT32 uiLength /* = -1 */) { m_pcData = NULL; this->uiLength = 0; uiOffset = 0; if(uiLength == (MDB_UINT32)-1) { uiLength = (MDB_UINT32)strlen(pcBuffer); } if(uiLength > 0) { char* pcDataBuffer = AllocBuffer(uiLength); if(pcBuffer) { memcpy(pcDataBuffer, pcBuffer, uiLength); } } }
void FString::ReallocBuffer (size_t newlen) { if (Data()->RefCount > 1) { // If more than one reference, we must use a new copy FStringData *old = Data(); AllocBuffer (newlen); StrCopy (Chars, old->Chars(), newlen < old->Len ? newlen : old->Len); old->Release(); } else { if (newlen > Data()->AllocLen) { Chars = (char *)(Data()->Realloc(newlen) + 1); } Data()->Len = (unsigned int)newlen; } }
CStrClass::CStrClass(TCHAR ch, int nLength) { if (nLength < 1) { // return empty string if invalid repeat count Init(); } else { AllocBuffer(nLength); #ifdef _UNICODE for (int i = 0; i < nLength; i++) m_pchData[i] = ch; #else memset(m_pchData, ch, nLength); #endif } }
BOOL COXQuickString::SetString(LPCTSTR szText) { // We do a fresh memory allocation when setting the string. Empty(); if (!szText) return TRUE; m_nLength = PtrToUint(_tcslen(szText)); m_nBufferSize = m_nLength + 1; m_szText = AllocBuffer(m_nBufferSize); if (!m_szText) return FALSE; UTBStr::tcscpy(m_szText, m_nBufferSize, szText); return TRUE; }
/* * @implemented */ CHString::CHString(LPCWSTR lpsz) throw (CHeap_Exception) { // Allow null initialize, in case something goes wrong m_pchData = afxPchNil; // If we have an input string if (lpsz != 0) { // Get its length int Len = SafeStrlen(lpsz); // Then, allocate a big enough buffer and copy string // Note that here, we don't null terminate the string... if (Len) { AllocBuffer(Len); wcsncpy(m_pchData, lpsz, Len); } } }
char* ReadFile(const char* path) { FILE* file = fopen(path, "rb"); IOTJS_ASSERT(file != NULL); fseek(file, 0, SEEK_END); size_t len = ftell(file); fseek(file, 0, SEEK_SET); char* buff = AllocBuffer(len + 1); size_t read = fread(buff, 1, len, file); IOTJS_ASSERT(read == len); *(buff+len) = 0; fclose(file); return buff; }
CString::CString(LPCTSTR lpsz) { Init(); if (lpsz != NULL && HIWORD(lpsz) == NULL) { UINT nID = LOWORD((DWORD)lpsz); if (!LoadString(nID)) TRACE1("Warning: implicit LoadString(%u) failed\n", nID); } else { int nLen = SafeStrlen(lpsz); if (nLen != 0) { AllocBuffer(nLen); memcpy(m_pchData, lpsz, nLen*sizeof(TCHAR)); } } }
void CString::AssignCopy(LPCSTR lpszSrcData, int nSrcLen) { // nSrcLen of -1 means we should measure if (nSrcLen < 0) nSrcLen = lpszSrcData ? lstrlen(lpszSrcData) : 0; // check if it will fit in the existing buffer if (nSrcLen > m_nAllocLength) { // won't fit so allocate another buffer Empty(); AllocBuffer(nSrcLen); } if (nSrcLen != 0) memcpy(m_pchData, lpszSrcData, nSrcLen); m_nDataLength = nSrcLen; m_pchData[m_nDataLength] = '\0'; }
LPTSTR CStrClass::GetBuffer(int nMinBufLength) { if (nMinBufLength > m_nAllocLength) { // we have to grow the buffer LPTSTR lpszOldData = m_pchData; int nOldLen = m_nDataLength; // AllocBuffer will tromp it AllocBuffer(nMinBufLength); memcpy(m_pchData, lpszOldData, nOldLen*sizeof(TCHAR)); m_nDataLength = nOldLen; m_pchData[m_nDataLength] = '\0'; SafeDelete(lpszOldData); } // return a pointer to the character storage for this string return m_pchData; }
BOOL COXQuickString::SetString(LPCTSTR szText, int nCount) { // We do a fresh memory allocation when setting the string. Empty(); if (!szText || nCount <= 0) return TRUE; m_nLength = min(PtrToUint(_tcslen(szText)), (UINT)nCount); m_nBufferSize = m_nLength + 1; m_szText = AllocBuffer(m_nBufferSize); if (!m_szText) return FALSE; UTBStr::tcsncpy(m_szText, m_nBufferSize, szText, m_nLength+1); m_szText[m_nLength] = TEXT('\0'); return TRUE; }
/* * @implemented */ CHString::CHString(WCHAR ch, int nRepeat) throw (CHeap_Exception) { // Allow null initialize, in case something goes wrong m_pchData = afxPchNil; // If we have a char to insert if (nRepeat >= 1) { // Allocate a buffer big enough AllocBuffer(nRepeat); // And if possible, repeat char if (m_pchData) { for (int i = 0; i < nRepeat; ++i) { m_pchData[i] = ch; } } } }
char *FString::LockBuffer() { if (Data()->RefCount == 1) { // We're the only user, so we can lock it straight away Data()->RefCount = -1; } else if (Data()->RefCount < -1) { // Already locked; just add to the lock count Data()->RefCount--; } else { // Somebody else is also using this character buffer, so create a copy FStringData *old = Data(); AllocBuffer (old->Len); StrCopy (Chars, old->Chars(), old->Len); old->Release(); Data()->RefCount = -1; } return Chars; }
void Network::WriteMessage(const PayloadMsg& aMessage) { //std::cout << "NET:WriteMessage " << aMessage.ShortDebugString() << std::endl; size_t messageSize = aMessage.ByteSize(); AllocBuffer(messageSize); aMessage.SerializeToArray(mMessageBuffer, messageSize); HeaderMsg header; header.set_size(messageSize); size_t headerSize = header.ByteSize(); header.SerializeToArray(mHeaderBuffer, headerSize); if (boost::asio::write(*mSSLStream, boost::asio::buffer(mHeaderBuffer, headerSize)) != headerSize) { boost::throw_exception(std::runtime_error("Неудалось записать в сокет загловок!")); } if (boost::asio::write(*mSSLStream, boost::asio::buffer(mMessageBuffer, messageSize)) != messageSize) { boost::throw_exception(std::runtime_error("Неудалось записать в сокет сообщение!")); } }
/* * @implemented */ void CHString::CopyBeforeWrite() throw (CHeap_Exception) { CHStringData* Data; // First, we need to get reference count // And we also need to save Data for later copy Data = GetData(); if (Data->nRefs <= 1) { // If its not used, don't waste time to realloc, it will do the job return; } // Release current data - we are sure it won't be freed upon that point // Thanks to the reference count check previously done Release(); // Alloc new buffer and copy old data in it AllocBuffer(Data->nDataLength); wcsncpy(m_pchData, Data->data(), Data->nDataLength); }
void FString::StripRight () { size_t max = Len(), i; for (i = max - 1; i-- > 0; ) { if (!isspace(Chars[i])) break; } if (Data()->RefCount <= 1) { Chars[i+1] = '\0'; ReallocBuffer (i+1); } else { FStringData *old = Data(); AllocBuffer (i+1); StrCopy (Chars, old->Chars(), i+1); old->Release(); } }
// // Replace a substring of this string with another string // void STR_String::Replace(int pos, int num, rcSTR_String str) { //bounds(pos, 0, Length()-1); //bounds(pos+num, 0, Length()); assertd(num >= 1); if (str.Length() < num) { // Remove some data from the string by replacement memcpy(this->m_data + pos + str.Length(), this->m_data + pos + num, this->m_len - pos - num + 1); memcpy(this->m_data + pos, str.ReadPtr(), str.Length()); } else { // Insert zero or more characters into the string AllocBuffer(this->m_len + str.Length() - num, true); if (str.Length() != num) memcpy(this->m_data + pos + str.Length(), this->m_data + pos + num, Length() - pos - num + 1); memcpy(this->m_data + pos, str.ReadPtr(), str.Length()); } this->m_len += str.Length() - num; }
void FString::StripRight (const char *charset) { size_t max = Len(), i; for (i = max; i-- > 0; ) { if (!strchr (charset, Chars[i])) break; } if (Data()->RefCount <= 1) { Chars[i+1] = '\0'; ReallocBuffer (i+1); } else { FStringData *old = Data(); AllocBuffer (i+1); StrCopy (Chars, old->Chars(), i+1); old->Release(); } }
void CFileName::ConcatCopy(int nSrc1Len, LPCTSTR lpszSrc1Data, int nSrc2Len, LPCTSTR lpszSrc2Data) { int n=1; int nNewLen = nSrc1Len + nSrc2Len; if(nSrc1Len>0){ if(1==nSrc2Len && cSep==*lpszSrc2Data){ // Appending a single separator to a non-null string is a no-op lpszSrc2Data++; nSrc2Len--; nNewLen--; } else { // Count the intervening separators n=(cSep==lpszSrc1Data[nSrc1Len-1])+(cSep==*lpszSrc2Data); switch(n){ case 0: nNewLen++; break; case 1: break; case 2: lpszSrc2Data++; nSrc2Len--; nNewLen--; break; } } } if (nNewLen != 0) { AllocBuffer(nNewLen); memcpy(m_pchData, lpszSrc1Data, nSrc1Len*sizeof(TCHAR)); LPTSTR p=m_pchData+nSrc1Len; if(0==n){ *p++=cSep; } memcpy(p, lpszSrc2Data, nSrc2Len*sizeof(TCHAR)); } }
LPTSTR CString::GetBuffer(int nMinBufLength) { ASSERT(nMinBufLength >= 0); if (GetData()->nRefs > 1 || nMinBufLength > GetData()->nAllocLength) { // we have to grow the buffer CStringData* pOldData = GetData(); int nOldLen = GetData()->nDataLength; // AllocBuffer will tromp it if (nMinBufLength < nOldLen) nMinBufLength = nOldLen; AllocBuffer(nMinBufLength); memcpy(m_pchData, pOldData->data(), (nOldLen+1)*sizeof(TCHAR)); GetData()->nDataLength = nOldLen; CString::Release(pOldData); } ASSERT(GetData()->nRefs <= 1); // return a pointer to the character storage for this string ASSERT(m_pchData != NULL); return m_pchData; }
const HeapString& HeapString::operator=(const char* srcString) { size_t srcStringLen = StrLen(srcString); if (srcStringLen == 0) { Clear(); return *this; } HeapStringData* stringData = GetStringData(); if (srcStringLen > stringData->maxSize) AllocBuffer(srcStringLen); memcpy(m_string, srcString, srcStringLen * sizeof(char)); if (srcStringLen > stringData->maxSize) stringData->Free(); GetStringData()->stringLen = srcStringLen; return *this; }
/* * @implemented */ void CHString::ConcatCopy(int nSrc1Len, LPCWSTR lpszSrc1Data, int nSrc2Len, LPCWSTR lpszSrc2Data) throw (CHeap_Exception) { int TotalLen; if (nSrc1Len < 0 || nSrc2Len < 0) { RaiseException(ERROR_INVALID_PARAMETER, EXCEPTION_NONCONTINUABLE, 0, 0); } // If both len are null, do nothing TotalLen = nSrc1Len + nSrc2Len; if (TotalLen == 0) { return; } // Otherwise, allocate a new buffer to hold everything (caller will release previous buffer) AllocBuffer(TotalLen); // And concat stuff wcsncpy(m_pchData, lpszSrc1Data, nSrc1Len); wcsncpy(m_pchData + nSrc1Len, lpszSrc2Data, nSrc2Len); }
bool ShadowLayerForwarder::AllocBuffer(const gfxIntSize& aSize, gfxASurface::gfxContentType aContent, SurfaceDescriptor* aBuffer) { bool tryPlatformSurface = true; #ifdef DEBUG tryPlatformSurface = !PR_GetEnv("MOZ_LAYERS_FORCE_SHMEM_SURFACES"); #endif if (tryPlatformSurface && PlatformAllocBuffer(aSize, aContent, aBuffer)) { return true; } nsRefPtr<gfxSharedImageSurface> buffer; if (!AllocBuffer(aSize, aContent, getter_AddRefs(buffer))) return false; *aBuffer = buffer->GetShmem(); return true; }
// takes nLength elements of psz starting at nPos void wxStringImpl::InitWith(const wxStringCharType *psz, size_t nPos, size_t nLength) { Init(); // if the length is not given, assume the string to be NUL terminated if ( nLength == npos ) { wxASSERT_MSG( nPos <= wxStrlen(psz), wxT("index out of bounds") ); nLength = wxStrlen(psz + nPos); } STATISTICS_ADD(InitialLength, nLength); if ( nLength > 0 ) { // trailing '\0' is written in AllocBuffer() if ( !AllocBuffer(nLength) ) { wxFAIL_MSG( wxT("out of memory in wxStringImpl::InitWith") ); return; } wxStringMemcpy(m_pchData, psz + nPos, nLength); } }
/* * @implemented */ CHString::CHString(LPCSTR lpsz) throw (CHeap_Exception) { // Allow null initialize, in case something goes wrong m_pchData = afxPchNil; // If we have input string if (lpsz != 0) { // Get its length int Len = strlen(lpsz); if (Len) { // Allocate and convert the string AllocBuffer(Len); mbstowcsz(m_pchData, lpsz, Len + 1); // Releasing buffer here is to allow to // update the buffer size. We notify we're // done with changing the string: recompute its // length, please ReleaseBuffer(); } } }