| 330 | } |
| 331 | |
| 332 | static Status ReallocateAligned(int64_t old_size, int64_t new_size, int64_t alignment, |
| 333 | uint8_t** ptr) { |
| 334 | uint8_t* previous_ptr = *ptr; |
| 335 | if (previous_ptr == memory_pool::internal::kZeroSizeArea) { |
| 336 | DCHECK_EQ(old_size, 0); |
| 337 | return AllocateAligned(new_size, alignment, ptr); |
| 338 | } |
| 339 | if (new_size == 0) { |
| 340 | DeallocateAligned(previous_ptr, old_size, alignment); |
| 341 | *ptr = memory_pool::internal::kZeroSizeArea; |
| 342 | return Status::OK(); |
| 343 | } |
| 344 | // Note: We cannot use realloc() here as it doesn't guarantee alignment. |
| 345 | |
| 346 | // Allocate new chunk |
| 347 | uint8_t* out = nullptr; |
| 348 | RETURN_NOT_OK(AllocateAligned(new_size, alignment, &out)); |
| 349 | DCHECK(out); |
| 350 | // Copy contents and release old memory chunk |
| 351 | memcpy(out, *ptr, static_cast<size_t>(std::min(new_size, old_size))); |
| 352 | #ifdef _WIN32 |
| 353 | _aligned_free(*ptr); |
| 354 | #else |
| 355 | free(*ptr); |
| 356 | #endif // defined(_WIN32) |
| 357 | *ptr = out; |
| 358 | return Status::OK(); |
| 359 | } |
| 360 | |
| 361 | static void DeallocateAligned(uint8_t* ptr, int64_t size, int64_t /*alignment*/) { |
| 362 | if (ptr == memory_pool::internal::kZeroSizeArea) { |