1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 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
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
use libc::c_char;

use ffi;
use std::fmt;
use std::ptr;
use std::ffi::CStr;
use std::ops::Not;
use std::clone::Clone;

pub enum IntHwlocBitmap {}

pub struct HwlocBitmap {
    bitmap: *mut IntHwlocBitmap,
    manage: bool,
}

pub type CpuSet = HwlocBitmap;
pub type NodeSet = HwlocBitmap;

impl HwlocBitmap {

    /// Creates a new empty HwlocBitmap (either CpuSet or NodeSet).
    ///
    /// Examples:
    ///
    /// ```
    /// use hwloc::NodeSet;
    ///
    /// let bitmap = NodeSet::new();
    /// assert_eq!("", format!("{}", bitmap));
    // ```
    pub fn new() -> HwlocBitmap {
        let int_bitmap = unsafe { ffi::hwloc_bitmap_alloc() };
        HwlocBitmap {
            bitmap: int_bitmap,
            manage: true,
        }
    }

    /// Creates a new HwlocBitmap (either CpuSet or NodeSet) and sets one index right away.
    ///
    /// Examples:
    ///
    /// ```
    /// use hwloc::NodeSet;
    ///
    /// let bitmap = NodeSet::from(1);
    /// assert_eq!("1", format!("{}", bitmap));
    // ```
    pub fn from(id: u32) -> HwlocBitmap {
        let mut bitmap = HwlocBitmap::new();
        bitmap.set(id);
        bitmap
    }

    /// Creates a new HwlocBitmap (either CpuSet or NodeSet) and sets the range right away.
    ///
    /// Examples:
    ///
    /// ```
    /// use hwloc::CpuSet;
    ///
    /// let bitmap = CpuSet::from_range(0, 5);
    /// assert_eq!("0-5", format!("{}", bitmap));
    // ```
    pub fn from_range(begin: u32, end: i32) -> HwlocBitmap {
        let mut bitmap = HwlocBitmap::new();
        bitmap.set_range(begin, end);
        bitmap
    }

    /// Wraps the given bitmap pointer into its rust bitmap representation.
    pub fn from_raw(bitmap: *mut IntHwlocBitmap, manage: bool) -> HwlocBitmap {
        HwlocBitmap {
            bitmap: bitmap,
            manage: manage,
        }
    }

    /// Returns the containted rae bitmap pointer for interaction with hwloc.
    pub fn as_ptr(&self) -> *const IntHwlocBitmap {
        self.bitmap as *const IntHwlocBitmap
    }

    /// Add index id in bitmap bitmap.
    pub fn set(&mut self, id: u32) {
        unsafe { ffi::hwloc_bitmap_set(self.bitmap, id) }
    }

    /// Add indexes from begin to end in this bitmap.
    ///
    /// If end is -1, the range is infinite.
    pub fn set_range(&mut self, begin: u32, end: i32) {
        unsafe { ffi::hwloc_bitmap_set_range(self.bitmap, begin, end) }
    }

    /// Remove index id from bitmap bitmap.
    pub fn unset(&mut self, id: u32) {
        unsafe { ffi::hwloc_bitmap_clr(self.bitmap, id) }
    }

    /// Remove indexes from begin to end in this bitmap.
    ///
    /// If end is -1, the range is infinite.
    pub fn unset_range(&mut self, begin: u32, end: i32) {
        unsafe { ffi::hwloc_bitmap_clr_range(self.bitmap, begin, end) }
    }

    /// The number of indexes that are in the bitmap.
    pub fn weight(&self) -> i32 {
        unsafe { ffi::hwloc_bitmap_weight(self.bitmap) }
    }

    /// Clears the complete bitmap.
    pub fn clear(&mut self) {
        unsafe { ffi::hwloc_bitmap_zero(self.bitmap) }
    }

    /// Checks if this bitmap has set fields.
    pub fn is_empty(&self) -> bool {
        let result = unsafe { ffi::hwloc_bitmap_iszero(self.bitmap) };
        if result == 0 {
            false
        } else {
            true
        }
    }

    /// Check if the field with the given id is set.
    pub fn is_set(&self, id: u32) -> bool {
        let result = unsafe { ffi::hwloc_bitmap_isset(self.bitmap, id) };
        if result == 0 {
            false
        } else {
            true
        }
    }

    /// Keep a single index among those set in the bitmap.
    ///
    /// May be useful before binding so that the process does not have a
    /// chance of migrating between multiple logical CPUs in the original mask.
    pub fn singlify(&mut self) {
        unsafe { ffi::hwloc_bitmap_singlify(self.bitmap) }
    }

    /// Inverts the current bitmap.
    pub fn invert(&mut self) {
        unsafe { ffi::hwloc_bitmap_not(self.bitmap, self.bitmap) }
    }

    /// Compute the first index (least significant bit) in bitmap bitmap.
    ///
    /// returns -1 if no index is set.
    pub fn first(&self) -> i32 {
        unsafe { ffi::hwloc_bitmap_first(self.bitmap) }
    }

    /// Compute the last index (most significant bit) in bitmap bitmap.
    ///
    /// returns -1 if no index is bitmap, or if the index bitmap is infinite.
    pub fn last(&self) -> i32 {
        unsafe { ffi::hwloc_bitmap_last(self.bitmap) }
    }
}

impl Not for HwlocBitmap {
    type Output = HwlocBitmap;

    /// Returns a new bitmap which contains the negated values of the current
    /// one.
    fn not(self) -> HwlocBitmap {
        unsafe {
            let result = ffi::hwloc_bitmap_alloc();
            ffi::hwloc_bitmap_not(result, self.bitmap);
            HwlocBitmap::from_raw(result, true)
        }
    }
}

