Motr  M0
cookie.c
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 
51 #ifdef __KERNEL__
52 #include <linux/string.h>
53 #else
54 #include <string.h>
55 #endif
56 
57 #include "lib/types.h"
58 #include "lib/errno.h" /* -EPROTO */
59 #include "lib/cookie.h"
60 #include "lib/arith.h" /* M0_IS_8ALIGNED */
61 #include "lib/time.h" /* m0_time_now() */
62 
63 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_LIB
64 #include "lib/trace.h"
65 
66 static uint64_t cookie_generation;
67 
68 M0_INTERNAL const struct m0_cookie M0_COOKIE_NULL = {
69  .co_generation = 0xffff,
70  .co_addr = 7,
71 };
72 
73 M0_INTERNAL bool m0_arch_addr_is_sane(const void *addr);
74 M0_INTERNAL int m0_arch_cookie_global_init(void);
75 M0_INTERNAL void m0_arch_cookie_global_fini(void);
76 
77 M0_INTERNAL int m0_cookie_global_init(void)
78 {
81 }
82 
83 M0_INTERNAL void m0_cookie_new(uint64_t * gen)
84 {
85  M0_PRE(gen != NULL);
86 
88 }
89 
90 M0_INTERNAL void m0_cookie_init(struct m0_cookie *cookie, const uint64_t *obj)
91 {
92  M0_PRE(cookie != NULL);
93  M0_PRE(obj != NULL);
94 
95  cookie->co_addr = (uint64_t)obj;
96  cookie->co_generation = *obj;
97 }
98 
99 M0_INTERNAL bool m0_addr_is_sane(const uint64_t *addr)
100 {
101  return addr > (uint64_t *)4096 && m0_arch_addr_is_sane(addr);
102 }
103 
104 M0_INTERNAL bool m0_addr_is_sane_and_aligned(const uint64_t *addr)
105 {
107 }
108 
109 M0_INTERNAL int m0_cookie_dereference(const struct m0_cookie *cookie,
110  uint64_t ** addr)
111 {
112  uint64_t *obj;
113 
114  M0_PRE(cookie != NULL);
115  M0_PRE(addr != NULL);
116 
117  obj = (uint64_t *)cookie->co_addr;
119  *addr = obj;
120  return 0;
121  } else
122  return -EPROTO;
123 }
124 
125 M0_INTERNAL bool m0_cookie_is_null(const struct m0_cookie *cookie)
126 {
127  return cookie->co_generation == M0_COOKIE_NULL.co_generation &&
128  cookie->co_addr == M0_COOKIE_NULL.co_addr;
129 }
130 
131 M0_INTERNAL bool m0_cookie_is_eq(const struct m0_cookie *cookie1,
132  const struct m0_cookie *cookie2)
133 {
134  return memcmp(cookie1, cookie2, sizeof *cookie1) == 0;
135 }
136 
137 M0_INTERNAL void m0_cookie_global_fini(void)
138 {
140 }
141 
142 #undef M0_TRACE_SUBSYSTEM
143 
146 /*
147  * Local variables:
148  * c-indentation-style: "K&R"
149  * c-basic-offset: 8
150  * tab-width: 8
151  * fill-column: 80
152  * scroll-step: 1
153  * End:
154  */
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
Definition: beck.c:170
static struct foo * obj
Definition: tlist.c:302
if(value==NULL)
Definition: dir.c:350
m0_time_t m0_time_now(void)
Definition: time.c:134
Definition: xcode.h:73
#define M0_IS_8ALIGNED(val)
Definition: arith.h:190