partition_alloc_forward.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_FORWARD_H_
  5. #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_FORWARD_H_
  6. #include <algorithm>
  7. #include <cstddef>
  8. #include "base/allocator/partition_allocator/partition_alloc_base/compiler_specific.h"
  9. #include "base/allocator/partition_allocator/partition_alloc_base/component_export.h"
  10. #include "base/allocator/partition_allocator/partition_alloc_base/debug/debugging_buildflags.h"
  11. #include "base/allocator/partition_allocator/partition_alloc_buildflags.h"
  12. namespace partition_alloc {
  13. namespace internal {
  14. // Alignment has two constraints:
  15. // - Alignment requirement for scalar types: alignof(std::max_align_t)
  16. // - Alignment requirement for operator new().
  17. //
  18. // The two are separate on Windows 64 bits, where the first one is 8 bytes, and
  19. // the second one 16. We could technically return something different for
  20. // malloc() and operator new(), but this would complicate things, and most of
  21. // our allocations are presumably coming from operator new() anyway.
  22. constexpr size_t kAlignment =
  23. std::max(alignof(max_align_t),
  24. static_cast<size_t>(__STDCPP_DEFAULT_NEW_ALIGNMENT__));
  25. static_assert(kAlignment <= 16,
  26. "PartitionAlloc doesn't support a fundamental alignment larger "
  27. "than 16 bytes.");
  28. constexpr bool ThreadSafe = true;
  29. template <bool thread_safe>
  30. struct SlotSpanMetadata;
  31. #if (BUILDFLAG(PA_DCHECK_IS_ON) || \
  32. BUILDFLAG(ENABLE_BACKUP_REF_PTR_SLOW_CHECKS)) && \
  33. BUILDFLAG(USE_BACKUP_REF_PTR)
  34. PA_COMPONENT_EXPORT(PARTITION_ALLOC)
  35. void CheckThatSlotOffsetIsZero(uintptr_t address);
  36. #endif
  37. } // namespace internal
  38. class PartitionStatsDumper;
  39. template <bool thread_safe = internal::ThreadSafe>
  40. struct PartitionRoot;
  41. using ThreadSafePartitionRoot = PartitionRoot<internal::ThreadSafe>;
  42. } // namespace partition_alloc
  43. // From https://clang.llvm.org/docs/AttributeReference.html#malloc:
  44. //
  45. // The malloc attribute indicates that the function acts like a system memory
  46. // allocation function, returning a pointer to allocated storage disjoint from
  47. // the storage for any other object accessible to the caller.
  48. //
  49. // Note that it doesn't apply to realloc()-type functions, as they can return
  50. // the same pointer as the one passed as a parameter, as noted in e.g. stdlib.h
  51. // on Linux systems.
  52. #if PA_HAS_ATTRIBUTE(malloc)
  53. #define PA_MALLOC_FN __attribute__((malloc))
  54. #endif
  55. // Allows the compiler to assume that the return value is aligned on a
  56. // kAlignment boundary. This is useful for e.g. using aligned vector
  57. // instructions in the constructor for zeroing.
  58. #if PA_HAS_ATTRIBUTE(assume_aligned)
  59. #define PA_MALLOC_ALIGNED \
  60. __attribute__((assume_aligned(::partition_alloc::internal::kAlignment)))
  61. #endif
  62. #if !defined(PA_MALLOC_FN)
  63. #define PA_MALLOC_FN
  64. #endif
  65. #if !defined(PA_MALLOC_ALIGNED)
  66. #define PA_MALLOC_ALIGNED
  67. #endif
  68. #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_FORWARD_H_