1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://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,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_ACELITE_CACHE_MANAGER_H
17 #define OHOS_ACELITE_CACHE_MANAGER_H
18 
19 #include "cache_config.h"
20 
21 #include <cstddef>
22 #include <cstdint>
23 #include "non_copyable.h"
24 
25 namespace OHOS {
26 namespace ACELite {
27 /**
28  * The structure used for recording the cache memory detail information.
29  */
30 struct CacheMemInfo {
31     // the cache memory start address, including the magic number at beginning
32     uintptr_t cacheStartAddr;
33     // count in bytes, and the max capability is 4M, including the magic number at the beginning and ending
34     size_t cacheLength;
35 };
36 
37 enum CacheSetupState : uint8_t {
38     STATE_NORMAL, // the cache is not initialized ever, or no user is configured
39     STATE_SUCCESS, // the cache was initialized and distributed to all users successfully
40     STATE_FAILURE, // the cache was tried be initialized but failed to distributed to all users
41 };
42 
43 /**
44  * This is one singleton instance for cache memory management. It can be used
45  * out of the applications' lifecycle. It records the memory area for all kinds
46  * of caching purpose in the frameworks. If the cache memory rang is not setup,
47  * the cache functionality will not work.
48  */
49 class CacheManager final {
50 public:
51     static CacheManager &GetInstance();
52     void SetupCacheMemInfo(uintptr_t startAddr, size_t length);
53     // return the start addrss of the assigned buffer the unit can use, which doesn't include the starting magic number
54     uintptr_t GetCacheBufAddress(CacheUser user) const;
55     // return the total buffer length the unit can use, which doesn't include the starting and ending magic numbers
56     size_t GetCacheBufLength(CacheUser user) const;
57     bool IsCacheAvailable(CacheUser user) const;
58     bool IsCacheOverflow(CacheUser user) const;
59     bool IsWholeCacheHealthy() const;
60     /**
61      * @brief SetConfigTable give the config table from outside, call this function carefully
62      * @param table the config table
63      * @param count the actual user count
64      */
65     void SetConfigTable(const CacheUnit *table, size_t count);
66 
67 private:
68     ACE_DISALLOW_COPY_AND_MOVE(CacheManager);
69     CacheManager();
70     ~CacheManager() = default;
71     /**
72      * @brief ResetDistributedInfo clean up all assigned buffer info
73      */
74     void ResetDistributedInfo();
75     /**
76      * @brief DistributeCacheRange called once to assign proper memory rang to all the user in the config table
77      * @param startAddr the entire cache buffer start address
78      * @param totalBytes the entire cache length
79      * @return the result of the assignment process, if the memory size can not meet the  lowest request, the process
80      * will return false as failure
81      */
82     CacheSetupState DistributeCacheRange(uintptr_t startAddr, size_t totalBytes);
83     CacheSetupState PrecheckStatus(uintptr_t startAddr, size_t totalBytes) const;
84     bool IsEnvReady(CacheUser user) const;
85     CacheMemInfo wholeCacheMemInfo_;
86     CacheMemInfo cacheUnitInfo_[USER_MAX_COUNT]; // count in bytes
87     const CacheUnit *configTable_;
88     size_t configTableLen_;
89     CacheSetupState cacheState_;
90     static const CacheUnit DEFAULT_CONFIG_TABLE[];
91 };
92 } // namespace ACELite
93 } // namespace OHOS
94 
95 #endif // OHOS_ACELITE_CACHE_MANAGER_H
96