Motr  M0
ringbuf.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 #include "lib/errno.h" /* ENOMEM */
24 #include "lib/misc.h" /* M0_SET0 */
25 #include "lib/memory.h" /* M0_ALLOC_ARR */
26 
27 #include "net/test/ringbuf.h"
28 
37 {
38  M0_PRE(rb != NULL);
39  M0_PRE(size != 0);
40 
41  rb->ntr_size = size;
42  m0_atomic64_set(&rb->ntr_start, 0);
43  m0_atomic64_set(&rb->ntr_end, 0);
44  M0_ALLOC_ARR(rb->ntr_buf, rb->ntr_size);
45 
46  if (rb->ntr_buf != NULL)
48 
49  return rb->ntr_buf == NULL ? -ENOMEM : 0;
50 }
51 
53 {
55 
56  m0_free(rb->ntr_buf);
57  M0_SET0(rb);
58 }
59 
61 {
62  int64_t start;
63  int64_t end;
64 
65  if (rb == NULL || rb->ntr_buf == NULL)
66  return false;
67 
68  start = m0_atomic64_get(&rb->ntr_start);
69  end = m0_atomic64_get(&rb->ntr_end);
70  if (start > end)
71  return false;
72  if (end - start > rb->ntr_size)
73  return false;
74  return true;
75 }
76 
78 {
79  int64_t index;
80 
82  index = m0_atomic64_add_return(&rb->ntr_end, 1) - 1;
84 
85  rb->ntr_buf[index % rb->ntr_size] = value;
86 }
87 
89 {
90  int64_t index;
91 
93  index = m0_atomic64_add_return(&rb->ntr_start, 1) - 1;
95 
96  return rb->ntr_buf[index % rb->ntr_size];
97 }
98 
100 {
102 
103  return m0_atomic64_get(&rb->ntr_end) == m0_atomic64_get(&rb->ntr_start);
104 }
105 
107 {
109 
110  return m0_atomic64_get(&rb->ntr_end) - m0_atomic64_get(&rb->ntr_start);
111 }
112 
117 /*
118  * Local variables:
119  * c-indentation-style: "K&R"
120  * c-basic-offset: 8
121  * tab-width: 8
122  * fill-column: 79
123  * scroll-step: 1
124  * End:
125  */
#define M0_PRE(cond)
#define M0_ALLOC_ARR(arr, nr)
Definition: memory.h:84
#define NULL
Definition: misc.h:38
int m0_net_test_ringbuf_init(struct m0_net_test_ringbuf *rb, size_t size)
Definition: ringbuf.c:36
int const char const void * value
Definition: dir.c:325
#define M0_SET0(obj)
Definition: misc.h:64
size_t m0_net_test_ringbuf_pop(struct m0_net_test_ringbuf *rb)
Definition: ringbuf.c:88
struct reqh_ut_balloc rb
Definition: reqh_fom_ut.c:159
void m0_net_test_ringbuf_fini(struct m0_net_test_ringbuf *rb)
Definition: ringbuf.c:52
#define M0_ASSERT(cond)
void m0_net_test_ringbuf_push(struct m0_net_test_ringbuf *rb, size_t value)
Definition: ringbuf.c:77
size_t m0_net_test_ringbuf_nr(struct m0_net_test_ringbuf *rb)
Definition: ringbuf.c:106
static int64_t m0_atomic64_get(const struct m0_atomic64 *a)
m0_bcount_t size
Definition: di.c:39
static int start(struct m0_fom *fom)
Definition: trigger_fom.c:321
bool m0_net_test_ringbuf_is_empty(struct m0_net_test_ringbuf *rb)
Definition: ringbuf.c:99
bool m0_net_test_ringbuf_invariant(const struct m0_net_test_ringbuf *rb)
Definition: ringbuf.c:60
void m0_free(void *data)
Definition: memory.c:146
static int64_t m0_atomic64_add_return(struct m0_atomic64 *a, int64_t d)
static void m0_atomic64_set(struct m0_atomic64 *a, int64_t num)