/*! Makes this string writable, and resizes the buffer to \a length bytes (not including the terminating null). @param length The length of the new buffer in bytes. @param copy If true, the current string will be copied into the new string. */ status_t BString::_MakeWritable(int32 length, bool copy) { char* newData = NULL; if (atomic_get(&_ReferenceCount()) > 1) { // we might share our data with someone else if (copy) newData = _Clone(fPrivateData, length); else newData = _Allocate(length); if (newData == NULL) return B_NO_MEMORY; if (atomic_add(&_ReferenceCount(), -1) == 1) { // someone else left, we were the last owner _FreePrivateData(); } } else { // we don't share our data with someone else newData = _Resize(length); if (newData == NULL) return B_NO_MEMORY; } fPrivateData = newData; return B_OK; }
// Allocate block for metadata // flags is BLOCKGROUP's flags status_t ExtentAllocator::AllocateTreeBlock(uint64& found, uint64 start, uint64 flags) { // TODO: implement more features here with flags, e.g DUP, RAID, etc BlockGroup blockGroup(fVolume->ExtentTree()); status_t status = blockGroup.Initialize(flags); if (status != B_OK) return status; if (start == (uint64)-1) start = blockGroup.Start(); // normalize inputs uint64 remainder = start % fVolume->BlockSize(); if (remainder != 0) start += fVolume->BlockSize() - remainder; status = _Allocate(found, start, fVolume->BlockSize(), BTRFS_EXTENT_FLAG_TREE_BLOCK); if (status != B_OK) return status; // check here because tree block locate in 2 blockgroups (system and // metadata), and there might be a case one can get over the limit. if (found >= blockGroup.End()) return B_BAD_DATA; return B_OK; }
BString& BString::_DoCharacterEscape(const char* string, const char* setOfCharsToEscape, char escapeChar) { if (_MakeWritable(string_length(string), false) != B_OK) return *this; memcpy(fPrivateData, string, Length()); PosVect positions; int32 length = Length(); int32 pos; for (int32 offset = 0; offset < length; offset += pos + 1) { pos = strcspn(fPrivateData + offset, setOfCharsToEscape); if (pos < length - offset && !positions.Add(offset + pos)) return *this; } uint32 count = positions.CountItems(); int32 newLength = length + count; if (!newLength) { _Resize(0); return *this; } char* newData = _Allocate(newLength); if (newData) { char* oldString = fPrivateData; char* newString = newData; int32 lastPos = 0; for (uint32 i = 0; i < count; ++i) { pos = positions.ItemAt(i); length = pos - lastPos; if (length > 0) { memcpy(newString, oldString, length); oldString += length; newString += length; } *newString++ = escapeChar; *newString++ = *oldString++; lastPos = pos + 1; } length = Length() + 1 - lastPos; if (length > 0) memcpy(newString, oldString, length); _FreePrivateData(); fPrivateData = newData; } return *this; }
T* Get(unsigned long _Size) { if (m_p == INVALID_PTR) { m_p = _Allocate(_Size, m_p); } else { m_p = _ReAllocate(_Size, m_p, m_Size); } m_Size = _Size; return m_p; }
char* BString::_Clone(const char* data, int32 length) { char* newData = _Allocate(length); if (newData == NULL) return NULL; if (data != NULL && length > 0) { // "data" may not span over the whole length strncpy(newData, data, length); } return newData; }
void * DebugAlloc::_ReAllocate(LPVOID lpData, size_t dwSize) { LPVOID lpRet; if(!lpData) { lpRet = _Allocate(dwSize); return lpRet; } OSEnterMutex(hDebugMutex); if(bEnableTracking) { ++allocationCounter; ++totalAllocations; } if(bEnableTracking && allocationCounter == memoryBreakID) ProgramBreak(); lpRet = FastAlloc::_ReAllocate(lpData, dwSize); /*if(bEnableTracking) {*/ for(DWORD i=0;i<numAllocations;i++) { if(AllocationList[i].Address == lpData) { if(bEnableTracking) AllocationList[i].allocationID = allocationCounter; else AllocationList[i].allocationID = INVALID; AllocationList[i].Address = lpRet; break; } } //} OSLeaveMutex(hDebugMutex); return lpRet; }
void BString::_ReplaceAtPositions(const PosVect* positions, int32 searchLength, const char* with, int32 withLength) { int32 length = Length(); uint32 count = positions->CountItems(); int32 newLength = length + count * (withLength - searchLength); if (!newLength) { _Resize(0); return; } char* newData = _Allocate(newLength); if (newData == NULL) return; char* oldString = fPrivateData; char* newString = newData; int32 lastPos = 0; for (uint32 i = 0; i < count; ++i) { int32 pos = positions->ItemAt(i); length = pos - lastPos; if (length > 0) { memcpy(newString, oldString, length); oldString += length; newString += length; } memcpy(newString, with, withLength); oldString += searchLength; newString += withLength; lastPos = pos + searchLength; } length = Length() + 1 - lastPos; if (length > 0) memcpy(newString, oldString, length); _FreePrivateData(); fPrivateData = newData; }
// Allocate block for file data status_t ExtentAllocator::AllocateDataBlock(uint64& found, uint64 size, uint64 start, uint64 flags) { // TODO: implement more features here with flags, e.g DUP, RAID, etc if (start == (uint64)-1) { BlockGroup blockGroup(fVolume->ExtentTree()); status_t status = blockGroup.Initialize(flags); if (status != B_OK) return status; start = blockGroup.Start(); } // normalize inputs uint64 remainder = start % fVolume->SectorSize(); if (remainder != 0) start += fVolume->SectorSize() - remainder; size = size / fVolume->SectorSize() * fVolume->SectorSize(); return _Allocate(found, start, size, BTRFS_EXTENT_FLAG_DATA); }
byte * _Mem_Allocate ( int32 size, uint64 type ) { MemorySpace * ms = _Q_->MemorySpace0 ; switch ( type ) { case OPENVMTIL: { if ( ms && ms->OpenVmTilSpace ) return _Allocate ( size, ms->OpenVmTilSpace ) ; else return MemList_AllocateChunk ( &_MemList_, size, OPENVMTIL ) ; } case LISP: case OBJECT_MEMORY: return _Allocate ( size, ms->ObjectSpace ) ; case TEMP_OBJECT_MEMORY: return _Allocate ( size, ms->TempObjectSpace ) ; case DICTIONARY: return _Allocate ( size, ms->DictionarySpace ) ; case SESSION: return _Allocate ( size, ms->SessionObjectsSpace ) ; case CODE: return _Allocate ( size, ms->CodeSpace ) ; case BUFFER: return _Allocate ( size, ms->BufferSpace ) ; case HISTORY: return _Allocate ( size, ms->HistorySpace ) ; case LISP_TEMP: return _Allocate ( size, ms->LispTempSpace ) ; case CONTEXT: return _Allocate ( size, ms->ContextSpace ) ; case COMPILER_TEMP_OBJECT_MEMORY: return _Allocate ( size, ms->CompilerTempObjectSpace ) ; case CFRTIL: case DATA_STACK: return _Allocate ( size, ms->CfrTilInternalSpace ) ; default: CfrTil_Exception ( MEMORY_ALLOCATION_ERROR, QUIT ) ; } }