Motr  M0
str.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 #ifndef __KERNEL__
24 #include <string.h> /* strlen */
25 #else
26 #include <linux/string.h> /* strlen */
27 #endif
28 
29 #include "lib/memory.h" /* m0_alloc */
30 #include "lib/misc.h" /* ergo, ARRAY_SIZE */
31 
32 #include "motr/magic.h" /* M0_NET_TEST_STR_MAGIC */
33 
34 #include "net/test/str.h"
35 
48  uint64_t ntsl_magic;
49  size_t ntsl_len;
50 };
51 
53  FIELD_DESCR(struct net_test_str_len, ntsl_magic),
54  FIELD_DESCR(struct net_test_str_len, ntsl_len),
55 };
56 
58  char **str,
59  struct m0_bufvec *bv,
60  m0_bcount_t bv_offset)
61 {
62  struct net_test_str_len str_len;
63  m0_bcount_t len;
64  m0_bcount_t len_total;
65 
67  M0_PRE(str != NULL);
68  M0_PRE(ergo(op == M0_NET_TEST_SERIALIZE, *str != NULL));
69 
70  if (op == M0_NET_TEST_SERIALIZE) {
71  str_len.ntsl_len = strlen(*str) + 1;
73  }
74  len = m0_net_test_serialize(op, &str_len,
76  bv, bv_offset);
77  len_total = net_test_len_accumulate(0, len);
78  if (len_total != 0) {
79  if (op == M0_NET_TEST_DESERIALIZE) {
80  if (str_len.ntsl_magic != M0_NET_TEST_STR_MAGIC)
81  return 0;
82  *str = m0_alloc(str_len.ntsl_len);
83  if (*str == NULL)
84  return 0;
85  }
86  len = m0_net_test_serialize_data(op, *str, str_len.ntsl_len,
87  true,
88  bv, bv_offset + len_total);
89  len_total = net_test_len_accumulate(len_total, len);
90  };
91 
92  return len_total;
93 }
94 
99 /*
100  * Local variables:
101  * c-indentation-style: "K&R"
102  * c-basic-offset: 8
103  * tab-width: 8
104  * fill-column: 79
105  * scroll-step: 1
106  * End:
107  */
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
size_t ntsl_len
Definition: str.c:49
m0_bcount_t m0_net_test_serialize(enum m0_net_test_serialize_op op, void *obj, const struct m0_net_test_descr descr[], size_t descr_nr, struct m0_bufvec *bv, m0_bcount_t bv_offset)
Definition: serialize.c:176
#define ergo(a, b)
Definition: misc.h:293
uint64_t ntsl_magic
Definition: str.c:48
uint64_t m0_bcount_t
Definition: types.h:77
static m0_bcount_t net_test_len_accumulate(m0_bcount_t accumulator, m0_bcount_t addend)
Definition: serialize.h:122
op
Definition: libdemo.c:64
void * m0_alloc(size_t size)
Definition: memory.c:126
#define FIELD_DESCR(type, field)
Definition: serialize.h:63
m0_bcount_t m0_net_test_str_serialize(enum m0_net_test_serialize_op op, char **str, struct m0_bufvec *bv, m0_bcount_t bv_offset)
Definition: str.c:57
m0_net_test_serialize_op
Definition: serialize.h:42
TYPE_DESCR(net_test_str_len)
m0_bcount_t m0_net_test_serialize_data(enum m0_net_test_serialize_op op, void *data, m0_bcount_t data_len, bool plain_data, struct m0_bufvec *bv, m0_bcount_t bv_offset)
Definition: serialize.c:163
#define USE_TYPE_DESCR(type_name)
Definition: serialize.h:57
Definition: vec.h:145