| 345 | } |
| 346 | |
| 347 | ULONG ConfigStorage::allocSlot(ULONG slotSize) |
| 348 | { |
| 349 | fb_assert(validate()); |
| 350 | |
| 351 | TraceCSHeader* header = m_sharedMemory->getHeader(); |
| 352 | |
| 353 | if (header->slots_free == 0 && header->slots_cnt == TraceCSHeader::TRACE_STORAGE_MAX_SLOTS) |
| 354 | (Arg::Gds(isc_random) << Arg::Str("No enough free slots")).raise(); |
| 355 | |
| 356 | // try to extend shared memory, if needed |
| 357 | if (header->mem_used + slotSize > header->mem_allocated) |
| 358 | { |
| 359 | if (header->mem_allocated >= header->mem_max_size) |
| 360 | (Arg::Gds(isc_random) << Arg::Str("No enough memory for new trase session")).raise(); |
| 361 | |
| 362 | ULONG newAlloc = FB_ALIGN(header->mem_used + slotSize, header->mem_allocated); |
| 363 | newAlloc = MIN(newAlloc, header->mem_max_size); |
| 364 | |
| 365 | #ifdef HAVE_OBJECT_MAP |
| 366 | FbLocalStatus status; |
| 367 | if (!m_sharedMemory->remapFile(&status, newAlloc, true)) |
| 368 | status_exception::raise(&status); |
| 369 | #else |
| 370 | (Arg::Gds(isc_random) << Arg::Str("Can not remap trace storage memory")).raise(); |
| 371 | #endif |
| 372 | header = m_sharedMemory->getHeader(); |
| 373 | header->mem_allocated = m_sharedMemory->sh_mem_length_mapped; |
| 374 | |
| 375 | fb_assert(validate()); |
| 376 | } |
| 377 | |
| 378 | fb_assert(header->mem_used + slotSize <= header->mem_allocated); |
| 379 | setDirty(); |
| 380 | |
| 381 | bool reuseFreeSlot = false; |
| 382 | if (header->slots_free) |
| 383 | { |
| 384 | // find free slot with best fit size |
| 385 | ULONG idxFound = 0; |
| 386 | ULONG lenFound = 0; |
| 387 | for (ULONG i = 0; i < header->slots_cnt; i++) |
| 388 | { |
| 389 | TraceCSHeader::Slot* slot = header->slots + i; |
| 390 | if (!slot->used && slot->size >= slotSize && |
| 391 | (!lenFound || lenFound > slot->size)) |
| 392 | { |
| 393 | lenFound = slot->size; |
| 394 | idxFound = i; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | if (lenFound) |
| 399 | { |
| 400 | header->slots_free--; |
| 401 | reuseFreeSlot = true; |
| 402 | |
| 403 | // move free slot to the top position |
| 404 | if (idxFound != header->slots_cnt - 1) |