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