Motr  M0
preload.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_CONF
24 #include "lib/trace.h"
25 
26 #include "conf/preload.h"
27 #include "conf/onwire.h" /* m0_confx */
28 #include "conf/onwire_xc.h" /* m0_confx_xc */
29 #include "xcode/xcode.h"
30 #include "lib/memory.h" /* M0_ALLOC_PTR */
31 #include "lib/errno.h" /* ENOMEM */
32 
33 M0_INTERNAL void m0_confx_free(struct m0_confx *enc)
34 {
35  M0_ENTRY();
36  if (enc != NULL)
37  m0_xcode_free_obj(&M0_XCODE_OBJ(m0_confx_xc, enc));
38  M0_LEAVE();
39 }
40 
41 M0_INTERNAL int m0_confstr_parse(const char *str, struct m0_confx **out)
42 {
43  int rc;
44 
45  M0_ENTRY();
46 
47  M0_ALLOC_PTR(*out);
48  if (*out == NULL)
49  return M0_ERR(-ENOMEM);
50 
51  rc = m0_xcode_read(&M0_XCODE_OBJ(m0_confx_xc, *out), str);
52  if (rc != 0) {
53  M0_LOG(M0_WARN, "Cannot parse configuration string:\n%s", str);
55  *out = NULL;
56  }
57  return M0_RC(rc);
58 }
59 
60 M0_INTERNAL int m0_confx_to_string(struct m0_confx *confx, char **out)
61 {
63  int rc;
64 
65  M0_ENTRY();
66 
67  size = m0_xcode_print(&M0_XCODE_OBJ(m0_confx_xc, confx), NULL, 0) + 1;
68  /*
69  * Spiel sends conf string over bulk transport. Bulk buffers
70  * have to be aligned.
71  */
73  if (*out == NULL)
74  return M0_ERR_INFO(-ENOMEM, "failed to allocate internal buffer"
75  " for encoded Spiel conf data");
76  /* Convert */
77  rc = m0_xcode_print(&M0_XCODE_OBJ(m0_confx_xc, confx),
78  *out, size) <= size ? 0 : M0_ERR(-ENOMEM);
79  if (rc != 0) {
81  *out = NULL;
82  }
83  return M0_RC(rc);
84 }
85 
86 M0_INTERNAL void m0_confx_string_free(char *str)
87 {
89  m0_free_aligned(str, strlen(str)+1, m0_pageshift_get());
90 }
91 
92 #undef M0_TRACE_SUBSYSTEM
M0_INTERNAL int m0_xcode_print(const struct m0_xcode_obj *obj, char *str, int nr)
Definition: string.c:278
#define M0_PRE(cond)
M0_INTERNAL void m0_confx_free(struct m0_confx *enc)
Definition: preload.c:33
#define NULL
Definition: misc.h:38
static bool m0_addr_is_aligned(const void *addr, unsigned shift)
Definition: memory.h:107
#define M0_LOG(level,...)
Definition: trace.h:167
M0_LEAVE()
M0_INTERNAL int m0_xcode_read(struct m0_xcode_obj *obj, const char *str)
Definition: string.c:162
M0_INTERNAL void m0_confx_string_free(char *str)
Definition: preload.c:86
M0_INTERNAL void m0_free_aligned(void *data, size_t size, unsigned shift)
Definition: memory.c:192
M0_INTERNAL void m0_xcode_free_obj(struct m0_xcode_obj *obj)
Definition: xcode.c:248
uint64_t m0_bcount_t
Definition: types.h:77
M0_INTERNAL int m0_pageshift_get(void)
Definition: memory.c:238
return M0_RC(rc)
#define M0_ENTRY(...)
Definition: trace.h:170
#define M0_ERR_INFO(rc, fmt,...)
Definition: trace.h:215
return M0_ERR(-EOPNOTSUPP)
M0_INTERNAL int m0_confx_to_string(struct m0_confx *confx, char **out)
Definition: preload.c:60
M0_INTERNAL int m0_confstr_parse(const char *str, struct m0_confx **out)
Definition: preload.c:41
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
m0_bcount_t size
Definition: di.c:39
#define M0_XCODE_OBJ(type, ptr)
Definition: xcode.h:962
#define out(...)
Definition: gen.c:41
M0_INTERNAL void * m0_alloc_aligned(size_t size, unsigned shift)
Definition: memory.c:168
int32_t rc
Definition: trigger_fop.h:47
Definition: trace.h:478