Motr  M0
refs.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 #include "ut/ut.h"
24 #include "lib/memory.h"
25 #include "lib/refs.h"
26 
27 struct test_struct {
28  struct m0_ref ref;
29 };
30 
31 static int free_done;
32 
33 void test_destructor(struct m0_ref *r)
34 {
35  struct test_struct *t;
36 
37  t = container_of(r, struct test_struct, ref);
38 
39  m0_free(t);
40  free_done = 1;
41 }
42 
43 void test_refs(void)
44 {
45  struct test_struct *t;
46 
47  t = m0_alloc(sizeof(struct test_struct));
48  M0_UT_ASSERT(t != NULL);
49 
50  free_done = 0;
51  m0_ref_init(&t->ref, 1, test_destructor);
52 
53  M0_UT_ASSERT(m0_ref_read(&t->ref) == 1);
54  m0_ref_get(&t->ref);
55  M0_UT_ASSERT(m0_ref_read(&t->ref) == 2);
56  m0_ref_put(&t->ref);
57  M0_UT_ASSERT(m0_ref_read(&t->ref) == 1);
58  m0_ref_put(&t->ref);
59 
61 }
#define NULL
Definition: misc.h:38
void test_refs(void)
Definition: refs.c:43
#define container_of(ptr, type, member)
Definition: misc.h:33
M0_INTERNAL void m0_ref_put(struct m0_ref *ref)
Definition: refs.c:38
void m0_ref_init(struct m0_ref *ref, int init_num, void(*release)(struct m0_ref *ref))
Definition: refs.c:24
M0_INTERNAL void m0_ref_get(struct m0_ref *ref)
Definition: refs.c:32
Definition: refs.h:34
void test_destructor(struct m0_ref *r)
Definition: refs.c:33
static struct m0_thread t[8]
Definition: service_ut.c:1230
void * m0_alloc(size_t size)
Definition: memory.c:126
static int free_done
Definition: refs.c:31
M0_INTERNAL int64_t m0_ref_read(const struct m0_ref *ref)
Definition: refs.c:44
static int r[NR]
Definition: thread.c:46
struct m0_ref ref
Definition: refs.c:28
void m0_free(void *data)
Definition: memory.c:146
#define M0_UT_ASSERT(a)
Definition: ut.h:46