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 #ifndef PARAM_BASE
36 #define PARAM_SPRINTF(buffer, buffSize, format, ...) \
37 snprintf_s((buffer), (buffSize), (buffSize) - 1, (format), ##__VA_ARGS__)
38 #define PARAM_MEMCPY(dest, destMax, src, count) memcpy_s((dest), (destMax), (src), (count))
39 #define PARAM_STRCPY(strDest, destMax, strSrc) strcpy_s((strDest), (destMax), (strSrc))
40 #else
41 #define PARAM_SPRINTF(buffer, buffSize, format, ...) snprintf((buffer), (buffSize), (format), ##__VA_ARGS__)
42 #define PARAM_MEMCPY(dest, destMax, src, count) (memcpy((dest), (src), (count)) != NULL) ? 0 : 1
43 #define PARAM_STRCPY(strDest, destMax, strSrc) (strcpy((strDest), (strSrc)) != NULL) ? 0 : 1
44 #endif
45
ReadCommitId(ParamNode * entry)46 static inline uint32_t ReadCommitId(ParamNode *entry)
47 {
48 uint32_t commitId = ATOMIC_LOAD_EXPLICIT(&entry->commitId, MEMORY_ORDER_ACQUIRE);
49 uint32_t retryTimes = 0;
50 while (commitId & PARAM_FLAGS_MODIFY) {
51 futex_wait(&entry->commitId, commitId);
52 commitId = ATOMIC_LOAD_EXPLICIT(&entry->commitId, MEMORY_ORDER_ACQUIRE);
53 retryTimes++;
54 }
55 return commitId & PARAM_FLAGS_COMMITID;
56 }
57
ReadParamValue_(ParamNode * entry,uint32_t * commitId,char * value,uint32_t * length)58 static inline int ReadParamValue_(ParamNode *entry, uint32_t *commitId, char *value, uint32_t *length)
59 {
60 uint32_t id = *commitId;
61 do {
62 *commitId = id;
63 int ret = PARAM_MEMCPY(value, *length, entry->data + entry->keyLength + 1, entry->valueLength);
64 PARAM_ONLY_CHECK(ret == 0, return -1);
65 value[entry->valueLength] = '\0';
66 *length = entry->valueLength;
67 id = ReadCommitId(entry);
68 } while (*commitId != id); // if change,must read
69 return 0;
70 }
71
72 #ifdef __cplusplus
73 #if __cplusplus
74 }
75 #endif
76 #endif
77 #endif // BASE_STARTUP_PARAM_BASE_H