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
31 #ifndef _Key_CACHE_H
32 #define _Key_CACHE_H
33
34 #include "los_compiler.h"
35 #include "los_list.h"
36 #include "fb_mem.h"
37
38 #define FNV1_32A_INIT ((UINT32)0x811c9dc5)
39 /*
40 * 32 bit magic FNV-1 prime
41 */
42 #define FNV_32_PRIME ((UINT32)0x01000193)
43
LOS_HashFNV32aBuf(const VOID * buf,size_t len,UINT32 hval)44 LITE_OS_SEC_ALW_INLINE STATIC INLINE UINT32 LOS_HashFNV32aBuf(const VOID *buf, size_t len, UINT32 hval)
45 {
46 const UINT8 *hashbuf = (const UINT8 *)buf;
47
48 /*
49 * FNV-1a hash each octet in the buffer
50 */
51 while (len-- != 0) {
52 /* xor the bottom with the current octet */
53 hval ^= (UINT32)*hashbuf++;
54
55 /* multiply by the 32 bit FNV magic prime mod 2^32 */
56 hval *= FNV_32_PRIME;
57 }
58
59 /* return our new hash value */
60 return hval;
61 }
62
LOS_HashFNV32aStr(CHAR * str,UINT32 hval)63 LITE_OS_SEC_ALW_INLINE STATIC INLINE UINT32 LOS_HashFNV32aStr(CHAR *str, UINT32 hval)
64 {
65 UINT8 *s = (UINT8 *)str;
66
67 /*
68 * FNV-1a hash each octet in the buffer
69 */
70 while (*s) {
71 /* xor the bottom with the current octet */
72 hval ^= (UINT32)*s++;
73
74 /* multiply by the 32 bit FNV magic prime mod 2^32 */
75 hval *= FNV_32_PRIME;
76 }
77
78 /* return our new hash value */
79 return hval;
80 }
81
82 typedef LOS_DL_LIST LIST_HEAD;
83 typedef LOS_DL_LIST LIST_ENTRY;
84 #define LOSCFG_MAX_PATH_CACHE_SIZE 512
85
86 struct KeyCache {
87 struct fb_mem *fbmem; /* vnode points to the cache */
88 LIST_ENTRY hashEntry; /* list entry for buckets in the hash table */
89 uint8_t nameLen; /* length of Key component */
90 #ifdef LOSCFG_DEBUG_VERSION
91 int hit; /* cache hit count */
92 #endif
93 char name[0]; /* Key component name */
94 };
95
96 int KeyCacheInit(void);
97 int KeyCacheFree(struct KeyCache *cache);
98 struct KeyCache *KeyCacheAlloc(struct fb_mem *fbmem, const char *name, uint8_t len);
99 int KeyCacheLookup(const char *name, int len, struct fb_mem **fbmem);
100 void FbMemKeyCacheFree(struct fb_mem *fbmem);
101 void KeyCacheMemoryDump(void);
102 void KeyCacheDump(void);
103 LIST_HEAD* GetKeyCacheList(void);
104 #ifdef LOSCFG_DEBUG_VERSION
105 void ResetKeyCacheHitInfo(int *hit, int *try);
106 #endif
107
108 #endif /* _Key_CACHE_H */
109