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 PARTITION_ALLOC_PARTITION_ALLOC_FORWARD_H_
6 #define PARTITION_ALLOC_PARTITION_ALLOC_FORWARD_H_
7 
8 #include <algorithm>
9 #include <cstddef>
10 #include <cstdint>
11 #include <type_traits>
12 
13 #include "partition_alloc/partition_alloc_base/compiler_specific.h"
14 #include "partition_alloc/partition_alloc_base/component_export.h"
15 #include "partition_alloc/partition_alloc_base/debug/debugging_buildflags.h"
16 #include "partition_alloc/partition_alloc_base/thread_annotations.h"
17 #include "partition_alloc/partition_alloc_config.h"
18 
19 namespace partition_alloc {
20 
21 namespace internal {
22 
23 // Alignment has two constraints:
24 // - Alignment requirement for scalar types: alignof(std::max_align_t)
25 // - Alignment requirement for operator new().
26 //
27 // The two are separate on Windows 64 bits, where the first one is 8 bytes, and
28 // the second one 16. We could technically return something different for
29 // malloc() and operator new(), but this would complicate things, and most of
30 // our allocations are presumably coming from operator new() anyway.
31 constexpr size_t kAlignment =
32     std::max(alignof(max_align_t),
33              static_cast<size_t>(__STDCPP_DEFAULT_NEW_ALIGNMENT__));
34 static_assert(kAlignment <= 16,
35               "PartitionAlloc doesn't support a fundamental alignment larger "
36               "than 16 bytes.");
37 
38 struct SlotSpanMetadata;
39 class PA_LOCKABLE Lock;
40 
41 // This type trait verifies a type can be used as a pointer offset.
42 //
43 // We support pointer offsets in signed (ptrdiff_t) or unsigned (size_t) values.
44 // Smaller types are also allowed.
45 template <typename Z>
46 static constexpr bool is_offset_type =
47     std::is_integral_v<Z> && sizeof(Z) <= sizeof(ptrdiff_t);
48 
49 }  // namespace internal
50 
51 class PartitionStatsDumper;
52 
53 struct PartitionRoot;
54 
55 namespace internal {
56 // Declare PartitionRootLock() for thread analysis. Its implementation
57 // is defined in partition_root.h.
58 Lock& PartitionRootLock(PartitionRoot*);
59 }  // namespace internal
60 
61 }  // namespace partition_alloc
62 
63 // From https://clang.llvm.org/docs/AttributeReference.html#malloc:
64 //
65 // The malloc attribute indicates that the function acts like a system memory
66 // allocation function, returning a pointer to allocated storage disjoint from
67 // the storage for any other object accessible to the caller.
68 //
69 // Note that it doesn't apply to realloc()-type functions, as they can return
70 // the same pointer as the one passed as a parameter, as noted in e.g. stdlib.h
71 // on Linux systems.
72 #if PA_HAS_ATTRIBUTE(malloc)
73 #define PA_MALLOC_FN __attribute__((malloc))
74 #endif
75 
76 #if !defined(PA_MALLOC_FN)
77 #define PA_MALLOC_FN
78 #endif
79 
80 #if !defined(PA_MALLOC_ALIGNED)
81 #define PA_MALLOC_ALIGNED
82 #endif
83 
84 #endif  // PARTITION_ALLOC_PARTITION_ALLOC_FORWARD_H_
85