Motr  M0
bob.h
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-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 #pragma once
24 
25 #ifndef __MOTR_LIB_BOB_H__
26 #define __MOTR_LIB_BOB_H__
27 
60 #include "lib/types.h" /* uint64_t */
61 
62 /* import */
63 struct m0_tl_descr;
64 
65 /* export */
66 struct m0_bob_type;
67 
71 struct m0_bob_type {
73  const char *bt_name;
77  uint64_t bt_magix;
82  bool (*bt_check)(const void *bob);
83 };
84 
88 M0_INTERNAL void m0_bob_type_tlist_init(struct m0_bob_type *bt,
89  const struct m0_tl_descr *td);
90 
94 M0_INTERNAL void m0_bob_init(const struct m0_bob_type *bt, void *bob);
95 
99 M0_INTERNAL void m0_bob_fini(const struct m0_bob_type *bt, void *bob);
100 
105 M0_INTERNAL bool m0_bob_check(const struct m0_bob_type *bt, const void *bob);
106 
111 #define M0_BOB_DEFINE(scope, bob_type, type) \
112 scope void type ## _bob_init(struct type *bob) \
113 { \
114  m0_bob_init(bob_type, bob); \
115 } \
116  \
117 scope void type ## _bob_fini(struct type *bob) \
118 { \
119  m0_bob_fini(bob_type, bob); \
120 } \
121  \
122 scope M0_UNUSED bool type ## _bob_check(const struct type *bob) \
123 { \
124  return m0_bob_check(bob_type, bob); \
125 } \
126  \
127 struct __ ## type ## _semicolon_catcher
128 
129 #define M0_BOB_DECLARE(scope, type) \
130 scope void type ## _bob_init(struct type *bob); \
131 scope void type ## _bob_fini(struct type *bob); \
132 scope bool type ## _bob_check(const struct type *bob)
133 
140 #define bob_of(ptr, type, field, bt) \
141 ({ \
142  void *__ptr = (void *)(ptr); \
143  type *__amb; \
144  \
145  M0_ASSERT(__ptr != NULL); \
146  __amb = container_of(__ptr, type, field); \
147  \
148  M0_ASSERT_INFO(m0_bob_check(bt, __amb), \
149  "%s.%s [%p->%p (%s)] got: %" PRIx64 " want: %" PRIx64 \
150  " check: %i.", (bt)->bt_name, #field, __ptr, __amb, #type, \
151  *((uint64_t *)(((void *)__amb) + (bt)->bt_magix_offset)), \
152  (bt)->bt_magix, \
153  (bt)->bt_check != NULL ? (bt)->bt_check(__amb) : -1); \
154  __amb; \
155 })
156 
159 /* __MOTR_LIB_BOB_H__ */
160 #endif
161 
162 /*
163  * Local variables:
164  * c-indentation-style: "K&R"
165  * c-basic-offset: 8
166  * tab-width: 8
167  * fill-column: 80
168  * scroll-step: 1
169  * End:
170  */
uint64_t bt_magix
Definition: bob.h:77
const char * bt_name
Definition: bob.h:73
int bt_magix_offset
Definition: bob.h:75
static struct btype bt[]
Definition: beck.c:459
M0_INTERNAL void m0_bob_init(const struct m0_bob_type *bt, void *bob)
Definition: bob.c:61
M0_INTERNAL void m0_bob_fini(const struct m0_bob_type *bt, void *bob)
Definition: bob.c:68
bool(* bt_check)(const void *bob)
Definition: bob.h:82
M0_INTERNAL void m0_bob_type_tlist_init(struct m0_bob_type *bt, const struct m0_tl_descr *td)
Definition: bob.c:41
M0_INTERNAL bool m0_bob_check(const struct m0_bob_type *bt, const void *bob)
Definition: bob.c:74