3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2013 Nest Labs, Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include <support/Pool.h>
26 StaticAllocatorBitmap::StaticAllocatorBitmap(void * storage, std::atomic<tBitChunkType> * usage, size_t capacity,
28 StaticAllocatorBase(capacity),
29 mElements(storage), mElementSize(elementSize), mUsage(usage)
31 for (size_t word = 0; word * kBitChunkSize < Capacity(); ++word)
33 mUsage[word].store(0);
37 void * StaticAllocatorBitmap::Allocate()
39 for (size_t word = 0; word * kBitChunkSize < Capacity(); ++word)
41 auto & usage = mUsage[word];
42 auto value = usage.load(std::memory_order_relaxed);
43 for (size_t offset = 0; offset < kBitChunkSize && offset + word * kBitChunkSize < Capacity(); ++offset)
45 if ((value & (kBit1 << offset)) == 0)
47 if (usage.compare_exchange_strong(value, value | (kBit1 << offset)))
50 return At(word * kBitChunkSize + offset);
54 value = usage.load(std::memory_order_relaxed); // if there is a race, update new usage
62 void StaticAllocatorBitmap::Deallocate(void * element)
64 size_t index = IndexOf(element);
65 size_t word = index / kBitChunkSize;
66 size_t offset = index - (word * kBitChunkSize);
68 // ensure the element is in the pool
69 assert(index < Capacity());
71 auto value = mUsage[word].fetch_and(~(kBit1 << offset));
72 nlASSERT((value & (kBit1 << offset)) != 0); // assert fail when free an unused slot