• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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.
25 namespace android {
26 namespace AllocatorFactory {
27 namespace {
28 // TODO make sure these are appropriate
29 constexpr inline size_t MAX_MEMORY_SIZE = 1024 * 1024 * 100;                  // 100 MiB
30 constexpr inline size_t DED_SIZE = (MAX_MEMORY_SIZE * 4) / 10;                // 40 MiB
31 constexpr inline size_t SHARED_SIZE = MAX_MEMORY_SIZE - DED_SIZE;             // 60 MiB
32 constexpr inline size_t SHARED_SIZE_LARGE = (SHARED_SIZE * 4) / 6;            // 40 MiB
33 constexpr inline size_t SHARED_SIZE_SMALL = SHARED_SIZE - SHARED_SIZE_LARGE;  // 20 MiB
34 constexpr inline size_t SMALL_THRESHOLD = 1024 * 40;                          // 40 KiB
35 
getDedicated()36 inline 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 
getSharedLarge()43 inline 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 
getSharedSmall()50 inline 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 
58 template <typename Policy, typename Allocator>
wrapWithPolicySnooping(Allocator allocator,std::string_view name)59 inline 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.
66 constexpr inline size_t CLIENT_BOUND = 32;
67 // Maximum amount of shared pools a single client can take (50%).
68 constexpr inline size_t ADV_THRESHOLD_INV = 2;
69 
getClientAllocator()70 inline 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 
92 using ClientAllocator = decltype(getClientAllocator());
93 }  // namespace
94 }  // namespace AllocatorFactory
95 }  // namespace android
96