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 20 #ifndef PW_MALLOC_LOCK_TYPE 21 /// Sets the type of synchronization primitive to use to mediate concurrent 22 /// allocations by the system allocator. 23 /// 24 /// Defaults to `pw::allocator::NoSync`, which does no locking. 25 #include "pw_allocator/synchronized_allocator.h" 26 #define PW_MALLOC_LOCK_TYPE ::pw::allocator::NoSync 27 #endif // PW_MALLOC_LOCK_TYPE 28 29 #ifndef PW_MALLOC_METRICS_TYPE 30 /// Sets the type of allocator metrics collected by the system allocator. 31 /// 32 /// Defaults to `pw::allocator::NoMetrics`, which does no tracking. 33 #include "pw_allocator/tracking_allocator.h" 34 #define PW_MALLOC_METRICS_TYPE ::pw::allocator::NoMetrics 35 36 #elif defined(PW_MALLOC_METRICS_INCLUDE) 37 38 /// Sets the header that can be included to define the `PW_MALLOC_METRICS_TYPE`. 39 #include PW_MALLOC_METRICS_INCLUDE 40 41 #endif // PW_MALLOC_METRICS_TYPE 42 43 #ifndef PW_MALLOC_BLOCK_OFFSET_TYPE 44 /// Sets the unsigned integer type used by `pw::allocator::BlockAllocator`s to 45 /// index blocks. 46 /// 47 /// Larger types allow addressing more memory, but increase allocation overhead 48 /// from block metadata. 49 /// 50 /// Defaults to platform's `uintptr_t` type. 51 #define PW_MALLOC_BLOCK_OFFSET_TYPE uintptr_t 52 #endif // PW_MALLOC_BLOCK_OFFSET_TYPE 53 54 #ifndef PW_MALLOC_MIN_BUCKET_SIZE 55 /// Sets the size of the smallest ``pw::allocator::Bucket` used by an allocator. 56 /// 57 /// See also `pw::allocator::BucketBlockAllocator` and 58 /// `pw::allocator::BuddyAllocator`. 59 /// 60 /// Must be a power of two. Defaults to 32. 61 #define PW_MALLOC_MIN_BUCKET_SIZE 32 62 #endif // PW_MALLOC_MIN_BUCKET_SIZE 63 static_assert(((PW_MALLOC_MIN_BUCKET_SIZE - 1) & PW_MALLOC_MIN_BUCKET_SIZE) == 64 0, 65 "PW_MALLOC_MIN_BUCKET_SIZE must be a power of two"); 66 67 #ifndef PW_MALLOC_NUM_BUCKETS 68 /// Sets the number of ``pw::allocator::Bucket` used by an allocator. 69 /// 70 /// See also `pw::allocator::BucketBlockAllocator` and 71 /// `pw::allocator::BuddyAllocator`. 72 /// 73 /// Defaults to 5. 74 #define PW_MALLOC_NUM_BUCKETS 5 75 #endif // PW_MALLOC_NUM_BUCKETS 76 77 #ifndef PW_MALLOC_DUAL_FIRST_FIT_THRESHOLD 78 /// Sets the threshold beyond which a 79 /// `pw::allocator::DualFirstFitBlockAllocator` considers allocations large. 80 /// 81 /// See also `pw::allocator::DualFirstFitBlockAllocator`. 82 /// 83 /// Defaults to 2KiB. 84 #define PW_MALLOC_DUAL_FIRST_FIT_THRESHOLD 2048 85 #endif // PW_MALLOC_DUAL_FIRST_FIT_THRESHOLD 86