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