Motr  M0
lockers.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/lockers.h" /* m0_lockers */
24 #include "lib/types.h" /* uint32_t */
25 #include "lib/string.h" /* memset */
26 #include "lib/assert.h" /* M0_PRE */
27 #include "lib/misc.h" /* M0_SET0 */
28 
35 static bool key_is_valid(const struct m0_lockers_type *lt, int key);
36 
37 M0_INTERNAL void m0_lockers_init(const struct m0_lockers_type *lt,
38  struct m0_lockers *lockers)
39 {
40  memset(lockers->loc_slots, 0,
41  lt->lot_max * sizeof lockers->loc_slots[0]);
42 }
43 
44 M0_INTERNAL int m0_lockers_allot(struct m0_lockers_type *lt)
45 {
46  int i;
47 
48  for (i = 0; i < lt->lot_max; ++i) {
49  if (!lt->lot_inuse[i]) {
50  lt->lot_inuse[i] = true;
51  return i;
52  }
53  }
54  M0_IMPOSSIBLE("Lockers table overflow.");
55 }
56 
57 M0_INTERNAL void m0_lockers_free(struct m0_lockers_type *lt, int key)
58 {
59  M0_PRE(key_is_valid(lt, key));
60  lt->lot_inuse[key] = false;
61 }
62 
63 M0_INTERNAL void m0_lockers_set(const struct m0_lockers_type *lt,
64  struct m0_lockers *lockers,
65  uint32_t key,
66  void *data)
67 {
68  M0_PRE(key_is_valid(lt, key));
69  lockers->loc_slots[key] = data;
70 }
71 
72 M0_INTERNAL void *m0_lockers_get(const struct m0_lockers_type *lt,
73  const struct m0_lockers *lockers,
74  uint32_t key)
75 {
76  M0_PRE(key_is_valid(lt, key));
77  return lockers->loc_slots[key];
78 }
79 
80 M0_INTERNAL void m0_lockers_clear(const struct m0_lockers_type *lt,
81  struct m0_lockers *lockers,
82  uint32_t key)
83 {
84  M0_PRE(key_is_valid(lt, key));
85  lockers->loc_slots[key] = NULL;
86 }
87 
88 M0_INTERNAL bool m0_lockers_is_empty(const struct m0_lockers_type *lt,
89  const struct m0_lockers *lockers,
90  uint32_t key)
91 {
92  M0_PRE(key_is_valid(lt, key));
93  return lockers->loc_slots[key] == NULL;
94 }
95 
96 M0_INTERNAL void m0_lockers_fini(struct m0_lockers_type *lt,
97  struct m0_lockers *lockers)
98 {
99 }
100 
101 static bool key_is_valid(const struct m0_lockers_type *lt, int key)
102 {
103  return key < lt->lot_max && lt->lot_inuse[key];
104 }
105 
109 /*
110  * Local variables:
111  * c-indentation-style: "K&R"
112  * c-basic-offset: 8
113  * tab-width: 8
114  * fill-column: 80
115  * scroll-step: 1
116  * End:
117  */
118 /*
119  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
120  */
#define M0_PRE(cond)
bool * lot_inuse
Definition: lockers.h:80
M0_INTERNAL void m0_lockers_fini(struct m0_lockers_type *lt, struct m0_lockers *lockers)
Definition: lockers.c:96
#define NULL
Definition: misc.h:38
struct m0_bufvec data
Definition: di.c:40
M0_INTERNAL void * m0_lockers_get(const struct m0_lockers_type *lt, const struct m0_lockers *lockers, uint32_t key)
Definition: lockers.c:72
int i
Definition: dir.c:1033
static int key
Definition: locality.c:283
M0_INTERNAL int m0_lockers_allot(struct m0_lockers_type *lt)
Definition: lockers.c:44
static bool key_is_valid(const struct m0_lockers_type *lt, int key)
Definition: lockers.c:101
M0_INTERNAL void m0_lockers_clear(const struct m0_lockers_type *lt, struct m0_lockers *lockers, uint32_t key)
Definition: lockers.c:80
M0_INTERNAL bool m0_lockers_is_empty(const struct m0_lockers_type *lt, const struct m0_lockers *lockers, uint32_t key)
Definition: lockers.c:88
M0_INTERNAL void m0_lockers_set(const struct m0_lockers_type *lt, struct m0_lockers *lockers, uint32_t key, void *data)
Definition: lockers.c:63
uint32_t lot_max
Definition: lockers.h:78
M0_INTERNAL void m0_lockers_init(const struct m0_lockers_type *lt, struct m0_lockers *lockers)
Definition: lockers.c:37
void * loc_slots[0]
Definition: lockers.h:84
M0_INTERNAL void m0_lockers_free(struct m0_lockers_type *lt, int key)
Definition: lockers.c:57
Definition: idx_mock.c:47
#define M0_IMPOSSIBLE(fmt,...)