| 327 | // |
| 328 | |
| 329 | void TempSpace::extend(FB_SIZE_T size) |
| 330 | { |
| 331 | logicalSize += size; |
| 332 | |
| 333 | if (logicalSize > physicalSize) |
| 334 | { |
| 335 | const FB_SIZE_T initialSize = initialBuffer.getCount(); |
| 336 | |
| 337 | // If the dynamic mode is specified, then we allocate new blocks |
| 338 | // by growing the same initial memory block in the specified chunks. |
| 339 | // Once the limit (64KB) is reached, we switch to the generic algorithm |
| 340 | // (1MB blocks), copy the existing data there and free the initial buffer. |
| 341 | // |
| 342 | // This mode should not be used if the caller never works with small blocks. |
| 343 | // Also, it MUST NOT be used if the caller deals with inMemory() or allocateBatch() |
| 344 | // routines and caches the pointers to use them later. These pointers may become |
| 345 | // invalid after resizing the initial block or after switching to large blocks. |
| 346 | |
| 347 | if (initiallyDynamic && logicalSize < MIN_TEMP_BLOCK_SIZE) |
| 348 | { |
| 349 | // allocate or extend the initial dynamic block, it will grow up to 64KB |
| 350 | if (!initialSize) |
| 351 | { |
| 352 | fb_assert(!head && !tail); |
| 353 | head = tail = FB_NEW_POOL(pool) InitialBlock(initialBuffer.getBuffer(size), size); |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | fb_assert(head == tail); |
| 358 | size += initialSize; |
| 359 | initialBuffer.resize(size); |
| 360 | new(head) InitialBlock(initialBuffer.begin(), size); |
| 361 | } |
| 362 | |
| 363 | physicalSize = size; |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | if (initialSize) |
| 368 | { |
| 369 | fb_assert(head == tail); |
| 370 | delete head; |
| 371 | head = tail = NULL; |
| 372 | size = static_cast<FB_SIZE_T>(FB_ALIGN(logicalSize, minBlockSize)); |
| 373 | physicalSize = size; |
| 374 | } |
| 375 | else |
| 376 | { |
| 377 | size = static_cast<FB_SIZE_T>(FB_ALIGN(logicalSize - physicalSize, minBlockSize)); |
| 378 | physicalSize += size; |
| 379 | } |
| 380 | |
| 381 | Block* block = NULL; |
| 382 | |
| 383 | { // scope |
| 384 | TempCacheLimitGuard guard(GET_DBB()); |
| 385 | |
| 386 | if (guard.reserve(size)) |
no test coverage detected