| 369 | // Protected function does "reallocate" |
| 370 | |
| 371 | vtkStringArray::ValueType* vtkStringArray::ResizeAndExtend(vtkIdType sz) |
| 372 | { |
| 373 | ValueType* newArray; |
| 374 | vtkIdType newSize; |
| 375 | |
| 376 | if (sz > this->Size) |
| 377 | { |
| 378 | // Requested size is bigger than current size. Allocate enough |
| 379 | // memory to fit the requested size and be more than double the |
| 380 | // currently allocated memory. |
| 381 | newSize = (this->Size + 1) + sz; |
| 382 | } |
| 383 | else if (sz == this->Size) |
| 384 | { |
| 385 | // Requested size is equal to current size. Do nothing. |
| 386 | return this->Array; |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | // Requested size is smaller than current size. Squeeze the |
| 391 | // memory. |
| 392 | newSize = sz; |
| 393 | } |
| 394 | |
| 395 | if (newSize <= 0) |
| 396 | { |
| 397 | this->Initialize(); |
| 398 | return nullptr; |
| 399 | } |
| 400 | |
| 401 | newArray = new ValueType[newSize]; |
| 402 | if (!newArray) |
| 403 | { |
| 404 | vtkErrorMacro("Cannot allocate memory\n"); |
| 405 | return nullptr; |
| 406 | } |
| 407 | |
| 408 | if (this->Array) |
| 409 | { |
| 410 | // can't use memcpy here |
| 411 | vtkIdType numCopy = (newSize < this->Size ? newSize : this->Size); |
| 412 | for (vtkIdType i = 0; i < numCopy; ++i) |
| 413 | { |
| 414 | newArray[i] = this->Array[i]; |
| 415 | } |
| 416 | if (this->DeleteFunction) |
| 417 | { |
| 418 | this->DeleteFunction(this->Array); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | if (newSize < this->Size) |
| 423 | { |
| 424 | this->MaxId = newSize - 1; |
| 425 | } |
| 426 | this->Size = newSize; |
| 427 | this->Array = newArray; |
| 428 | this->DeleteFunction = DefaultDeleteFunction; |
no test coverage detected