WIP: next #1

Draft
vinzenz wants to merge 25 commits from next into main
10 changed files with 101 additions and 110 deletions
Showing only changes of commit f483b5a2d5 - Show all commits

View file

@ -851,13 +851,6 @@ TypedCommand *sp_command_bitvec(size_t offset,
CompressionCode compression, CompressionCode compression,
BinaryOperation operation); BinaryOperation operation);
/**
* Set the brightness of all tiles to the same value.
*
* Returns: a new [servicepoint::Command::Brightness] instance.
*/
TypedCommand */*notnull*/ sp_command_brightness(Brightness brightness);
/** /**
* Set the brightness of individual tiles in a rectangular area of the display. * Set the brightness of individual tiles in a rectangular area of the display.
* *
@ -865,7 +858,7 @@ TypedCommand */*notnull*/ sp_command_brightness(Brightness brightness);
* *
* Returns: a new [servicepoint::Command::CharBrightness] instance. * Returns: a new [servicepoint::Command::CharBrightness] instance.
*/ */
TypedCommand */*notnull*/ sp_command_char_brightness(size_t x, TypedCommand */*notnull*/ sp_command_brightness_grid(size_t x,
size_t y, size_t y,
BrightnessGrid */*notnull*/ grid); BrightnessGrid */*notnull*/ grid);
@ -932,6 +925,13 @@ TypedCommand */*notnull*/ sp_command_fade_out(void);
*/ */
void sp_command_free(TypedCommand */*notnull*/ command); void sp_command_free(TypedCommand */*notnull*/ command);
/**
* Set the brightness of all tiles to the same value.
*
* Returns: a new [servicepoint::Command::Brightness] instance.
*/
TypedCommand */*notnull*/ sp_command_global_brightness(Brightness brightness);
/** /**
* Kills the udp daemon on the display, which usually results in a restart. * Kills the udp daemon on the display, which usually results in a restart.
* *

View file

@ -1,8 +1,8 @@
use crate::byte_slice::ByteSlice;
use crate::{heap_drop, heap_move, heap_move_nonnull};
use servicepoint::{Bitmap, DataRef, Grid}; use servicepoint::{Bitmap, DataRef, Grid};
use std::ptr::NonNull; use std::ptr::NonNull;
use crate::byte_slice::ByteSlice;
/// Creates a new [Bitmap] with the specified dimensions. /// Creates a new [Bitmap] with the specified dimensions.
/// ///
/// # Arguments /// # Arguments
@ -32,7 +32,7 @@ pub unsafe extern "C" fn sp_bitmap_new(
height: usize, height: usize,
) -> *mut Bitmap { ) -> *mut Bitmap {
if let Some(bitmap) = Bitmap::new(width, height) { if let Some(bitmap) = Bitmap::new(width, height) {
Box::leak(Box::new(bitmap)) heap_move(bitmap)
} else { } else {
std::ptr::null_mut() std::ptr::null_mut()
} }
@ -43,8 +43,7 @@ pub unsafe extern "C" fn sp_bitmap_new(
/// returns: [Bitmap] initialized to all pixels off. /// returns: [Bitmap] initialized to all pixels off.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_new_max_sized() -> NonNull<Bitmap> { pub unsafe extern "C" fn sp_bitmap_new_max_sized() -> NonNull<Bitmap> {
let result = Box::new(Bitmap::max_sized()); heap_move_nonnull(Bitmap::max_sized())
NonNull::from(Box::leak(result))
} }
/// Loads a [Bitmap] with the specified dimensions from the provided data. /// Loads a [Bitmap] with the specified dimensions from the provided data.
@ -63,7 +62,7 @@ pub unsafe extern "C" fn sp_bitmap_load(
) -> *mut Bitmap { ) -> *mut Bitmap {
let data = unsafe { data.as_slice() }; let data = unsafe { data.as_slice() };
if let Ok(bitmap) = Bitmap::load(width, height, data) { if let Ok(bitmap) = Bitmap::load(width, height, data) {
Box::leak(Box::new(bitmap)) heap_move(bitmap)
} else { } else {
std::ptr::null_mut() std::ptr::null_mut()
} }
@ -74,14 +73,13 @@ pub unsafe extern "C" fn sp_bitmap_load(
pub unsafe extern "C" fn sp_bitmap_clone( pub unsafe extern "C" fn sp_bitmap_clone(
bitmap: NonNull<Bitmap>, bitmap: NonNull<Bitmap>,
) -> NonNull<Bitmap> { ) -> NonNull<Bitmap> {
let result = Box::new(unsafe { bitmap.as_ref().clone() }); heap_move_nonnull(unsafe { bitmap.as_ref().clone() })
NonNull::from(Box::leak(result))
} }
/// Deallocates a [Bitmap]. /// Deallocates a [Bitmap].
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitmap_free(bitmap: NonNull<Bitmap>) { pub unsafe extern "C" fn sp_bitmap_free(bitmap: NonNull<Bitmap>) {
_ = unsafe { Box::from_raw(bitmap.as_ptr()) }; unsafe { heap_drop(bitmap) }
} }
/// Gets the current value at the specified position in the [Bitmap]. /// Gets the current value at the specified position in the [Bitmap].

View file

@ -1,6 +1,6 @@
use crate::ByteSlice; use crate::{heap_drop, heap_move_nonnull, ByteSlice};
use std::ptr::NonNull;
use servicepoint::BitVecU8Msb0; use servicepoint::BitVecU8Msb0;
use std::ptr::NonNull;
/// A vector of bits /// A vector of bits
/// ///
@ -31,21 +31,16 @@ impl Clone for SPBitVec {
/// - when `size` is not divisible by 8. /// - when `size` is not divisible by 8.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitvec_new(size: usize) -> NonNull<SPBitVec> { pub unsafe extern "C" fn sp_bitvec_new(size: usize) -> NonNull<SPBitVec> {
let result = heap_move_nonnull(SPBitVec(BitVecU8Msb0::repeat(false, size)))
Box::new(SPBitVec(BitVecU8Msb0::repeat(false, size)));
NonNull::from(Box::leak(result))
} }
/// Interpret the data as a series of bits and load then into a new [SPBitVec] instance. /// Interpret the data as a series of bits and load then into a new [SPBitVec] instance.
/// ///
/// returns: [SPBitVec] instance containing data. /// returns: [SPBitVec] instance containing data.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitvec_load( pub unsafe extern "C" fn sp_bitvec_load(data: ByteSlice) -> NonNull<SPBitVec> {
data: ByteSlice,
) -> NonNull<SPBitVec> {
let data = unsafe { data.as_slice() }; let data = unsafe { data.as_slice() };
let result = Box::new(SPBitVec(BitVecU8Msb0::from_slice(data))); heap_move_nonnull(SPBitVec(BitVecU8Msb0::from_slice(data)))
NonNull::from(Box::leak(result))
} }
/// Clones a [SPBitVec]. /// Clones a [SPBitVec].
@ -53,14 +48,13 @@ pub unsafe extern "C" fn sp_bitvec_load(
pub unsafe extern "C" fn sp_bitvec_clone( pub unsafe extern "C" fn sp_bitvec_clone(
bit_vec: NonNull<SPBitVec>, bit_vec: NonNull<SPBitVec>,
) -> NonNull<SPBitVec> { ) -> NonNull<SPBitVec> {
let result = Box::new(unsafe { bit_vec.as_ref().clone() }); heap_move_nonnull(unsafe { bit_vec.as_ref().clone() })
NonNull::from(Box::leak(result))
} }
/// Deallocates a [SPBitVec]. /// Deallocates a [SPBitVec].
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_bitvec_free(bit_vec: NonNull<SPBitVec>) { pub unsafe extern "C" fn sp_bitvec_free(bit_vec: NonNull<SPBitVec>) {
_ = unsafe { Box::from_raw(bit_vec.as_ptr()) }; unsafe { heap_drop(bit_vec) }
} }
/// Gets the value of a bit from the [SPBitVec]. /// Gets the value of a bit from the [SPBitVec].

View file

@ -1,4 +1,4 @@
use crate::ByteSlice; use crate::{heap_drop, heap_move, heap_move_nonnull, ByteSlice};
use servicepoint::{Brightness, BrightnessGrid, ByteGrid, DataRef, Grid}; use servicepoint::{Brightness, BrightnessGrid, ByteGrid, DataRef, Grid};
use std::mem::transmute; use std::mem::transmute;
use std::ptr::NonNull; use std::ptr::NonNull;
@ -25,8 +25,7 @@ pub unsafe extern "C" fn sp_brightness_grid_new(
width: usize, width: usize,
height: usize, height: usize,
) -> NonNull<BrightnessGrid> { ) -> NonNull<BrightnessGrid> {
let result = Box::new(BrightnessGrid::new(width, height)); heap_move_nonnull(BrightnessGrid::new(width, height))
NonNull::from(Box::leak(result))
} }
/// Loads a [BrightnessGrid] with the specified dimensions from the provided data. /// Loads a [BrightnessGrid] with the specified dimensions from the provided data.
@ -44,7 +43,7 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
Some(grid) => grid, Some(grid) => grid,
}; };
if let Ok(grid) = BrightnessGrid::try_from(grid) { if let Ok(grid) = BrightnessGrid::try_from(grid) {
Box::leak(Box::new(grid)) heap_move(grid)
} else { } else {
std::ptr::null_mut() std::ptr::null_mut()
} }
@ -55,8 +54,7 @@ pub unsafe extern "C" fn sp_brightness_grid_load(
pub unsafe extern "C" fn sp_brightness_grid_clone( pub unsafe extern "C" fn sp_brightness_grid_clone(
brightness_grid: NonNull<BrightnessGrid>, brightness_grid: NonNull<BrightnessGrid>,
) -> NonNull<BrightnessGrid> { ) -> NonNull<BrightnessGrid> {
let result = Box::new(unsafe { brightness_grid.as_ref().clone() }); heap_move_nonnull(unsafe { brightness_grid.as_ref().clone() })
NonNull::from(Box::leak(result))
} }
/// Deallocates a [BrightnessGrid]. /// Deallocates a [BrightnessGrid].
@ -64,7 +62,7 @@ pub unsafe extern "C" fn sp_brightness_grid_clone(
pub unsafe extern "C" fn sp_brightness_grid_free( pub unsafe extern "C" fn sp_brightness_grid_free(
brightness_grid: NonNull<BrightnessGrid>, brightness_grid: NonNull<BrightnessGrid>,
) { ) {
_ = unsafe { Box::from_raw(brightness_grid.as_ptr()) }; unsafe { heap_drop(brightness_grid) }
} }
/// Gets the current value at the specified position. /// Gets the current value at the specified position.

View file

@ -1,4 +1,4 @@
use crate::ByteSlice; use crate::{heap_drop, heap_move, heap_move_nonnull, ByteSlice};
use servicepoint::{CharGrid, Grid}; use servicepoint::{CharGrid, Grid};
use std::ptr::NonNull; use std::ptr::NonNull;
@ -19,8 +19,7 @@ pub unsafe extern "C" fn sp_char_grid_new(
width: usize, width: usize,
height: usize, height: usize,
) -> NonNull<CharGrid> { ) -> NonNull<CharGrid> {
let result = Box::new(CharGrid::new(width, height)); heap_move_nonnull(CharGrid::new(width, height))
NonNull::from(Box::leak(result))
} }
/// Loads a [CharGrid] with the specified dimensions from the provided data. /// Loads a [CharGrid] with the specified dimensions from the provided data.
@ -34,7 +33,7 @@ pub unsafe extern "C" fn sp_char_grid_load(
) -> *mut CharGrid { ) -> *mut CharGrid {
let data = unsafe { data.as_slice() }; let data = unsafe { data.as_slice() };
if let Ok(grid) = CharGrid::load_utf8(width, height, data.to_vec()) { if let Ok(grid) = CharGrid::load_utf8(width, height, data.to_vec()) {
Box::leak(Box::new(grid)) heap_move(grid)
} else { } else {
std::ptr::null_mut() std::ptr::null_mut()
} }
@ -45,14 +44,13 @@ pub unsafe extern "C" fn sp_char_grid_load(
pub unsafe extern "C" fn sp_char_grid_clone( pub unsafe extern "C" fn sp_char_grid_clone(
char_grid: NonNull<CharGrid>, char_grid: NonNull<CharGrid>,
) -> NonNull<CharGrid> { ) -> NonNull<CharGrid> {
let result = unsafe { char_grid.as_ref().clone() }; heap_move_nonnull(unsafe { char_grid.as_ref().clone() })
NonNull::from(Box::leak(Box::new(result)))
} }
/// Deallocates a [CharGrid]. /// Deallocates a [CharGrid].
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_char_grid_free(char_grid: NonNull<CharGrid>) { pub unsafe extern "C" fn sp_char_grid_free(char_grid: NonNull<CharGrid>) {
_ = unsafe { Box::from_raw(char_grid.as_ptr()) }; unsafe { heap_drop(char_grid) }
} }
/// Returns the current value at the specified position. /// Returns the current value at the specified position.

View file

@ -1,5 +1,8 @@
use crate::SPBitVec; use crate::{heap_drop, heap_move, heap_move_nonnull, SPBitVec};
use servicepoint::{BinaryOperation, Bitmap, Brightness, BrightnessGrid, CharGrid, CompressionCode, Cp437Grid, GlobalBrightnessCommand, Packet, TypedCommand}; use servicepoint::{
BinaryOperation, Bitmap, Brightness, BrightnessGrid, CharGrid,
CompressionCode, Cp437Grid, GlobalBrightnessCommand, Packet, TypedCommand,
};
use std::ptr::NonNull; use std::ptr::NonNull;
/// Tries to turn a [Packet] into a [TypedCommand]. /// Tries to turn a [Packet] into a [TypedCommand].
@ -14,7 +17,7 @@ pub unsafe extern "C" fn sp_command_try_from_packet(
let packet = *unsafe { Box::from_raw(packet.as_ptr()) }; let packet = *unsafe { Box::from_raw(packet.as_ptr()) };
match servicepoint::TypedCommand::try_from(packet) { match servicepoint::TypedCommand::try_from(packet) {
Err(_) => std::ptr::null_mut(), Err(_) => std::ptr::null_mut(),
Ok(command) => Box::into_raw(Box::new(command)), Ok(command) => heap_move(command),
} }
} }
@ -25,8 +28,7 @@ pub unsafe extern "C" fn sp_command_try_from_packet(
pub unsafe extern "C" fn sp_command_clone( pub unsafe extern "C" fn sp_command_clone(
command: NonNull<TypedCommand>, command: NonNull<TypedCommand>,
) -> NonNull<TypedCommand> { ) -> NonNull<TypedCommand> {
let result = Box::new(unsafe { command.as_ref().clone() }); heap_move_nonnull(unsafe { command.as_ref().clone() })
NonNull::from(Box::leak(result))
} }
/// Set all pixels to the off state. /// Set all pixels to the off state.
@ -42,8 +44,7 @@ pub unsafe extern "C" fn sp_command_clone(
/// ``` /// ```
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_clear() -> NonNull<TypedCommand> { pub unsafe extern "C" fn sp_command_clear() -> NonNull<TypedCommand> {
let result = Box::new(servicepoint::ClearCommand.into()); heap_move_nonnull(servicepoint::ClearCommand.into())
NonNull::from(Box::leak(result))
} }
/// Kills the udp daemon on the display, which usually results in a restart. /// Kills the udp daemon on the display, which usually results in a restart.
@ -53,8 +54,7 @@ pub unsafe extern "C" fn sp_command_clear() -> NonNull<TypedCommand> {
/// Returns: a new [servicepoint::Command::HardReset] instance. /// Returns: a new [servicepoint::Command::HardReset] instance.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_hard_reset() -> NonNull<TypedCommand> { pub unsafe extern "C" fn sp_command_hard_reset() -> NonNull<TypedCommand> {
let result = Box::new(servicepoint::HardResetCommand.into()); heap_move_nonnull(servicepoint::HardResetCommand.into())
NonNull::from(Box::leak(result))
} }
/// A yet-to-be-tested command. /// A yet-to-be-tested command.
@ -62,19 +62,17 @@ pub unsafe extern "C" fn sp_command_hard_reset() -> NonNull<TypedCommand> {
/// Returns: a new [servicepoint::Command::FadeOut] instance. /// Returns: a new [servicepoint::Command::FadeOut] instance.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_fade_out() -> NonNull<TypedCommand> { pub unsafe extern "C" fn sp_command_fade_out() -> NonNull<TypedCommand> {
let result = Box::new(servicepoint::FadeOutCommand.into()); heap_move_nonnull(servicepoint::FadeOutCommand.into())
NonNull::from(Box::leak(result))
} }
/// Set the brightness of all tiles to the same value. /// Set the brightness of all tiles to the same value.
/// ///
/// Returns: a new [servicepoint::Command::Brightness] instance. /// Returns: a new [servicepoint::Command::Brightness] instance.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_brightness( pub unsafe extern "C" fn sp_command_global_brightness(
brightness: Brightness, brightness: Brightness,
) -> NonNull<TypedCommand> { ) -> NonNull<TypedCommand> {
let result = Box::new(GlobalBrightnessCommand::from(brightness).into()); heap_move_nonnull(GlobalBrightnessCommand::from(brightness).into())
NonNull::from(Box::leak(result))
} }
/// Set the brightness of individual tiles in a rectangular area of the display. /// Set the brightness of individual tiles in a rectangular area of the display.
@ -83,20 +81,18 @@ pub unsafe extern "C" fn sp_command_brightness(
/// ///
/// Returns: a new [servicepoint::Command::CharBrightness] instance. /// Returns: a new [servicepoint::Command::CharBrightness] instance.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_char_brightness( pub unsafe extern "C" fn sp_command_brightness_grid(
x: usize, x: usize,
y: usize, y: usize,
grid: NonNull<BrightnessGrid>, grid: NonNull<BrightnessGrid>,
) -> NonNull<TypedCommand> { ) -> NonNull<TypedCommand> {
let grid = unsafe { *Box::from_raw(grid.as_ptr()) }; let grid = unsafe { *Box::from_raw(grid.as_ptr()) };
let result = Box::new( let result = servicepoint::BrightnessGridCommand {
servicepoint::BrightnessGridCommand {
origin: servicepoint::Origin::new(x, y), origin: servicepoint::Origin::new(x, y),
grid, grid,
} }
.into(), .into();
); heap_move_nonnull(result)
NonNull::from(Box::leak(result))
} }
/// Set pixel data starting at the pixel offset on screen. /// Set pixel data starting at the pixel offset on screen.
@ -130,7 +126,7 @@ pub unsafe extern "C" fn sp_command_bitvec(
compression, compression,
} }
.into(); .into();
Box::leak(Box::new(command)) heap_move(command)
} }
/// Show codepage 437 encoded text on the screen. /// Show codepage 437 encoded text on the screen.
@ -145,14 +141,12 @@ pub unsafe extern "C" fn sp_command_cp437_grid(
grid: NonNull<Cp437Grid>, grid: NonNull<Cp437Grid>,
) -> NonNull<TypedCommand> { ) -> NonNull<TypedCommand> {
let grid = *unsafe { Box::from_raw(grid.as_ptr()) }; let grid = *unsafe { Box::from_raw(grid.as_ptr()) };
let result = Box::new( let result = servicepoint::Cp437GridCommand {
servicepoint::Cp437GridCommand {
origin: servicepoint::Origin::new(x, y), origin: servicepoint::Origin::new(x, y),
grid, grid,
} }
.into(), .into();
); heap_move_nonnull(result)
NonNull::from(Box::leak(result))
} }
/// Show UTF-8 encoded text on the screen. /// Show UTF-8 encoded text on the screen.
@ -167,14 +161,12 @@ pub unsafe extern "C" fn sp_command_char_grid(
grid: NonNull<CharGrid>, grid: NonNull<CharGrid>,
) -> NonNull<TypedCommand> { ) -> NonNull<TypedCommand> {
let grid = unsafe { *Box::from_raw(grid.as_ptr()) }; let grid = unsafe { *Box::from_raw(grid.as_ptr()) };
let result = Box::new( let result = servicepoint::CharGridCommand {
servicepoint::CharGridCommand {
origin: servicepoint::Origin::new(x, y), origin: servicepoint::Origin::new(x, y),
grid, grid,
} }
.into(), .into();
); heap_move_nonnull(result)
NonNull::from(Box::leak(result))
} }
/// Sets a window of pixels to the specified values. /// Sets a window of pixels to the specified values.
@ -200,7 +192,7 @@ pub unsafe extern "C" fn sp_command_bitmap(
compression, compression,
} }
.into(); .into();
Box::leak(Box::new(command)) heap_move(command)
} }
/// Deallocates a [TypedCommand]. /// Deallocates a [TypedCommand].
@ -213,5 +205,5 @@ pub unsafe extern "C" fn sp_command_bitmap(
/// ``` /// ```
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_command_free(command: NonNull<TypedCommand>) { pub unsafe extern "C" fn sp_command_free(command: NonNull<TypedCommand>) {
_ = unsafe { Box::from_raw(command.as_ptr()) }; unsafe { heap_drop(command) }
} }

View file

@ -1,3 +1,4 @@
use crate::{heap_drop, heap_move};
use servicepoint::{Connection, Packet, TypedCommand, UdpConnection}; use servicepoint::{Connection, Packet, TypedCommand, UdpConnection};
use std::ffi::{c_char, CStr}; use std::ffi::{c_char, CStr};
use std::net::{Ipv4Addr, SocketAddrV4}; use std::net::{Ipv4Addr, SocketAddrV4};
@ -26,7 +27,7 @@ pub unsafe extern "C" fn sp_udp_open(
Ok(value) => value, Ok(value) => value,
}; };
Box::into_raw(Box::new(connection)) heap_move(connection)
} }
/// Creates a new instance of [UdpConnection]. /// Creates a new instance of [UdpConnection].
@ -42,15 +43,18 @@ pub unsafe extern "C" fn sp_udp_open(
/// ``` /// ```
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_udp_open_ipv4( pub unsafe extern "C" fn sp_udp_open_ipv4(
ip1: u8, ip2: u8, ip3: u8, ip4: u8, ip1: u8,
ip2: u8,
ip3: u8,
ip4: u8,
port: u16, port: u16,
) -> *mut UdpConnection { ) -> *mut UdpConnection {
let addr = SocketAddrV4::new(Ipv4Addr::from( [ip1, ip2, ip3, ip4]), port); let addr = SocketAddrV4::new(Ipv4Addr::from([ip1, ip2, ip3, ip4]), port);
let connection = match UdpConnection::open(addr) { let connection = match UdpConnection::open(addr) {
Err(_) => return std::ptr::null_mut(), Err(_) => return std::ptr::null_mut(),
Ok(value) => value, Ok(value) => value,
}; };
Box::into_raw(Box::new(connection)) heap_move(connection)
} }
/// Sends a [Packet] to the display using the [UdpConnection]. /// Sends a [Packet] to the display using the [UdpConnection].
@ -90,8 +94,6 @@ pub unsafe extern "C" fn sp_udp_send_command(
/// Closes and deallocates a [UdpConnection]. /// Closes and deallocates a [UdpConnection].
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_udp_free( pub unsafe extern "C" fn sp_udp_free(connection: NonNull<UdpConnection>) {
connection: NonNull<UdpConnection>, unsafe { heap_drop(connection) }
) {
_ = unsafe { Box::from_raw(connection.as_ptr()) };
} }

View file

@ -1,4 +1,4 @@
use crate::ByteSlice; use crate::{heap_drop, heap_move, heap_move_nonnull, ByteSlice};
use servicepoint::{Cp437Grid, DataRef, Grid}; use servicepoint::{Cp437Grid, DataRef, Grid};
use std::ptr::NonNull; use std::ptr::NonNull;
@ -10,8 +10,7 @@ pub unsafe extern "C" fn sp_cp437_grid_new(
width: usize, width: usize,
height: usize, height: usize,
) -> NonNull<Cp437Grid> { ) -> NonNull<Cp437Grid> {
let result = Box::new(Cp437Grid::new(width, height)); heap_move_nonnull(Cp437Grid::new(width, height))
NonNull::from(Box::leak(result))
} }
/// Loads a [Cp437Grid] with the specified dimensions from the provided data. /// Loads a [Cp437Grid] with the specified dimensions from the provided data.
@ -24,7 +23,7 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
let data = unsafe { data.as_slice() }; let data = unsafe { data.as_slice() };
let grid = Cp437Grid::load(width, height, data); let grid = Cp437Grid::load(width, height, data);
if let Some(grid) = grid { if let Some(grid) = grid {
Box::leak(Box::new(grid)) heap_move(grid)
} else { } else {
std::ptr::null_mut() std::ptr::null_mut()
} }
@ -35,14 +34,13 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
pub unsafe extern "C" fn sp_cp437_grid_clone( pub unsafe extern "C" fn sp_cp437_grid_clone(
cp437_grid: NonNull<Cp437Grid>, cp437_grid: NonNull<Cp437Grid>,
) -> NonNull<Cp437Grid> { ) -> NonNull<Cp437Grid> {
let result = Box::new(unsafe { cp437_grid.as_ref().clone() }); heap_move_nonnull(unsafe { cp437_grid.as_ref().clone() })
NonNull::from(Box::leak(result))
} }
/// Deallocates a [Cp437Grid]. /// Deallocates a [Cp437Grid].
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: NonNull<Cp437Grid>) { pub unsafe extern "C" fn sp_cp437_grid_free(cp437_grid: NonNull<Cp437Grid>) {
_ = unsafe { Box::from_raw(cp437_grid.as_ptr()) }; unsafe { heap_drop(cp437_grid) }
} }
/// Gets the current value at the specified position. /// Gets the current value at the specified position.

View file

@ -34,6 +34,7 @@ pub use crate::command::*;
pub use crate::connection::*; pub use crate::connection::*;
pub use crate::cp437_grid::*; pub use crate::cp437_grid::*;
pub use crate::packet::*; pub use crate::packet::*;
use std::ptr::NonNull;
mod bitmap; mod bitmap;
mod bitvec; mod bitvec;
@ -49,3 +50,15 @@ use std::time::Duration;
/// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets. /// Actual hardware limit is around 28-29ms/frame. Rounded up for less dropped packets.
pub const SP_FRAME_PACING_MS: u128 = Duration::from_millis(30).as_millis(); pub const SP_FRAME_PACING_MS: u128 = Duration::from_millis(30).as_millis();
pub(crate) fn heap_move<T>(x: T) -> *mut T {
Box::into_raw(Box::new(x))
}
pub(crate) fn heap_move_nonnull<T>(x: T) -> NonNull<T> {
NonNull::from(Box::leak(Box::new(x)))
}
pub(crate) unsafe fn heap_drop<T>(x: NonNull<T>) {
drop(unsafe { Box::from_raw(x.as_ptr()) });
}

View file

@ -1,4 +1,4 @@
use crate::ByteSlice; use crate::{heap_drop, heap_move, heap_move_nonnull, ByteSlice};
use servicepoint::{Header, Packet, TypedCommand}; use servicepoint::{Header, Packet, TypedCommand};
use std::ptr::NonNull; use std::ptr::NonNull;
@ -12,7 +12,7 @@ pub unsafe extern "C" fn sp_packet_from_command(
) -> *mut Packet { ) -> *mut Packet {
let command = unsafe { *Box::from_raw(command.as_ptr()) }; let command = unsafe { *Box::from_raw(command.as_ptr()) };
if let Ok(packet) = command.try_into() { if let Ok(packet) = command.try_into() {
Box::leak(Box::new(packet)) heap_move(packet)
} else { } else {
std::ptr::null_mut() std::ptr::null_mut()
} }
@ -26,7 +26,7 @@ pub unsafe extern "C" fn sp_packet_try_load(data: ByteSlice) -> *mut Packet {
let data = unsafe { data.as_slice() }; let data = unsafe { data.as_slice() };
match servicepoint::Packet::try_from(data) { match servicepoint::Packet::try_from(data) {
Err(_) => std::ptr::null_mut(), Err(_) => std::ptr::null_mut(),
Ok(packet) => Box::into_raw(Box::new(packet)), Ok(packet) => heap_move(packet),
} }
} }
@ -45,8 +45,7 @@ pub unsafe extern "C" fn sp_packet_from_parts(
Vec::from(payload) Vec::from(payload)
}; };
let packet = Box::new(Packet { header, payload }); heap_move_nonnull(Packet { header, payload })
NonNull::from(Box::leak(packet))
} }
/// Returns a pointer to the header field of the provided packet. /// Returns a pointer to the header field of the provided packet.
@ -100,12 +99,11 @@ pub unsafe extern "C" fn sp_packet_serialize_to(
pub unsafe extern "C" fn sp_packet_clone( pub unsafe extern "C" fn sp_packet_clone(
packet: NonNull<Packet>, packet: NonNull<Packet>,
) -> NonNull<Packet> { ) -> NonNull<Packet> {
let result = Box::new(unsafe { packet.as_ref().clone() }); heap_move_nonnull(unsafe { packet.as_ref().clone() })
NonNull::from(Box::leak(result))
} }
/// Deallocates a [Packet]. /// Deallocates a [Packet].
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp_packet_free(packet: NonNull<Packet>) { pub unsafe extern "C" fn sp_packet_free(packet: NonNull<Packet>) {
_ = unsafe { Box::from_raw(packet.as_ptr()) } unsafe { heap_drop(packet) }
} }