blob: 7534607fbd27ff9cd93b692ad6f22868f581781f [file] [log] [blame]
Atneya3c61d882021-09-20 14:52:15 -04001/*
2**
3** Copyright 2022, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include <mediautils/SharedMemoryAllocator.h>
19
20#pragma once
21
22// TODO how do we appropriately restrict visibility of this header?
23// It should only be included in AudioFlinger.h
24// We will make everything internal linkage for now.
25namespace android {
26namespace AllocatorFactory {
27namespace {
28// TODO make sure these are appropriate
29constexpr inline size_t MAX_MEMORY_SIZE = 1024 * 1024 * 100; // 100 MiB
30constexpr inline size_t DED_SIZE = (MAX_MEMORY_SIZE * 4) / 10; // 40 MiB
31constexpr inline size_t SHARED_SIZE = MAX_MEMORY_SIZE - DED_SIZE; // 60 MiB
32constexpr inline size_t SHARED_SIZE_LARGE = (SHARED_SIZE * 4) / 6; // 40 MiB
33constexpr inline size_t SHARED_SIZE_SMALL = SHARED_SIZE - SHARED_SIZE_LARGE; // 20 MiB
34constexpr inline size_t SMALL_THRESHOLD = 1024 * 40; // 40 KiB
35
36inline auto getDedicated() {
37 using namespace mediautils;
38 static const auto allocator =
39 std::make_shared<PolicyAllocator<MemoryHeapBaseAllocator, SizePolicy<DED_SIZE>>>();
40 return allocator;
41}
42
43inline auto getSharedLarge() {
44 using namespace mediautils;
45 static const auto allocator = std::make_shared<
46 PolicyAllocator<MemoryHeapBaseAllocator, SizePolicy<SHARED_SIZE_LARGE>>>();
47 return allocator;
48}
49
50inline auto getSharedSmall() {
51 using namespace mediautils;
52 static const auto allocator =
53 std::make_shared<PolicyAllocator<MemoryHeapBaseAllocator,
54 SizePolicy<SHARED_SIZE_SMALL, 0, SMALL_THRESHOLD>>>();
55 return allocator;
56}
57
58template <typename Policy, typename Allocator>
59inline auto wrapWithPolicySnooping(Allocator allocator, std::string_view name) {
60 using namespace mediautils;
61 return SnoopingAllocator{PolicyAllocator{IndirectAllocator{allocator}, Policy{}}, name};
62}
63
64// A reasonable upper bound on how many clients we expect, and how many pieces to slice
65// the dedicate pool.
66constexpr inline size_t CLIENT_BOUND = 32;
67// Maximum amount of shared pools a single client can take (50%).
68constexpr inline size_t ADV_THRESHOLD_INV = 2;
69
70inline auto getClientAllocator() {
71 using namespace mediautils;
72 const auto makeDedPool = []() {
73 return wrapWithPolicySnooping<SizePolicy<DED_SIZE / CLIENT_BOUND>>(getDedicated(),
74 "Dedicated Pool");
75 };
76 const auto makeLargeShared = []() {
77 return wrapWithPolicySnooping<SizePolicy<SHARED_SIZE_LARGE / ADV_THRESHOLD_INV>>(
78 getSharedLarge(), "Large Shared");
79 };
80 const auto makeSmallShared = []() {
81 return wrapWithPolicySnooping<
82 SizePolicy<SHARED_SIZE_SMALL / ADV_THRESHOLD_INV>>(
83 getSharedSmall(), "Small Shared");
84 };
85
86 return ScopedAllocator{std::make_shared<
87 FallbackAllocator<decltype(makeDedPool()),
88 decltype(FallbackAllocator(makeLargeShared(), makeSmallShared()))>>(
89 makeDedPool(), FallbackAllocator{makeLargeShared(), makeSmallShared()})};
90}
91
92using ClientAllocator = decltype(getClientAllocator());
93} // namespace
94} // namespace AllocatorFactory
95} // namespace android