@@ -251,22 +251,22 @@ std::shared_ptr<uint64_t> TokenPool::getToken() {
251251 size_t index = (token - self->baseAddr_ ) / UINT64_WIDTH;
252252 size_t bit = (token - self->baseAddr_ ) % UINT64_WIDTH;
253253 uint64_t mask = 1UL << bit;
254- if ((self->allocationMap_ [index] & mask) == 0 ) {
255- WARN (" TokenPool tried to free a token that was not allocated" );
256- return ;
257- }
258254 self->allocationMap_ [index] &= ~mask;
259255 };
260256
261257 size_t size = allocationMap_.size ();
262258 for (size_t i = 0 ; i < size; i++) {
259+ uint64_t ullong = allocationMap_[i].to_ullong ();
263260 uint64_t mask = (i + 1 == size) ? tailMask_ : ~0ULL ;
264- uint64_t holes = (~allocationMap_[i] ) & mask;
261+ uint64_t holes = (~ullong ) & mask;
265262 if (!holes) continue ;
266- size_t bit = __builtin_ctzll (holes);
267- allocationMap_[i] |= (1UL << bit);
268- INFO (MSCCLPP_ALLOC, " TokenPool allocated token at addr %p" , baseAddr_ + i * UINT64_WIDTH + bit);
269- return std::shared_ptr<uint64_t >(baseAddr_ + i * UINT64_WIDTH + bit, deleter);
263+ for (int bit = 0 ; bit < UINT64_WIDTH; bit++) {
264+ if (holes & (1UL << bit)) {
265+ allocationMap_[i].set (bit);
266+ INFO (MSCCLPP_ALLOC, " TokenPool allocated token at addr %p" , baseAddr_ + i * UINT64_WIDTH + bit);
267+ return std::shared_ptr<uint64_t >(baseAddr_ + i * UINT64_WIDTH + bit, deleter);
268+ }
269+ }
270270 }
271271 throw Error (" TokenPool is exhausted" , ErrorCode::InternalError);
272272}
0 commit comments