| 344 | } |
| 345 | |
| 346 | std::shared_ptr<MemoryPool> MemoryManager::addRootPool( |
| 347 | const std::string& name, |
| 348 | int64_t maxCapacity, |
| 349 | std::unique_ptr<MemoryReclaimer> reclaimer, |
| 350 | const std::optional<MemoryPool::DebugOptions>& poolDebugOpts) { |
| 351 | std::string poolName = name; |
| 352 | if (poolName.empty()) { |
| 353 | static std::atomic<int64_t> poolId{0}; |
| 354 | poolName = fmt::format("default_root_{}", poolId++); |
| 355 | } |
| 356 | |
| 357 | MemoryPool::Options options; |
| 358 | options.alignment = alignment_; |
| 359 | options.maxCapacity = maxCapacity; |
| 360 | options.trackUsage = true; |
| 361 | options.coreOnAllocationFailureEnabled = coreOnAllocationFailureEnabled_; |
| 362 | options.getPreferredSize = getPreferredSize_; |
| 363 | options.debugOptions = poolDebugOpts; |
| 364 | |
| 365 | options.poolRegex = poolRegex_; |
| 366 | options.singleAllocationThreshold = singleAllocationThreshold_; |
| 367 | options.accumulativeAllocationThreshold = accumulativeAllocationThreshold_; |
| 368 | options.enableDynamicMemoryQuotaManager = enableDynamicMemoryQuotaManager_; |
| 369 | |
| 370 | auto pool = createRootPool(poolName, reclaimer, options); |
| 371 | if (!disableMemoryPoolTracking_) { |
| 372 | try { |
| 373 | std::unique_lock guard{mutex_}; |
| 374 | if (pools_.find(poolName) != pools_.end()) { |
| 375 | BOLT_FAIL("Duplicate root pool name found: {}", poolName); |
| 376 | } |
| 377 | pools_.emplace(poolName, pool); |
| 378 | } catch (const BoltRuntimeError&) { |
| 379 | arbitrator_->removePool(pool.get()); |
| 380 | throw; |
| 381 | } |
| 382 | } |
| 383 | // NOTE: we need to set destruction callback at the end to avoid potential |
| 384 | // deadlock or failure because of duplicate memory pool name or unexpected |
| 385 | // failure to add memory pool to the arbitrator. |
| 386 | pool->setDestructionCallback(poolDestructionCb_); |
| 387 | return pool; |
| 388 | } |
| 389 | |
| 390 | std::shared_ptr<MemoryPool> MemoryManager::addLeafPool( |
| 391 | const std::string& name, |