1 // Copyright 2023 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_PARTITION_DCHECK_HELPER_H_
6 #define PARTITION_ALLOC_PARTITION_DCHECK_HELPER_H_
7 
8 #include "partition_alloc/partition_alloc_base/compiler_specific.h"
9 #include "partition_alloc/partition_alloc_base/component_export.h"
10 #include "partition_alloc/partition_alloc_forward.h"
11 #include "partition_alloc/partition_lock.h"
12 
13 namespace partition_alloc::internal {
14 
15 struct PartitionSuperPageExtentEntry;
16 
17 #if BUILDFLAG(PA_DCHECK_IS_ON)
18 
19 // To allow these asserts to have empty bodies in no-DCHECK() builds, while
20 // avoiding issues with circular includes.
21 #define PA_EMPTY_BODY_IF_DCHECK_IS_OFF()
22 // Export symbol if dcheck-is-on. Because the body is not empty.
23 #define PA_EXPORT_IF_DCHECK_IS_ON() PA_COMPONENT_EXPORT(PARTITION_ALLOC)
24 
25 #else  // BUILDFLAG(PA_DCHECK_IS_ON)
26 
27 // The static_assert() eats follow-on semicolons.
28 #define PA_EMPTY_BODY_IF_DCHECK_IS_OFF() \
29   {}                                     \
30   static_assert(true)
31 // inline if dcheck-is-off so it's no overhead.
32 #define PA_EXPORT_IF_DCHECK_IS_ON() PA_ALWAYS_INLINE
33 
34 #endif  // BUILDFLAG(PA_DCHECK_IS_ON)
35 
36 PA_EXPORT_IF_DCHECK_IS_ON()
37 void DCheckIsValidSlotSpan(internal::SlotSpanMetadata* slot_span)
38     PA_EMPTY_BODY_IF_DCHECK_IS_OFF();
39 
40 PA_EXPORT_IF_DCHECK_IS_ON()
41 void DCheckIsWithInSuperPagePayload(uintptr_t address)
42     PA_EMPTY_BODY_IF_DCHECK_IS_OFF();
43 
44 PA_EXPORT_IF_DCHECK_IS_ON()
45 void DCheckNumberOfPartitionPagesInSuperPagePayload(
46     const PartitionSuperPageExtentEntry* entry,
47     const PartitionRoot* root,
48     size_t number_of_nonempty_slot_spans) PA_EMPTY_BODY_IF_DCHECK_IS_OFF();
49 
50 PA_EXPORT_IF_DCHECK_IS_ON()
51 void DCheckIsValidShiftFromSlotStart(internal::SlotSpanMetadata* slot_span,
52                                      size_t shift_from_slot_start)
53     PA_EMPTY_BODY_IF_DCHECK_IS_OFF();
54 
55 // Checks that the object is a multiple of slot size (i.e. at a slot start).
56 PA_EXPORT_IF_DCHECK_IS_ON()
57 void DCheckIsValidObjectAddress(internal::SlotSpanMetadata* slot_span,
58                                 uintptr_t object_addr)
59     PA_EMPTY_BODY_IF_DCHECK_IS_OFF();
60 
61 PA_EXPORT_IF_DCHECK_IS_ON()
62 void DCheckRootLockIsAcquired(PartitionRoot* root)
63     PA_EMPTY_BODY_IF_DCHECK_IS_OFF();
64 
65 }  // namespace partition_alloc::internal
66 
67 #endif  // PARTITION_ALLOC_PARTITION_DCHECK_HELPER_H_
68