• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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 BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_FORWARD_H_
6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_FORWARD_H_
7 
8 #include <algorithm>
9 #include <cstddef>
10 #include <cstdint>
11 #include <type_traits>
12 
13 #include "base/allocator/partition_allocator/partition_alloc_base/compiler_specific.h"
14 #include "base/allocator/partition_allocator/partition_alloc_base/component_export.h"
15 #include "base/allocator/partition_allocator/partition_alloc_base/debug/debugging_buildflags.h"
16 #include "base/allocator/partition_allocator/partition_alloc_config.h"
17 
18 namespace partition_alloc {
19 
20 namespace internal {
21 
22 // Alignment has two constraints:
23 // - Alignment requirement for scalar types: alignof(std::max_align_t)
24 // - Alignment requirement for operator new().
25 //
26 // The two are separate on Windows 64 bits, where the first one is 8 bytes, and
27 // the second one 16. We could technically return something different for
28 // malloc() and operator new(), but this would complicate things, and most of
29 // our allocations are presumably coming from operator new() anyway.
30 constexpr size_t kAlignment =
31     std::max(alignof(max_align_t),
32              static_cast<size_t>(__STDCPP_DEFAULT_NEW_ALIGNMENT__));
33 static_assert(kAlignment <= 16,
34               "PartitionAlloc doesn't support a fundamental alignment larger "
35               "than 16 bytes.");
36 
37 constexpr bool ThreadSafe = true;
38 
39 template <bool thread_safe>
40 struct SlotSpanMetadata;
41 
42 #if (BUILDFLAG(PA_DCHECK_IS_ON) ||                    \
43      BUILDFLAG(ENABLE_BACKUP_REF_PTR_SLOW_CHECKS)) && \
44     BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT)
45 PA_COMPONENT_EXPORT(PARTITION_ALLOC)
46 void CheckThatSlotOffsetIsZero(uintptr_t address);
47 #endif
48 
49 // This type trait verifies a type can be used as a pointer offset.
50 //
51 // We support pointer offsets in signed (ptrdiff_t) or unsigned (size_t) values.
52 // Smaller types are also allowed.
53 template <typename Z>
54 static constexpr bool is_offset_type =
55     std::is_integral_v<Z> && sizeof(Z) <= sizeof(ptrdiff_t);
56 
57 }  // namespace internal
58 
59 class PartitionStatsDumper;
60 
61 template <bool thread_safe = internal::ThreadSafe>
62 struct PartitionRoot;
63 
64 using ThreadSafePartitionRoot = PartitionRoot<internal::ThreadSafe>;
65 
66 }  // namespace partition_alloc
67 
68 // From https://clang.llvm.org/docs/AttributeReference.html#malloc:
69 //
70 // The malloc attribute indicates that the function acts like a system memory
71 // allocation function, returning a pointer to allocated storage disjoint from
72 // the storage for any other object accessible to the caller.
73 //
74 // Note that it doesn't apply to realloc()-type functions, as they can return
75 // the same pointer as the one passed as a parameter, as noted in e.g. stdlib.h
76 // on Linux systems.
77 #if PA_HAS_ATTRIBUTE(malloc)
78 #define PA_MALLOC_FN __attribute__((malloc))
79 #endif
80 
81 // Allows the compiler to assume that the return value is aligned on a
82 // kAlignment boundary. This is useful for e.g. using aligned vector
83 // instructions in the constructor for zeroing.
84 #if PA_HAS_ATTRIBUTE(assume_aligned)
85 #define PA_MALLOC_ALIGNED \
86   __attribute__((assume_aligned(::partition_alloc::internal::kAlignment)))
87 #endif
88 
89 #if !defined(PA_MALLOC_FN)
90 #define PA_MALLOC_FN
91 #endif
92 
93 #if !defined(PA_MALLOC_ALIGNED)
94 #define PA_MALLOC_ALIGNED
95 #endif
96 
97 #endif  // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_FORWARD_H_
98