From 2e468fb47ac58c2d5724b84d9b41c7a36fc4f299 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 12 Apr 2025 18:41:43 +0200 Subject: [PATCH] add sp_bitmap_from_bitvec --- include/servicepoint.h | 9 +++++++++ src/bitmap.rs | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/servicepoint.h b/include/servicepoint.h index 2b9b07c..51b00ca 100644 --- a/include/servicepoint.h +++ b/include/servicepoint.h @@ -388,6 +388,15 @@ void sp_bitmap_fill(Bitmap */*notnull*/ bitmap, bool value); */ void sp_bitmap_free(Bitmap */*notnull*/ bitmap); +/** + * Tries to convert the BitVec to a Bitmap. + * + * The provided BitVec gets consumed. + * + * Returns NULL in case of error. + */ +Bitmap *sp_bitmap_from_bitvec(size_t width, SPBitVec */*notnull*/ bitvec); + /** * Gets the current value at the specified position in the [Bitmap]. * diff --git a/src/bitmap.rs b/src/bitmap.rs index 988a6ce..f203395 100644 --- a/src/bitmap.rs +++ b/src/bitmap.rs @@ -68,6 +68,24 @@ pub unsafe extern "C" fn sp_bitmap_load( } } +/// Tries to convert the BitVec to a Bitmap. +/// +/// The provided BitVec gets consumed. +/// +/// Returns NULL in case of error. +#[no_mangle] +pub unsafe extern "C" fn sp_bitmap_from_bitvec( + width: usize, + bitvec: NonNull, +) -> *mut Bitmap { + let bitvec = unsafe { *Box::from_raw(bitvec.as_ptr()) }; + if let Ok(bitmap) = Bitmap::from_bitvec(width, bitvec.0) { + heap_move(bitmap) + } else { + std::ptr::null_mut() + } +} + /// Clones a [Bitmap]. #[no_mangle] pub unsafe extern "C" fn sp_bitmap_clone(