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 : : #pragma once
16 : :
17 : : #include <cstddef>
18 : : #include <cstdint>
19 : :
20 : : namespace hpactor::mem {
21 : :
22 : : enum class SizeClass : uint8_t {
23 : : k32B = 0,
24 : : k64B = 1,
25 : : k128B = 2,
26 : : k256B = 3,
27 : : k512B = 4,
28 : : k1KB = 5,
29 : : k2KB = 6,
30 : : k4KB = 7,
31 : : };
32 : :
33 : : inline constexpr uint8_t kNumSizeClasses = 8;
34 : :
35 : : inline constexpr size_t kSizeClassTable[kNumSizeClasses] = {
36 : : 32, 64, 128, 256, 512, 1024, 2048, 4096
37 : : };
38 : :
39 : : // AllocHeader (32 bytes) + CanaryFooter (8 bytes) = 40 bytes overhead
40 : : inline constexpr size_t kAllocOverhead = 40;
41 : :
42 : 1160038 : constexpr size_t size_for_class(SizeClass sc) noexcept {
43 : 1160038 : return kSizeClassTable[static_cast<uint8_t>(sc)];
44 : : }
45 : :
46 : 1158640 : constexpr size_t block_size(SizeClass sc) noexcept {
47 : 1158640 : return size_for_class(sc) + kAllocOverhead;
48 : : }
49 : :
50 : 1 : constexpr size_t user_size(size_t block_sz) noexcept {
51 : 1 : return block_sz - kAllocOverhead;
52 : : }
53 : :
54 : 33 : inline SizeClass class_for_size(size_t user_bytes) noexcept {
55 : 97 : for (uint8_t i = 0; i < kNumSizeClasses; ++i) {
56 : 97 : if (user_bytes <= kSizeClassTable[i]) {
57 : 33 : return static_cast<SizeClass>(i);
58 : : }
59 : : }
60 : 0 : return SizeClass::k4KB;
61 : : }
62 : :
63 : : } // namespace hpactor::mem
|