Motr  M0
bitstring.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2011-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 #ifdef __KERNEL__
24 #include <linux/string.h>
25 #else
26 #include <string.h>
27 #endif
28 
29 #include "lib/bitstring.h"
30 #include "lib/arith.h" /* M0_3WAY */
31 #include "lib/memory.h" /* m0_alloc() */
32 
33 M0_INTERNAL void *m0_bitstring_buf_get(struct m0_bitstring *c)
34 {
35  return c->b_data;
36 }
37 
38 M0_INTERNAL uint32_t m0_bitstring_len_get(const struct m0_bitstring *c)
39 {
40  return c->b_len;
41 }
42 
43 M0_INTERNAL void m0_bitstring_len_set(struct m0_bitstring *c, uint32_t len)
44 {
45  c->b_len = len;
46 }
47 
48 M0_INTERNAL struct m0_bitstring *m0_bitstring_alloc(const char *name,
49  size_t len)
50 {
51  struct m0_bitstring *c = m0_alloc(sizeof(*c) + len);
52  if (c == NULL)
53  return NULL;
54  m0_bitstring_copy(c, name, len);
55  return c;
56 }
57 
58 M0_INTERNAL void m0_bitstring_free(struct m0_bitstring *c)
59 {
60  m0_free(c);
61 }
62 
63 M0_INTERNAL void m0_bitstring_copy(struct m0_bitstring *dst, const char *src,
64  size_t count)
65 {
66  memcpy(m0_bitstring_buf_get(dst), src, count);
68 }
69 
75 M0_INTERNAL int m0_bitstring_cmp(const struct m0_bitstring *s1,
76  const struct m0_bitstring *s2)
77 {
78  /* Compare leading parts up to min_len. If they differ, return the
79  * result, otherwize compare lengths. */
80  return memcmp(s1->b_data, s2->b_data, min_check(s1->b_len, s2->b_len))
81  ?: M0_3WAY(s1->b_len, s2->b_len);
82 }
83 
84 /*
85  * Local variables:
86  * c-indentation-style: "K&R"
87  * c-basic-offset: 8
88  * tab-width: 8
89  * fill-column: 80
90  * scroll-step: 1
91  * End:
92  */
M0_INTERNAL void m0_bitstring_len_set(struct m0_bitstring *c, uint32_t len)
Definition: bitstring.c:43
#define NULL
Definition: misc.h:38
static struct m0_bufvec dst
Definition: xform.c:61
#define M0_3WAY(v0, v1)
Definition: arith.h:199
char b_data[0]
Definition: bitstring.h:37
#define min_check(a, b)
Definition: arith.h:88
M0_INTERNAL uint32_t m0_bitstring_len_get(const struct m0_bitstring *c)
Definition: bitstring.c:38
static m0_bcount_t count
Definition: xcode.c:167
const char * name
Definition: trace.c:110
M0_INTERNAL void m0_bitstring_copy(struct m0_bitstring *dst, const char *src, size_t count)
Definition: bitstring.c:63
M0_INTERNAL struct m0_bitstring * m0_bitstring_alloc(const char *name, size_t len)
Definition: bitstring.c:48
static struct m0_addb2_callback c
Definition: consumer.c:41
void * m0_alloc(size_t size)
Definition: memory.c:126
M0_INTERNAL int m0_bitstring_cmp(const struct m0_bitstring *s1, const struct m0_bitstring *s2)
Definition: bitstring.c:75
uint32_t b_len
Definition: bitstring.h:36
M0_INTERNAL void m0_bitstring_free(struct m0_bitstring *c)
Definition: bitstring.c:58
M0_INTERNAL void * m0_bitstring_buf_get(struct m0_bitstring *c)
Definition: bitstring.c:33
void m0_free(void *data)
Definition: memory.c:146
struct m0_pdclust_src_addr src
Definition: fd.c:108