| 299 | } |
| 300 | |
| 301 | size_t BatchAllocate(size_t num, void** ret) { |
| 302 | mutex_lock l(mu_); |
| 303 | auto allocated = free_queue_.PopBatch(num, ret); |
| 304 | auto remains = num - allocated; |
| 305 | if (remains == 0) { |
| 306 | return num; |
| 307 | } |
| 308 | |
| 309 | void** cur = ret + allocated; |
| 310 | allocated = current_chunk_->BatchAllocate(remains, cur); |
| 311 | remains -= allocated; |
| 312 | if (remains == 0) { |
| 313 | return num; |
| 314 | } |
| 315 | |
| 316 | cur += allocated; |
| 317 | if (remains < current_chunk_->Count()) { |
| 318 | current_chunk_ = CreateChunk(); |
| 319 | |
| 320 | allocated = current_chunk_->BatchAllocate(remains, cur); |
| 321 | return num - (remains - allocated); |
| 322 | } |
| 323 | |
| 324 | // Allocate in multiple chunks. |
| 325 | auto chunk_num = remains / current_chunk_->Count(); |
| 326 | for (int i = 0; i < chunk_num; ++i) { |
| 327 | current_chunk_ = CreateChunk(); |
| 328 | allocated = current_chunk_->FullAllocate(cur); |
| 329 | |
| 330 | cur += allocated; |
| 331 | remains -= allocated; |
| 332 | } |
| 333 | |
| 334 | current_chunk_ = CreateChunk(); |
| 335 | allocated = current_chunk_->BatchAllocate(remains, cur); |
| 336 | return num - (remains - allocated); |
| 337 | } |
| 338 | |
| 339 | void BatchDeallocate(std::vector<void *> &ptrs) { |
| 340 | mutex_lock l(mu_); |
no test coverage detected