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 while (commitId & PARAM_FLAGS_MODIFY) {
50 futex_wait(&entry->commitId, commitId);
51 commitId = ATOMIC_LOAD_EXPLICIT(&entry->commitId, MEMORY_ORDER_ACQUIRE);
52 }
53 return commitId & PARAM_FLAGS_COMMITID;
54 }
55
ReadParamValue_(ParamNode * entry,uint32_t * commitId,char * value,uint32_t * length)56 static inline int ReadParamValue_(ParamNode *entry, uint32_t *commitId, char *value, uint32_t *length)
57 {
58 uint32_t id = *commitId;
59 do {
60 *commitId = id;
61 int ret = PARAM_MEMCPY(value, *length, entry->data + entry->keyLength + 1, entry->valueLength);
62 PARAM_CHECK(ret == 0, return -1, "Failed to copy value");
63 value[entry->valueLength] = '\0';
64 *length = entry->valueLength;
65 id = ReadCommitId(entry);
66 } while (*commitId != id); // if change,must read
67 return 0;
68 }
69
70 #ifdef __cplusplus
71 #if __cplusplus
72 }
73 #endif
74 #endif
75 #endif // BASE_STARTUP_PARAM_BASE_H