1 /*
2  * Copyright (c) 2023 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 BASE_STARTUP_PARAM_BASE_H
17 #define BASE_STARTUP_PARAM_BASE_H
18 
19 #include "param_osadp.h"
20 #include "param_trie.h"
21 
22 #ifndef PARAM_BASE
23 #include "securec.h"
24 #endif
25 
26 #ifdef __cplusplus
27 #if __cplusplus
28 extern "C" {
29 #endif
30 #endif
31 
32 #define WORKSPACE_STATUS_IN_PROCESS    0x01
33 #define WORKSPACE_STATUS_VALID       0x02
34 
35 #define READ_COMMIT_ID_LOOP_RETRY_TIME_WARNING  50
36 #ifndef PARAM_BASE
37 #define PARAM_SPRINTF(buffer, buffSize, format, ...) \
38     snprintf_s((buffer), (buffSize), (buffSize) - 1, (format), ##__VA_ARGS__)
39 #define PARAM_MEMCPY(dest, destMax, src, count)  memcpy_s((dest), (destMax), (src), (count))
40 #define PARAM_STRCPY(strDest, destMax, strSrc)   strcpy_s((strDest), (destMax), (strSrc))
41 #else
42 #define PARAM_SPRINTF(buffer, buffSize, format, ...) snprintf((buffer), (buffSize), (format), ##__VA_ARGS__)
43 #define PARAM_MEMCPY(dest, destMax, src, count) (memcpy((dest), (src), (count)) != NULL) ? 0 : 1
44 #define PARAM_STRCPY(strDest, destMax, strSrc) (strcpy((strDest), (strSrc)) != NULL) ? 0 : 1
45 #endif
46 
ReadCommitId(ParamNode * entry)47 static inline uint32_t ReadCommitId(ParamNode *entry)
48 {
49     uint32_t commitId = ATOMIC_LOAD_EXPLICIT(&entry->commitId, MEMORY_ORDER_ACQUIRE);
50     uint32_t retryTimes = 0;
51     while (commitId & PARAM_FLAGS_MODIFY) {
52         futex_wait(&entry->commitId, commitId);
53         commitId = ATOMIC_LOAD_EXPLICIT(&entry->commitId, MEMORY_ORDER_ACQUIRE);
54         if (retryTimes++ % READ_COMMIT_ID_LOOP_RETRY_TIME_WARNING == 0) {
55             PARAM_LOGW("ReadCommitId warning, too many retry times, %{public}d", retryTimes);
56         }
57     }
58     return commitId & PARAM_FLAGS_COMMITID;
59 }
60 
ReadParamValue_(ParamNode * entry,uint32_t * commitId,char * value,uint32_t * length)61 static inline int ReadParamValue_(ParamNode *entry, uint32_t *commitId, char *value, uint32_t *length)
62 {
63     uint32_t id = *commitId;
64     do {
65         *commitId = id;
66         int ret = PARAM_MEMCPY(value, *length, entry->data + entry->keyLength + 1, entry->valueLength);
67         PARAM_CHECK(ret == 0, return -1, "Failed to copy value");
68         value[entry->valueLength] = '\0';
69         *length = entry->valueLength;
70         id = ReadCommitId(entry);
71     } while (*commitId != id); // if change,must read
72     return 0;
73 }
74 
75 #ifdef __cplusplus
76 #if __cplusplus
77 }
78 #endif
79 #endif
80 #endif // BASE_STARTUP_PARAM_BASE_H