// Set the string's storage capacity to the designated value. size_t UtlString::capacity(size_t N) { #ifdef _VXWORKS size_t maxFreeBlockSize = 0; #endif char* newData = 0; if(mCapacity < N && N > 0) { if(mCapacity + UTLSTRING_MIN_INCREMENT > N) { N = mCapacity + UTLSTRING_MIN_INCREMENT; } #ifdef _VXWORKS if (N > CHECK_BLOCK_THRESHOLD) { maxFreeBlockSize = FindMaxFreeBlock(); // We are not going over the largest block size then try to // allocate otherwise we will return NULL, and the size returned // will be zero! if (N < maxFreeBlockSize) { newData = new char[N]; } } else { newData = new char[N]; } #else //for all other OS's lets assume it really returns NULL and not crashes newData = new char[N]; #endif if (newData) { if(mSize > 0 && mpData) { memcpy(newData, mpData, mSize); } else { newData[0] = '\000'; } if(mpData && mpData != mBuiltIn) { delete[] mpData; } mpData = newData; mCapacity = N; } } return(mCapacity); }
// Set the string's storage capacity to the designated value. size_t UtlString::capacity(size_t N) { #ifdef _VXWORKS size_t maxFreeBlockSize = 0; #endif size_t newSize = 0; char* newData = 0; if(mCapacity < N && N > 0) { if(mCapacity + UTLSTRING_MIN_INCREMENT > N) { N = mCapacity + UTLSTRING_MIN_INCREMENT; } #ifdef _VXWORKS if (N > CHECK_BLOCK_THRESHOLD) { maxFreeBlockSize = FindMaxFreeBlock(); // We are not going over the largest block size then try to // allocate otherwise we will return NULL, and the size returned // will be zero! if (N < maxFreeBlockSize) { newData = new char[N]; } } else { newData = new char[N]; } #else //for all other OS's lets assume it really returns NULL and not crashes newData = new char[N]; #endif if (newData) { if(mSize > 0 && mpData) { memcpy(newData, mpData, mSize); } else { newData[0] = '\0'; } if(mpData && mpData != mBuiltIn) { delete[] mpData; } mpData = newData; newSize = mCapacity = N; } else { osPrintf("******** ERROR******* : UtlString::capacity failed (%d). Memory not allocated!\n", N); #ifdef _VXWORKS osPrintf("******** ERROR******* : Largest block = %d, requested size = %d\n",maxFreeBlockSize, N); #endif newSize = 0; } } else { newSize = mCapacity; } return(newSize); }