1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef PARTITION_ALLOC_FREESLOT_BITMAP_CONSTANTS_H_
6 #define PARTITION_ALLOC_FREESLOT_BITMAP_CONSTANTS_H_
7 
8 #include <cstdint>
9 
10 #include "partition_alloc/partition_alloc_base/bits.h"
11 #include "partition_alloc/partition_alloc_base/compiler_specific.h"
12 #include "partition_alloc/partition_alloc_buildflags.h"
13 #include "partition_alloc/partition_alloc_constants.h"
14 #include "partition_alloc/partition_alloc_forward.h"
15 #include "partition_alloc/reservation_offset_table.h"
16 
17 namespace partition_alloc::internal {
18 
19 using FreeSlotBitmapCellType = uint64_t;
20 constexpr size_t kFreeSlotBitmapBitsPerCell =
21     sizeof(FreeSlotBitmapCellType) * CHAR_BIT;
22 constexpr size_t kFreeSlotBitmapOffsetMask = kFreeSlotBitmapBitsPerCell - 1;
23 
24 // The number of bits necessary for the bitmap is equal to the maximum number of
25 // slots in a super page.
26 constexpr size_t kFreeSlotBitmapSize =
27     (kSuperPageSize / kSmallestBucket) / CHAR_BIT;
28 
29 PA_ALWAYS_INLINE PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR size_t
ReservedFreeSlotBitmapSize()30 ReservedFreeSlotBitmapSize() {
31 #if BUILDFLAG(USE_FREESLOT_BITMAP)
32   return base::bits::AlignUp(kFreeSlotBitmapSize, PartitionPageSize());
33 #else
34   return 0;
35 #endif
36 }
37 
38 PA_ALWAYS_INLINE PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR size_t
CommittedFreeSlotBitmapSize()39 CommittedFreeSlotBitmapSize() {
40 #if BUILDFLAG(USE_FREESLOT_BITMAP)
41   return base::bits::AlignUp(kFreeSlotBitmapSize, SystemPageSize());
42 #else
43   return 0;
44 #endif
45 }
46 
47 PA_ALWAYS_INLINE PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR size_t
NumPartitionPagesPerFreeSlotBitmap()48 NumPartitionPagesPerFreeSlotBitmap() {
49   return ReservedFreeSlotBitmapSize() / PartitionPageSize();
50 }
51 
52 #if BUILDFLAG(USE_FREESLOT_BITMAP)
SuperPageFreeSlotBitmapAddr(uintptr_t super_page)53 PA_ALWAYS_INLINE uintptr_t SuperPageFreeSlotBitmapAddr(uintptr_t super_page) {
54   PA_DCHECK(!(super_page % kSuperPageAlignment));
55   return super_page + PartitionPageSize();
56 }
57 #endif
58 
59 }  // namespace partition_alloc::internal
60 
61 #endif  // PARTITION_ALLOC_FREESLOT_BITMAP_CONSTANTS_H_
62