Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : //
3 : : // Licensed under the Apache License, Version 2.0 (the "License");
4 : : // you may not use this file except in compliance with the License.
5 : : // You may obtain a copy of the License at
6 : : //
7 : : // http://www.apache.org/licenses/LICENSE-2.0
8 : : //
9 : : // Unless required by applicable law or agreed to in writing, software
10 : : // distributed under the License is distributed on an "AS IS" BASIS,
11 : : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : // See the License for the specific language governing permissions and
13 : : // limitations under the License.
14 : :
15 : : #include <hpactor/mem/thread_local_allocator.hpp>
16 : :
17 : : namespace hpactor::mem {
18 : :
19 : 28 : ThreadLocalAllocator::ThreadLocalAllocator() {
20 : 252 : for (uint8_t i = 0; i < kNumSizeClasses; ++i) {
21 : 224 : caches_[i] = new SlabCache(static_cast<SizeClass>(i));
22 : : }
23 : 28 : }
24 : :
25 : 28 : ThreadLocalAllocator::~ThreadLocalAllocator() {
26 : 252 : for (uint8_t i = 0; i < kNumSizeClasses; ++i) {
27 : 224 : delete caches_[i];
28 : : }
29 : 28 : }
30 : :
31 : 1254024 : void* ThreadLocalAllocator::allocate(SizeClass sc, ActorId owner) noexcept {
32 : 1254024 : return caches_[static_cast<uint8_t>(sc)]->allocate(owner);
33 : : }
34 : :
35 : 20 : void* ThreadLocalAllocator::allocate_bytes(size_t user_bytes, ActorId owner) noexcept {
36 : 20 : SizeClass sc = class_for_size(user_bytes);
37 : 20 : return allocate(sc, owner);
38 : : }
39 : :
40 : 1154024 : void ThreadLocalAllocator::deallocate(void* user_ptr) noexcept {
41 : 1154024 : auto* hdr = AllocHeader::from_user_data(user_ptr);
42 : 1154024 : SizeClass sc = static_cast<SizeClass>(hdr->size_class);
43 : 1154024 : caches_[static_cast<uint8_t>(sc)]->deallocate(user_ptr);
44 : 1154024 : }
45 : :
46 : 0 : const SlabCache::Stats& ThreadLocalAllocator::stats(SizeClass sc) const noexcept {
47 : 0 : return caches_[static_cast<uint8_t>(sc)]->stats();
48 : : }
49 : :
50 : : } // namespace hpactor::mem
|