1 // Copyright 2021 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 15 // Configuration macros for the function module. 16 #pragma once 17 18 #include <cstddef> 19 20 #include "lib/fit/function.h" 21 22 // The maximum size of a callable that can be inlined within a function. 23 // Callables larger than this are stored externally to the function (if dynamic 24 // allocation is enabled). 25 // 26 // This defaults to the size of 1 pointer, which is capable of storing common 27 // callables such as function pointers and lambdas with a single pointer's size 28 // of captured data. 29 #ifndef PW_FUNCTION_INLINE_CALLABLE_SIZE 30 #define PW_FUNCTION_INLINE_CALLABLE_SIZE (sizeof(void*)) 31 #endif // PW_FUNCTION_INLINE_CALLABLE_SIZE 32 33 static_assert(PW_FUNCTION_INLINE_CALLABLE_SIZE > 0 && 34 PW_FUNCTION_INLINE_CALLABLE_SIZE % alignof(void*) == 0); 35 36 // Whether functions should allocate memory dynamically if a callable is larger 37 // than the inline size. 38 // 39 // Enabling this allows functions to support callables larger than 40 // `PW_FUNCTION_INLINE_CALLABLE_SIZE` by dynamically allocating storage space 41 // for them. The Allocator type used can be provided as a template argument, and 42 // the default type can be specified by overriding 43 // `PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE`. The Allocator must satisfy the C++ 44 // named requirements for Allocator: 45 // https://en.cppreference.com/w/cpp/named_req/Allocator 46 #ifndef PW_FUNCTION_ENABLE_DYNAMIC_ALLOCATION 47 #define PW_FUNCTION_ENABLE_DYNAMIC_ALLOCATION 0 48 #endif // PW_FUNCTION_ENABLE_DYNAMIC_ALLOCATION 49 50 // The default Allocator used to dynamically allocate the callable, if dynamic 51 // allocation is enabled. Its `value_type` is irrelevant, since it must support 52 // rebinding. 53 // 54 // This definition is useful to ensure that a project can specify an Allocator 55 // that will be used even by Pigweed source code. 56 #ifndef PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE 57 #define PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE fit::default_callable_allocator 58 #endif // PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE 59 60 namespace pw::function_internal::config { 61 62 inline constexpr size_t kInlineCallableSize = PW_FUNCTION_INLINE_CALLABLE_SIZE; 63 inline constexpr bool kEnableDynamicAllocation = 64 PW_FUNCTION_ENABLE_DYNAMIC_ALLOCATION; 65 66 } // namespace pw::function_internal::config 67