1 /*
2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #include "los_config.h"
31 #include "los_debug.h"
32 #include "stdlib.h"
33 #include "limits.h"
34 #include "key_cache.h"
35
36 #define PATH_CACHE_HASH_MASK (LOSCFG_MAX_PATH_CACHE_SIZE - 1)
37 LIST_HEAD g_keyCacheHashEntrys[LOSCFG_MAX_PATH_CACHE_SIZE];
38 #ifdef LOSCFG_DEBUG_VERSION
39 static int g_totalKeyCacheHit = 0;
40 static int g_totalKeyCacheTry = 0;
41 #define TRACE_TRY_CACHE() do { g_totalKeyCacheTry++; } while (0)
42 #define TRACE_HIT_CACHE(kc) do { kc->hit++; g_totalKeyCacheHit++; } while (0)
43
ResetKeyCacheHitInfo(int * hit,int * try)44 void ResetKeyCacheHitInfo(int *hit, int *try)
45 {
46 *hit = g_totalKeyCacheHit;
47 *try = g_totalKeyCacheTry;
48 g_totalKeyCacheHit = 0;
49 g_totalKeyCacheTry = 0;
50 }
51 #else
52 #define TRACE_TRY_CACHE()
53 #define TRACE_HIT_CACHE(kc)
54 #endif
55
KeyCacheInit(void)56 int KeyCacheInit(void)
57 {
58 for (int i = 0; i < LOSCFG_MAX_PATH_CACHE_SIZE; i++) {
59 LOS_ListInit(&g_keyCacheHashEntrys[i]);
60 }
61 return LOS_OK;
62 }
63
KeyCacheDump(void)64 void KeyCacheDump(void)
65 {
66 PRINTK("-------->keyCache dump in\n");
67 for (int i = 0; i < LOSCFG_MAX_PATH_CACHE_SIZE; i++) {
68 struct KeyCache *kc = NULL;
69 LIST_HEAD *nhead = &g_keyCacheHashEntrys[i];
70
71 LOS_DL_LIST_FOR_EACH_ENTRY(kc, nhead, struct KeyCache, hashEntry) {
72 PRINTK(" keyCache dump hash %d item %s %p %d\n", i,
73 kc->name, kc->fbmem, kc->nameLen);
74 }
75 }
76 PRINTK("-------->keyCache dump out\n");
77 }
78
KeyCacheMemoryDump(void)79 void KeyCacheMemoryDump(void)
80 {
81 int keyCacheNum = 0;
82 int nameSum = 0;
83 for (int i = 0; i < LOSCFG_MAX_PATH_CACHE_SIZE; i++) {
84 LIST_HEAD *dhead = &g_keyCacheHashEntrys[i];
85 struct KeyCache *dent = NULL;
86
87 LOS_DL_LIST_FOR_EACH_ENTRY(dent, dhead, struct KeyCache, hashEntry) {
88 keyCacheNum++;
89 nameSum += dent->nameLen;
90 }
91 }
92 PRINTK("keyCache number = %d\n", keyCacheNum);
93 PRINTK("keyCache memory size = %d(B)\n", keyCacheNum * sizeof(struct KeyCache) + nameSum);
94 }
95
NameHash(const char * name,int len)96 static uint32_t NameHash(const char *name, int len)
97 {
98 uint32_t hash;
99 hash = LOS_HashFNV32aBuf(name, len, FNV1_32A_INIT);
100 return hash;
101 }
102
KeyCacheInsert(struct KeyCache * cache,const char * name,int len)103 static void KeyCacheInsert(struct KeyCache *cache, const char* name, int len)
104 {
105 int hash = NameHash(name, len) & PATH_CACHE_HASH_MASK;
106 LOS_ListAdd(&g_keyCacheHashEntrys[hash], &cache->hashEntry);
107 }
108
KeyCacheAlloc(struct fb_mem * fbmem,const char * name,uint8_t len)109 struct KeyCache *KeyCacheAlloc(struct fb_mem *fbmem, const char *name, uint8_t len)
110 {
111 struct KeyCache *kc = NULL;
112 size_t keyCacheSize;
113 int ret;
114
115 if (name == NULL || len > NAME_MAX || fbmem == NULL) {
116 return NULL;
117 }
118 keyCacheSize = sizeof(struct KeyCache) + len + 1;
119 kc = (struct KeyCache*)zalloc(keyCacheSize);
120 if (kc == NULL) {
121 PRINT_ERR("keyCache alloc failed, no memory!\n");
122 return NULL;
123 }
124
125 ret = strncpy_s(kc->name, len + 1, name, len);
126 if (ret != LOS_OK) {
127 return NULL;
128 }
129
130 kc->nameLen = len;
131 kc->fbmem = fbmem;
132
133 KeyCacheInsert(kc, name, len);
134
135 return kc;
136 }
137
KeyCacheFree(struct KeyCache * kc)138 int KeyCacheFree(struct KeyCache *kc)
139 {
140 if (kc == NULL) {
141 PRINT_ERR("keyCache free: invalid keyCache\n");
142 return -ENOENT;
143 }
144 LOS_ListDelete(&kc->hashEntry);
145 free(kc);
146 return LOS_OK;
147 }
148
KeyCacheLookup(const char * name,int len,struct fb_mem ** fbmem)149 int KeyCacheLookup(const char *name, int len, struct fb_mem **fbmem)
150 {
151 struct KeyCache *kc = NULL;
152 int hash = NameHash(name, len) & PATH_CACHE_HASH_MASK;
153 LIST_HEAD *dhead = &g_keyCacheHashEntrys[hash];
154
155 TRACE_TRY_CACHE();
156 LOS_DL_LIST_FOR_EACH_ENTRY(kc, dhead, struct KeyCache, hashEntry) {
157 if (kc->nameLen == len && !strncmp(kc->name, name, len)) {
158 *fbmem = kc->fbmem;
159 TRACE_HIT_CACHE(kc);
160 return LOS_OK;
161 }
162 }
163 return -ENOENT;
164 }
165
FbMemKeyCacheFree(struct fb_mem * fbmem)166 void FbMemKeyCacheFree(struct fb_mem *fbmem) {
167 for (int i = 0; i < LOSCFG_MAX_PATH_CACHE_SIZE; i++) {
168 struct KeyCache *kc = NULL;
169 LIST_HEAD *nhead = &g_keyCacheHashEntrys[i];
170
171 LOS_DL_LIST_FOR_EACH_ENTRY(kc, nhead, struct KeyCache, hashEntry) {
172 if (kc->fbmem == fbmem) {
173 KeyCacheFree(kc);
174 return;
175 }
176 }
177 }
178 }
179
GetKeyCacheList()180 LIST_HEAD* GetKeyCacheList()
181 {
182 return g_keyCacheHashEntrys;
183 }
184