| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 |
|
| 7 | use crate::{
|
| 8 | alloc::{AllocError, Flags},
|
| 9 | cpu::CpuId,
|
| 10 | prelude::*,
|
| 11 | types::Opaque,
|
| 12 | };
|
| 13 |
|
| 14 | #[cfg(CONFIG_CPUMASK_OFFSTACK)]
|
| 15 | use core::ptr::{self, NonNull};
|
| 16 |
|
| 17 | use core::ops::{Deref, DerefMut};
|
| 18 |
|
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | #[repr(transparent)]
|
| 49 | pub struct Cpumask(Opaque<bindings::cpumask>);
|
| 50 |
|
| 51 | impl Cpumask {
|
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | pub unsafe fn from_raw_mut<'a>(ptr: *mut bindings::cpumask) -> &'a mut Self {
|
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | unsafe { &mut *ptr.cast() }
|
| 64 | }
|
| 65 |
|
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | pub unsafe fn from_raw<'a>(ptr: *const bindings::cpumask) -> &'a Self {
|
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | unsafe { &*ptr.cast() }
|
| 78 | }
|
| 79 |
|
| 80 | |
| 81 | pub fn as_raw(&self) -> *mut bindings::cpumask {
|
| 82 | let this: *const Self = self;
|
| 83 | this.cast_mut().cast()
|
| 84 | }
|
| 85 |
|
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | #[inline]
|
| 92 | pub fn set(&mut self, cpu: CpuId) {
|
| 93 | |
| 94 | unsafe { bindings::__cpumask_set_cpu(u32::from(cpu), self.as_raw()) };
|
| 95 | }
|
| 96 |
|
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | #[inline]
|
| 103 | pub fn clear(&mut self, cpu: CpuId) {
|
| 104 | |
| 105 | |
| 106 | unsafe { bindings::__cpumask_clear_cpu(i32::from(cpu), self.as_raw()) };
|
| 107 | }
|
| 108 |
|
| 109 | |
| 110 | |
| 111 | |
| 112 | #[inline]
|
| 113 | pub fn test(&self, cpu: CpuId) -> bool {
|
| 114 | |
| 115 | unsafe { bindings::cpumask_test_cpu(i32::from(cpu), self.as_raw()) }
|
| 116 | }
|
| 117 |
|
| 118 | |
| 119 | |
| 120 | |
| 121 | #[inline]
|
| 122 | pub fn setall(&mut self) {
|
| 123 | |
| 124 | unsafe { bindings::cpumask_setall(self.as_raw()) };
|
| 125 | }
|
| 126 |
|
| 127 | |
| 128 | |
| 129 | |
| 130 | #[inline]
|
| 131 | pub fn empty(&self) -> bool {
|
| 132 | |
| 133 | unsafe { bindings::cpumask_empty(self.as_raw()) }
|
| 134 | }
|
| 135 |
|
| 136 | |
| 137 | |
| 138 | |
| 139 | #[inline]
|
| 140 | pub fn full(&self) -> bool {
|
| 141 | |
| 142 | unsafe { bindings::cpumask_full(self.as_raw()) }
|
| 143 | }
|
| 144 |
|
| 145 | |
| 146 | |
| 147 | |
| 148 | #[inline]
|
| 149 | pub fn weight(&self) -> u32 {
|
| 150 | |
| 151 | unsafe { bindings::cpumask_weight(self.as_raw()) }
|
| 152 | }
|
| 153 |
|
| 154 | |
| 155 | |
| 156 | |
| 157 | #[inline]
|
| 158 | pub fn copy(&self, dstp: &mut Self) {
|
| 159 | |
| 160 | unsafe { bindings::cpumask_copy(dstp.as_raw(), self.as_raw()) };
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | #[repr(transparent)]
|
| 216 | pub struct CpumaskVar {
|
| 217 | #[cfg(CONFIG_CPUMASK_OFFSTACK)]
|
| 218 | ptr: NonNull<Cpumask>,
|
| 219 | #[cfg(not(CONFIG_CPUMASK_OFFSTACK))]
|
| 220 | mask: Cpumask,
|
| 221 | }
|
| 222 |
|
| 223 | impl CpumaskVar {
|
| 224 | |
| 225 | pub fn new_zero(_flags: Flags) -> Result<Self, AllocError> {
|
| 226 | Ok(Self {
|
| 227 | #[cfg(CONFIG_CPUMASK_OFFSTACK)]
|
| 228 | ptr: {
|
| 229 | let mut ptr: *mut bindings::cpumask = ptr::null_mut();
|
| 230 |
|
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | unsafe { bindings::zalloc_cpumask_var(&mut ptr, _flags.as_raw()) };
|
| 236 | NonNull::new(ptr.cast()).ok_or(AllocError)?
|
| 237 | },
|
| 238 |
|
| 239 | #[cfg(not(CONFIG_CPUMASK_OFFSTACK))]
|
| 240 | mask: Cpumask(Opaque::zeroed()),
|
| 241 | })
|
| 242 | }
|
| 243 |
|
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | pub unsafe fn new(_flags: Flags) -> Result<Self, AllocError> {
|
| 251 | Ok(Self {
|
| 252 | #[cfg(CONFIG_CPUMASK_OFFSTACK)]
|
| 253 | ptr: {
|
| 254 | let mut ptr: *mut bindings::cpumask = ptr::null_mut();
|
| 255 |
|
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | unsafe { bindings::alloc_cpumask_var(&mut ptr, _flags.as_raw()) };
|
| 261 | NonNull::new(ptr.cast()).ok_or(AllocError)?
|
| 262 | },
|
| 263 | #[cfg(not(CONFIG_CPUMASK_OFFSTACK))]
|
| 264 | mask: Cpumask(Opaque::uninit()),
|
| 265 | })
|
| 266 | }
|
| 267 |
|
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | pub unsafe fn from_raw_mut<'a>(ptr: *mut bindings::cpumask_var_t) -> &'a mut Self {
|
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | unsafe { &mut *ptr.cast() }
|
| 280 | }
|
| 281 |
|
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | pub unsafe fn from_raw<'a>(ptr: *const bindings::cpumask_var_t) -> &'a Self {
|
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | unsafe { &*ptr.cast() }
|
| 294 | }
|
| 295 |
|
| 296 | |
| 297 | pub fn try_clone(cpumask: &Cpumask) -> Result<Self> {
|
| 298 | |
| 299 | let mut cpumask_var = unsafe { Self::new(GFP_KERNEL) }?;
|
| 300 |
|
| 301 | cpumask.copy(&mut cpumask_var);
|
| 302 | Ok(cpumask_var)
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 | |
| 307 | impl Deref for CpumaskVar {
|
| 308 | type Target = Cpumask;
|
| 309 |
|
| 310 | #[cfg(CONFIG_CPUMASK_OFFSTACK)]
|
| 311 | fn deref(&self) -> &Self::Target {
|
| 312 | |
| 313 | unsafe { &*self.ptr.as_ptr() }
|
| 314 | }
|
| 315 |
|
| 316 | #[cfg(not(CONFIG_CPUMASK_OFFSTACK))]
|
| 317 | fn deref(&self) -> &Self::Target {
|
| 318 | &self.mask
|
| 319 | }
|
| 320 | }
|
| 321 |
|
| 322 | impl DerefMut for CpumaskVar {
|
| 323 | #[cfg(CONFIG_CPUMASK_OFFSTACK)]
|
| 324 | fn deref_mut(&mut self) -> &mut Cpumask {
|
| 325 | |
| 326 | unsafe { self.ptr.as_mut() }
|
| 327 | }
|
| 328 |
|
| 329 | #[cfg(not(CONFIG_CPUMASK_OFFSTACK))]
|
| 330 | fn deref_mut(&mut self) -> &mut Cpumask {
|
| 331 | &mut self.mask
|
| 332 | }
|
| 333 | }
|
| 334 |
|
| 335 | impl Drop for CpumaskVar {
|
| 336 | fn drop(&mut self) {
|
| 337 | #[cfg(CONFIG_CPUMASK_OFFSTACK)]
|
| 338 | |
| 339 | unsafe {
|
| 340 | bindings::free_cpumask_var(self.as_raw())
|
| 341 | };
|
| 342 | }
|
| 343 | }
|