impl Drop for HwlocBitmap {
    fn drop(&mut self) {
        if self.manage {
            unsafe { ffi::hwloc_bitmap_free(self.bitmap) }
        }
    }
}

impl fmt::Display for HwlocBitmap {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut result: *mut c_char = ptr::null_mut();
        unsafe {
            ffi::hwloc_bitmap_list_asprintf(&mut result, self.bitmap);
            write!(f, "{}", CStr::from_ptr(result).to_str().unwrap())
        }
    }
}

impl fmt::Debug for HwlocBitmap {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut result: *mut c_char = ptr::null_mut();
        unsafe {
            ffi::hwloc_bitmap_list_asprintf(&mut result, self.bitmap);
            write!(f, "{}", CStr::from_ptr(result).to_str().unwrap())
        }
    }
}

impl Clone for HwlocBitmap {
    fn clone(&self) -> HwlocBitmap {
        let dup = unsafe { ffi::hwloc_bitmap_dup(self.bitmap) };
        HwlocBitmap::from_raw(dup, true)
    }
}

impl PartialEq for HwlocBitmap {
    fn eq(&self, other: &Self) -> bool {
        let result = unsafe { ffi::hwloc_bitmap_compare(self.bitmap, other.as_ptr()) };
        result == 0
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn should_check_if_bitmap_is_empty() {
        let mut bitmap = HwlocBitmap::new();

        assert!(bitmap.is_empty());
        bitmap.set(1);
        assert!(!bitmap.is_empty());
        bitmap.unset(1);
        assert!(bitmap.is_empty());
    }

    #[test]
    fn should_create_by_range() {
        let bitmap = HwlocBitmap::from_range(0, 5);
        assert_eq!("0-5", format!("{}", bitmap));
    }

    #[test]
    fn should_set_and_unset_bitmap_index() {
        let mut bitmap = HwlocBitmap::new();
        assert_eq!("", format!("{}", bitmap));

        assert!(bitmap.is_empty());

        bitmap.set(1);
        bitmap.set(3);
        bitmap.set(8);
        assert_eq!("1,3,8", format!("{}", bitmap));
        assert!(!bitmap.is_empty());

        bitmap.unset(3);
        assert_eq!("1,8", format!("{}", bitmap));
        assert!(!bitmap.is_empty());
    }

    #[test]
    fn should_check_if_is_set() {
        let mut bitmap = HwlocBitmap::new();

        assert!(!bitmap.is_set(3));
        bitmap.set(3);
        assert!(bitmap.is_set(3));
        bitmap.unset(3);
        assert!(!bitmap.is_set(3));
    }

    #[test]
    fn should_set_and_unset_range() {
        let mut bitmap = HwlocBitmap::new();
        assert_eq!("", format!("{}", bitmap));

        bitmap.set_range(2, 5);
        assert_eq!("2-5", format!("{}", bitmap));

        bitmap.set_range(4, 7);
        assert_eq!("2-7", format!("{}", bitmap));

        bitmap.set(9);
        assert_eq!("2-7,9", format!("{}", bitmap));

        bitmap.unset_range(6, 10);
        assert_eq!("2-5", format!("{}", bitmap));
    }

    #[test]
    fn should_clear_the_bitmap() {
        let mut bitmap = HwlocBitmap::new();

        assert!(bitmap.is_empty());
        bitmap.set_range(4, 7);
        assert!(!bitmap.is_empty());
        assert!(bitmap.is_set(5));

        bitmap.clear();
        assert!(!bitmap.is_set(5));
        assert!(bitmap.is_empty());
    }

    #[test]
    fn should_get_weight() {
        let mut bitmap = HwlocBitmap::new();

        assert_eq!(0, bitmap.weight());

        bitmap.set(9);
        assert_eq!(1, bitmap.weight());

        bitmap.set_range(2, 5);
        assert_eq!(5, bitmap.weight());

        bitmap.unset(4);
        assert_eq!(4, bitmap.weight());

        bitmap.clear();
        assert_eq!(0, bitmap.weight());
    }

    #[test]
    fn should_invert() {
        let mut bitmap = HwlocBitmap::new();
        bitmap.set(3);

        assert_eq!("3", format!("{}", bitmap));
        assert_eq!("0-2,4-", format!("{}", !bitmap));
    }

    #[test]
    fn should_singlify() {
        let mut bitmap = HwlocBitmap::new();
        bitmap.set_range(0, 127);
        assert_eq!(128, bitmap.weight());

        bitmap.invert();
        assert_eq!(-1, bitmap.weight());

        bitmap.singlify();
        assert_eq!(1, bitmap.weight());

        assert_eq!(128, bitmap.first());
        assert_eq!(128, bitmap.last());
    }

    #[test]
    fn should_check_equality() {
        let mut bitmap1 = HwlocBitmap::new();
        bitmap1.set_range(0, 3);

        let mut bitmap2 = HwlocBitmap::new();
        bitmap2.set_range(0, 3);

        let mut bitmap3 = HwlocBitmap::new();
        bitmap3.set_range(1, 5);

        assert_eq!(bitmap1, bitmap2);
        assert!(bitmap2 == bitmap1);
        assert!(bitmap1 != bitmap3);
        assert!(bitmap3 != bitmap2);
    }

    #[test]
    fn should_clone() {
        let mut src = HwlocBitmap::new();
        src.set_range(0, 3);

        let dst = src.clone();
        assert_eq!(src, dst);
    }

}