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_SHIM_ALLOCATOR_DISPATCH_H_
6 #define PARTITION_ALLOC_SHIM_ALLOCATOR_DISPATCH_H_
7 
8 #include <cstddef>
9 
10 namespace allocator_shim {
11 
12 struct AllocatorDispatch {
13   using AllocFn = void*(const AllocatorDispatch* self,
14                         size_t size,
15                         void* context);
16   using AllocUncheckedFn = void*(const AllocatorDispatch* self,
17                                  size_t size,
18                                  void* context);
19   using AllocZeroInitializedFn = void*(const AllocatorDispatch* self,
20                                        size_t n,
21                                        size_t size,
22                                        void* context);
23   using AllocAlignedFn = void*(const AllocatorDispatch* self,
24                                size_t alignment,
25                                size_t size,
26                                void* context);
27   using ReallocFn = void*(const AllocatorDispatch* self,
28                           void* address,
29                           size_t size,
30                           void* context);
31   using FreeFn = void(const AllocatorDispatch* self,
32                       void* address,
33                       void* context);
34   // Returns the allocated size of user data (not including heap overhead).
35   // Can be larger than the requested size.
36   using GetSizeEstimateFn = size_t(const AllocatorDispatch* self,
37                                    void* address,
38                                    void* context);
39   using GoodSizeFn = size_t(const AllocatorDispatch* self,
40                             size_t size,
41                             void* context);
42   using ClaimedAddressFn = bool(const AllocatorDispatch* self,
43                                 void* address,
44                                 void* context);
45   using BatchMallocFn = unsigned(const AllocatorDispatch* self,
46                                  size_t size,
47                                  void** results,
48                                  unsigned num_requested,
49                                  void* context);
50   using BatchFreeFn = void(const AllocatorDispatch* self,
51                            void** to_be_freed,
52                            unsigned num_to_be_freed,
53                            void* context);
54   using FreeDefiniteSizeFn = void(const AllocatorDispatch* self,
55                                   void* ptr,
56                                   size_t size,
57                                   void* context);
58   using TryFreeDefaultFn = void(const AllocatorDispatch* self,
59                                 void* ptr,
60                                 void* context);
61   using AlignedMallocFn = void*(const AllocatorDispatch* self,
62                                 size_t size,
63                                 size_t alignment,
64                                 void* context);
65   using AlignedReallocFn = void*(const AllocatorDispatch* self,
66                                  void* address,
67                                  size_t size,
68                                  size_t alignment,
69                                  void* context);
70   using AlignedFreeFn = void(const AllocatorDispatch* self,
71                              void* address,
72                              void* context);
73 
74   AllocFn* const alloc_function;
75   AllocUncheckedFn* const alloc_unchecked_function;
76   AllocZeroInitializedFn* const alloc_zero_initialized_function;
77   AllocAlignedFn* const alloc_aligned_function;
78   ReallocFn* const realloc_function;
79   FreeFn* const free_function;
80   GetSizeEstimateFn* const get_size_estimate_function;
81   GoodSizeFn* const good_size_function;
82   // claimed_address, batch_malloc, batch_free, free_definite_size and
83   // try_free_default are specific to the OSX and iOS allocators.
84   ClaimedAddressFn* const claimed_address_function;
85   BatchMallocFn* const batch_malloc_function;
86   BatchFreeFn* const batch_free_function;
87   FreeDefiniteSizeFn* const free_definite_size_function;
88   TryFreeDefaultFn* const try_free_default_function;
89   // _aligned_malloc, _aligned_realloc, and _aligned_free are specific to the
90   // Windows allocator.
91   AlignedMallocFn* const aligned_malloc_function;
92   AlignedReallocFn* const aligned_realloc_function;
93   AlignedFreeFn* const aligned_free_function;
94 
95   const AllocatorDispatch* next;
96 
97   // |default_dispatch| is statically defined by one (and only one) of the
98   // allocator_shim_default_dispatch_to_*.cc files, depending on the build
99   // configuration.
100   static const AllocatorDispatch default_dispatch;
101 };
102 
103 }  // namespace allocator_shim
104 
105 #endif  // PARTITION_ALLOC_SHIM_ALLOCATOR_DISPATCH_H_
106