• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #include <cstdint>
17 
18 #include "pw_allocator/metrics.h"
19 #include "pw_allocator/synchronized_allocator.h"
20 #include "pw_allocator/tracking_allocator.h"
21 
22 #ifndef PW_MALLOC_LOCK_TYPE
23 /// Sets the type of synchronization primitive to use to mediate concurrent
24 /// allocations by the system allocator.
25 ///
26 /// Defaults to `pw::allocator::NoSync`, which does no locking.
27 #define PW_MALLOC_LOCK_TYPE ::pw::allocator::NoSync
28 #endif  // PW_MALLOC_LOCK_TYPE
29 
30 #ifndef PW_MALLOC_METRICS_TYPE
31 /// Sets the type of allocator metrics collected by the system allocator.
32 ///
33 /// Defaults to `pw::allocator::NoMetrics`, which does no tracking.
34 #define PW_MALLOC_METRICS_TYPE ::pw::allocator::NoMetrics
35 #endif  // PW_MALLOC_METRICS_TYPE
36 
37 #ifndef PW_MALLOC_BLOCK_OFFSET_TYPE
38 /// Sets the unsigned integer type used by `pw::allocator::BlockAllocator`s to
39 /// index blocks.
40 ///
41 /// Larger types allow addressing more memory, but increase allocation overhead
42 /// from block metadata.
43 ///
44 /// Defaults to platform's `uintptr_t` type.
45 #define PW_MALLOC_BLOCK_OFFSET_TYPE uintptr_t
46 #endif  // PW_MALLOC_BLOCK_OFFSET_TYPE
47 
48 #ifndef PW_MALLOC_BLOCK_POISON_INTERVAL
49 /// Sets how frequently `pw::allocator::BlockAllocator`s poison free blocks.
50 ///
51 /// Poisoned free blocks are checked on allocation to ensure nothing has
52 /// modified their usable space while deallocated. Setting this value to a
53 /// nonzero value N while poison every N-th free block.
54 ///
55 /// Defaults to 0, which disables poisoning.
56 #define PW_MALLOC_BLOCK_POISON_INTERVAL 0
57 #endif  // PW_MALLOC_BLOCK_POISON_INTERVAL
58 
59 #ifndef PW_MALLOC_BLOCK_ALIGNMENT
60 /// Sets the minimum alignment for a `pw::allocator::BlockAllocator`s memory.
61 ///
62 /// Must be a power of two.
63 ///
64 /// Defaults to the block offset type's alignment, which is the smallest value
65 /// that has any effect on the block allocator.
66 #define PW_MALLOC_BLOCK_ALIGNMENT alignof(PW_MALLOC_BLOCK_OFFSET_TYPE)
67 #endif  // PW_MALLOC_BLOCK_ALIGNMENT
68 static_assert(((PW_MALLOC_BLOCK_ALIGNMENT - 1) & PW_MALLOC_BLOCK_ALIGNMENT) ==
69                   0,
70               "PW_MALLOC_BLOCK_ALIGNMENT must be a power of two");
71 
72 #ifndef PW_MALLOC_MIN_BUCKET_SIZE
73 /// Sets the size of the smallest ``pw::allocator::Bucket` used by an allocator.
74 ///
75 /// See also `pw::allocator::BucketBlockAllocator` and
76 /// `pw::allocator::BuddyAllocator`.
77 ///
78 /// Must be a power of two. Defaults to 32.
79 #define PW_MALLOC_MIN_BUCKET_SIZE 32
80 #endif  // PW_MALLOC_MIN_BUCKET_SIZE
81 static_assert(((PW_MALLOC_MIN_BUCKET_SIZE - 1) & PW_MALLOC_MIN_BUCKET_SIZE) ==
82                   0,
83               "PW_MALLOC_MIN_BUCKET_SIZE must be a power of two");
84 
85 #ifndef PW_MALLOC_NUM_BUCKETS
86 /// Sets the number of ``pw::allocator::Bucket` used by an allocator.
87 ///
88 /// See also `pw::allocator::BucketBlockAllocator` and
89 /// `pw::allocator::BuddyAllocator`.
90 ///
91 /// Defaults to 5.
92 #define PW_MALLOC_NUM_BUCKETS 5
93 #endif  // PW_MALLOC_NUM_BUCKETS
94 
95 #ifndef PW_MALLOC_DUAL_FIRST_FIT_THRESHOLD
96 /// Sets the threshold beyond which a
97 /// `pw::allocator::DualFirstFitBlockAllocator` considers allocations large.
98 ///
99 /// See also `pw::allocator::DualFirstFitBlockAllocator`.
100 ///
101 /// Defaults to 2KiB.
102 #define PW_MALLOC_DUAL_FIRST_FIT_THRESHOLD 2048
103 #endif  // PW_MALLOC_DUAL_FIRST_FIT_THRESHOLD
104