Skip to content

Commit

Permalink
rename _dealloc to _free
Browse files Browse the repository at this point in the history
  • Loading branch information
kaesaecracker committed Sep 7, 2024
1 parent f45c809 commit aa359cc
Show file tree
Hide file tree
Showing 19 changed files with 389 additions and 410 deletions.
4 changes: 2 additions & 2 deletions crates/servicepoint_binding_c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ int main(void) {
SPPacket *packet = sp_packet_from_command(command);
while (sp_connection_send(connection, sp_packet_clone(packet)));

sp_packet_dealloc(packet);
sp_connection_dealloc(connection);
sp_packet_free(packet);
sp_connection_free(connection);
return 0;
}
```
Expand Down
4 changes: 2 additions & 2 deletions crates/servicepoint_binding_c/examples/lang_c/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int main(void) {
SPPacket *packet = sp_packet_from_command(command);
while (sp_connection_send(connection, sp_packet_clone(packet)));

sp_packet_dealloc(packet);
sp_connection_dealloc(connection);
sp_packet_free(packet);
sp_connection_free(connection);
return 0;
}
54 changes: 27 additions & 27 deletions crates/servicepoint_binding_c/src/bit_vec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! C functions for interacting with `BitVec`s
//! C functions for interacting with `SPBitVec`s
//!
//! prefix `sp_bit_vec_`

Expand All @@ -11,7 +11,7 @@ use servicepoint::bitvec::prelude::{BitVec, Msb0};
/// ```C
/// SPBitVec vec = sp_bit_vec_new(8);
/// sp_bit_vec_set(vec, 5, true);
/// sp_bit_vec_dealloc(vec);
/// sp_bit_vec_free(vec);
/// ```
pub struct SPBitVec(BitVec<u8, Msb0>);

Expand All @@ -33,13 +33,13 @@ impl Clone for SPBitVec {
}
}

/// Creates a new `BitVec` instance.
/// Creates a new `SPBitVec` instance.
///
/// # Arguments
///
/// - `size`: size in bits.
///
/// returns: `BitVec` with all bits set to false.
/// returns: `SPBitVec` with all bits set to false.
///
/// # Panics
///
Expand All @@ -50,13 +50,13 @@ impl Clone for SPBitVec {
/// The caller has to make sure that:
///
/// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_bit_vec_dealloc`.
/// by explicitly calling `sp_bit_vec_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_new(size: usize) -> *mut SPBitVec {
Box::into_raw(Box::new(SPBitVec(BitVec::repeat(false, size))))
}

/// Interpret the data as a series of bits and load then into a new `BitVec` instance.
/// Interpret the data as a series of bits and load then into a new `SPBitVec` instance.
///
/// # Safety
///
Expand All @@ -65,7 +65,7 @@ pub unsafe extern "C" fn sp_bit_vec_new(size: usize) -> *mut SPBitVec {
/// - `data` points to a valid memory location of at least `data_length`
/// bytes in size.
/// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_bit_vec_dealloc`.
/// by explicitly calling `sp_bit_vec_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_load(
data: *const u8,
Expand All @@ -75,38 +75,38 @@ pub unsafe extern "C" fn sp_bit_vec_load(
Box::into_raw(Box::new(SPBitVec(BitVec::from_slice(data))))
}

/// Clones a `BitVec`.
/// Clones a `SPBitVec`.
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `this` points to a valid `BitVec`
/// - `this` points to a valid `SPBitVec`
/// - `this` is not written to concurrently
/// - the returned instance is freed in some way, either by using a consuming function or
/// by explicitly calling `sp_bit_vec_dealloc`.
/// by explicitly calling `sp_bit_vec_free`.
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_clone(
this: *const SPBitVec,
) -> *mut SPBitVec {
Box::into_raw(Box::new((*this).clone()))
}

/// Deallocates a `BitVec`.
/// Deallocates a `SPBitVec`.
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `this` points to a valid `BitVec`
/// - `this` points to a valid `SPBitVec`
/// - `this` is not used concurrently or after this call
/// - `this` was not passed to another consuming function, e.g. to create a `Command`
/// - `this` was not passed to another consuming function, e.g. to create a `SPCommand`
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_dealloc(this: *mut SPBitVec) {
pub unsafe extern "C" fn sp_bit_vec_free(this: *mut SPBitVec) {
_ = Box::from_raw(this);
}

/// Gets the value of a bit from the `BitVec`.
/// Gets the value of a bit from the `SPBitVec`.
///
/// # Arguments
///
Expand All @@ -123,7 +123,7 @@ pub unsafe extern "C" fn sp_bit_vec_dealloc(this: *mut SPBitVec) {
///
/// The caller has to make sure that:
///
/// - `this` points to a valid `BitVec`
/// - `this` points to a valid `SPBitVec`
/// - `this` is not written to concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_get(
Expand All @@ -133,7 +133,7 @@ pub unsafe extern "C" fn sp_bit_vec_get(
*(*this).0.get(index).unwrap()
}

/// Sets the value of a bit in the `BitVec`.
/// Sets the value of a bit in the `SPBitVec`.
///
/// # Arguments
///
Expand All @@ -151,7 +151,7 @@ pub unsafe extern "C" fn sp_bit_vec_get(
///
/// The caller has to make sure that:
///
/// - `this` points to a valid `BitVec`
/// - `this` points to a valid `SPBitVec`
/// - `this` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_set(
Expand All @@ -162,7 +162,7 @@ pub unsafe extern "C" fn sp_bit_vec_set(
(*this).0.set(index, value)
}

/// Sets the value of all bits in the `BitVec`.
/// Sets the value of all bits in the `SPBitVec`.
///
/// # Arguments
///
Expand All @@ -172,20 +172,20 @@ pub unsafe extern "C" fn sp_bit_vec_set(
///
/// The caller has to make sure that:
///
/// - `this` points to a valid `BitVec`
/// - `this` points to a valid `SPBitVec`
/// - `this` is not written to or read from concurrently
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_fill(this: *mut SPBitVec, value: bool) {
(*this).0.fill(value)
}

/// Gets the length of the `BitVec` in bits.
/// Gets the length of the `SPBitVec` in bits.
///
/// # Safety
///
/// The caller has to make sure that:
///
/// - `this` points to a valid `BitVec`
/// - `this` points to a valid `SPBitVec`
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_len(this: *const SPBitVec) -> usize {
(*this).0.len()
Expand All @@ -197,21 +197,21 @@ pub unsafe extern "C" fn sp_bit_vec_len(this: *const SPBitVec) -> usize {
///
/// The caller has to make sure that:
///
/// - `this` points to a valid `BitVec`
/// - `this` points to a valid `SPBitVec`
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_is_empty(this: *const SPBitVec) -> bool {
(*this).0.is_empty()
}

/// Gets an unsafe reference to the data of the `BitVec` instance.
/// Gets an unsafe reference to the data of the `SPBitVec` instance.
///
/// ## Safety
///
/// The caller has to make sure that:
///
/// - `this` points to a valid `BitVec`
/// - the returned memory range is never accessed after the passed `BitVec` has been freed
/// - the returned memory range is never accessed concurrently, either via the `BitVec` or directly
/// - `this` points to a valid `SPBitVec`
/// - the returned memory range is never accessed after the passed `SPBitVec` has been freed
/// - the returned memory range is never accessed concurrently, either via the `SPBitVec` or directly
#[no_mangle]
pub unsafe extern "C" fn sp_bit_vec_unsafe_data_ref(
this: *mut SPBitVec,
Expand Down
Loading

0 comments on commit aa359cc

Please sign in to comment.