move heap functions to own mod
This commit is contained in:
		
							parent
							
								
									cf6e6385ec
								
							
						
					
					
						commit
						626a887480
					
				
					 17 changed files with 82 additions and 65 deletions
				
			
		|  | @ -1,4 +1,4 @@ | ||||||
| use crate::{ | use crate::mem::{ | ||||||
|     heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, |     heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, | ||||||
| }; | }; | ||||||
| use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; | use servicepoint::{Bitmap, BitmapCommand, CompressionCode, Origin, Packet}; | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| use crate::{ | use crate::mem::{ | ||||||
|     heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, |     heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, | ||||||
| }; | }; | ||||||
| use servicepoint::{ | use servicepoint::{ | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| use crate::{ | use crate::mem::{ | ||||||
|     heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, |     heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, | ||||||
| }; | }; | ||||||
| use servicepoint::{ | use servicepoint::{ | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| use crate::{heap_drop, heap_move_nonnull}; | use crate::mem::{heap_drop, heap_move_nonnull}; | ||||||
| use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; | use servicepoint::{ClearCommand, FadeOutCommand, HardResetCommand}; | ||||||
| use std::ptr::NonNull; | use std::ptr::NonNull; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| use crate::{ | use crate::mem::{ | ||||||
|     heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, |     heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, | ||||||
| }; | }; | ||||||
| use servicepoint::{BitmapCommand, CharGrid, CharGridCommand, Origin, Packet}; | use servicepoint::{BitmapCommand, CharGrid, CharGridCommand, Origin, Packet}; | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| use crate::{ | use crate::mem::{ | ||||||
|     heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, |     heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, | ||||||
| }; | }; | ||||||
| use servicepoint::{ | use servicepoint::{ | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| use crate::{ | use crate::mem::{ | ||||||
|     heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, |     heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, | ||||||
| }; | }; | ||||||
| use servicepoint::{ | use servicepoint::{ | ||||||
|  | @ -65,11 +65,11 @@ impl SPCommand { | ||||||
|     }; |     }; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /// Tries to turn a [Packet] into a [TypedCommand].
 | /// Tries to turn a [Packet] into a [SPCommand].
 | ||||||
| ///
 | ///
 | ||||||
| /// The packet is deallocated in the process.
 | /// The packet is dropped in the process.
 | ||||||
| ///
 | ///
 | ||||||
| /// Returns: pointer to new [TypedCommand] instance or NULL if parsing failed.
 | /// Returns: pointer to new [SPCommand] instance or NULL if parsing failed.
 | ||||||
| #[no_mangle] | #[no_mangle] | ||||||
| pub unsafe extern "C" fn sp_cmd_generic_try_from_packet( | pub unsafe extern "C" fn sp_cmd_generic_try_from_packet( | ||||||
|     packet: NonNull<Packet>, |     packet: NonNull<Packet>, | ||||||
|  | @ -142,9 +142,9 @@ pub unsafe extern "C" fn sp_cmd_generic_try_from_packet( | ||||||
|     })) |     })) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /// Clones a [SPCommand] instance.
 | /// Clones an [SPCommand] instance.
 | ||||||
| ///
 | ///
 | ||||||
| /// returns: new [SPCommand] instance.
 | /// returns: a new [SPCommand] instance.
 | ||||||
| #[no_mangle] | #[no_mangle] | ||||||
| pub unsafe extern "C" fn sp_cmd_generic_clone(command: SPCommand) -> SPCommand { | pub unsafe extern "C" fn sp_cmd_generic_clone(command: SPCommand) -> SPCommand { | ||||||
|     unsafe { |     unsafe { | ||||||
|  | @ -217,12 +217,12 @@ pub unsafe extern "C" fn sp_cmd_generic_clone(command: SPCommand) -> SPCommand { | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /// Deallocates a [SPCommand].
 | /// Deallocates an [SPCommand].
 | ||||||
| ///
 | ///
 | ||||||
| /// # Examples
 | /// # Examples
 | ||||||
| ///
 | ///
 | ||||||
| /// ```C
 | /// ```C
 | ||||||
| /// TypedCommand c = sp_command_clear();
 | /// SPCommand c = sp_cmd_clear_into_generic(sp_cmd_clear_new());
 | ||||||
| /// sp_command_free(c);
 | /// sp_command_free(c);
 | ||||||
| /// ```
 | /// ```
 | ||||||
| #[no_mangle] | #[no_mangle] | ||||||
|  | @ -248,10 +248,10 @@ pub unsafe extern "C" fn sp_cmd_generic_free(command: SPCommand) { | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /// Turns a [TypedCommand] into a [Packet].
 | /// Tries to turn a [SPCommand] into a [Packet].
 | ||||||
| /// The [TypedCommand] gets consumed.
 | /// The [SPCommand] gets consumed.
 | ||||||
| ///
 | ///
 | ||||||
| /// Returns NULL in case of an error.
 | /// Returns tag [CommandTag::Invalid] in case of an error.
 | ||||||
| #[no_mangle] | #[no_mangle] | ||||||
| pub unsafe extern "C" fn sp_cmd_generic_into_packet( | pub unsafe extern "C" fn sp_cmd_generic_into_packet( | ||||||
|     command: SPCommand, |     command: SPCommand, | ||||||
|  |  | ||||||
|  | @ -1,4 +1,4 @@ | ||||||
| use crate::{ | use crate::mem::{ | ||||||
|     heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, |     heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, | ||||||
| }; | }; | ||||||
| use servicepoint::{ | use servicepoint::{ | ||||||
|  |  | ||||||
|  | @ -1,6 +1,9 @@ | ||||||
| use crate::{ | use crate::{ | ||||||
|     containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, |     containers::ByteSlice, | ||||||
|     heap_move_ok, heap_move_some, heap_remove, |     mem::{ | ||||||
|  |         heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, | ||||||
|  |         heap_remove, | ||||||
|  |     }, | ||||||
| }; | }; | ||||||
| use servicepoint::{ | use servicepoint::{ | ||||||
|     Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid, |     Bitmap, BitmapCommand, CompressionCode, DataRef, DisplayBitVec, Grid, | ||||||
|  |  | ||||||
|  | @ -1,6 +1,8 @@ | ||||||
| use crate::{ | use crate::{ | ||||||
|     containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, |     containers::ByteSlice, | ||||||
|     heap_move_ok, heap_remove, |     mem::{ | ||||||
|  |         heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, | ||||||
|  |     }, | ||||||
| }; | }; | ||||||
| use servicepoint::{ | use servicepoint::{ | ||||||
|     BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, |     BinaryOperation, BitVecCommand, CompressionCode, DisplayBitVec, Packet, | ||||||
|  |  | ||||||
|  | @ -1,6 +1,9 @@ | ||||||
| use crate::{ | use crate::{ | ||||||
|     containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, |     containers::ByteSlice, | ||||||
|     heap_move_ok, heap_move_some, heap_remove, |     mem::{ | ||||||
|  |         heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, | ||||||
|  |         heap_remove, | ||||||
|  |     }, | ||||||
| }; | }; | ||||||
| use servicepoint::{ | use servicepoint::{ | ||||||
|     Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, |     Brightness, BrightnessGrid, BrightnessGridCommand, ByteGrid, DataRef, Grid, | ||||||
|  |  | ||||||
|  | @ -1,6 +1,8 @@ | ||||||
| use crate::{ | use crate::{ | ||||||
|     containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, |     containers::ByteSlice, | ||||||
|     heap_move_ok, heap_remove, |     mem::{ | ||||||
|  |         heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_remove, | ||||||
|  |     }, | ||||||
| }; | }; | ||||||
| use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; | use servicepoint::{CharGrid, CharGridCommand, Grid, Origin, Packet}; | ||||||
| use std::ptr::NonNull; | use std::ptr::NonNull; | ||||||
|  |  | ||||||
|  | @ -1,6 +1,9 @@ | ||||||
| use crate::{ | use crate::{ | ||||||
|     containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, |     containers::ByteSlice, | ||||||
|     heap_move_ok, heap_move_some, heap_remove, |     mem::{ | ||||||
|  |         heap_clone, heap_drop, heap_move_nonnull, heap_move_ok, heap_move_some, | ||||||
|  |         heap_remove, | ||||||
|  |     }, | ||||||
| }; | }; | ||||||
| use servicepoint::{ | use servicepoint::{ | ||||||
|     Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, |     Cp437Grid, Cp437GridCommand, DataRef, Grid, Origin, Packet, | ||||||
|  |  | ||||||
							
								
								
									
										31
									
								
								src/lib.rs
									
										
									
									
									
								
							
							
						
						
									
										31
									
								
								src/lib.rs
									
										
									
									
									
								
							|  | @ -25,10 +25,9 @@ | ||||||
| //! }
 | //! }
 | ||||||
| //! ```
 | //! ```
 | ||||||
| 
 | 
 | ||||||
| use std::ptr::NonNull; |  | ||||||
| 
 |  | ||||||
| pub mod commands; | pub mod commands; | ||||||
| pub mod containers; | pub mod containers; | ||||||
|  | pub mod mem; | ||||||
| pub mod packet; | pub mod packet; | ||||||
| pub mod udp; | pub mod udp; | ||||||
| 
 | 
 | ||||||
|  | @ -37,34 +36,6 @@ 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) fn heap_move_ok<T, E>(x: Result<T, E>) -> *mut T { |  | ||||||
|     x.map(|x| heap_move(x)).unwrap_or(std::ptr::null_mut()) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| pub(crate) fn heap_move_some<T>(x: Option<T>) -> *mut T { |  | ||||||
|     x.map(|x| heap_move(x)).unwrap_or(std::ptr::null_mut()) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| pub(crate) unsafe fn heap_drop<T>(x: NonNull<T>) { |  | ||||||
|     drop(unsafe { heap_remove(x) }); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| pub(crate) unsafe fn heap_remove<T>(x: NonNull<T>) -> T { |  | ||||||
|     unsafe { *Box::from_raw(x.as_ptr()) } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| unsafe fn heap_clone<T: Clone>(source: NonNull<T>) -> NonNull<T> { |  | ||||||
|     heap_move_nonnull(unsafe { source.as_ref().clone() }) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| /// This is a type only used by cbindgen to have a type for pointers.
 | /// This is a type only used by cbindgen to have a type for pointers.
 | ||||||
| pub struct UdpSocket; | pub struct UdpSocket; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										29
									
								
								src/mem.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/mem.rs
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,29 @@ | ||||||
|  | use std::ptr::NonNull; | ||||||
|  | 
 | ||||||
|  | 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) fn heap_move_ok<T, E>(x: Result<T, E>) -> *mut T { | ||||||
|  |     x.map(|x| heap_move(x)).unwrap_or(std::ptr::null_mut()) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub(crate) fn heap_move_some<T>(x: Option<T>) -> *mut T { | ||||||
|  |     x.map(|x| heap_move(x)).unwrap_or(std::ptr::null_mut()) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub(crate) unsafe fn heap_drop<T>(x: NonNull<T>) { | ||||||
|  |     drop(unsafe { heap_remove(x) }); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub(crate) unsafe fn heap_remove<T>(x: NonNull<T>) -> T { | ||||||
|  |     unsafe { *Box::from_raw(x.as_ptr()) } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub(crate) unsafe fn heap_clone<T: Clone>(source: NonNull<T>) -> NonNull<T> { | ||||||
|  |     heap_move_nonnull(unsafe { source.as_ref().clone() }) | ||||||
|  | } | ||||||
|  | @ -1,6 +1,6 @@ | ||||||
| use crate::{ | use crate::{ | ||||||
|     containers::ByteSlice, heap_clone, heap_drop, heap_move_nonnull, |     containers::ByteSlice, | ||||||
|     heap_move_ok, |     mem::{heap_clone, heap_drop, heap_move_nonnull, heap_move_ok}, | ||||||
| }; | }; | ||||||
| use servicepoint::{CommandCode, Header, Packet}; | use servicepoint::{CommandCode, Header, Packet}; | ||||||
| use std::ptr::NonNull; | use std::ptr::NonNull; | ||||||
|  |  | ||||||
							
								
								
									
										14
									
								
								src/udp.rs
									
										
									
									
									
								
							
							
						
						
									
										14
									
								
								src/udp.rs
									
										
									
									
									
								
							|  | @ -1,9 +1,13 @@ | ||||||
| use crate::commands::{CommandTag, SPCommand}; | use crate::{ | ||||||
| use crate::{heap_drop, heap_move_ok, heap_remove}; |     commands::{CommandTag, SPCommand}, | ||||||
|  |     mem::{heap_drop, heap_move_ok, heap_remove}, | ||||||
|  | }; | ||||||
| use servicepoint::{Header, Packet, UdpSocketExt}; | use servicepoint::{Header, Packet, UdpSocketExt}; | ||||||
| use std::ffi::{c_char, CStr}; | use std::{ | ||||||
| use std::net::{Ipv4Addr, SocketAddrV4, UdpSocket}; |     ffi::{c_char, CStr}, | ||||||
| use std::ptr::NonNull; |     net::{Ipv4Addr, SocketAddrV4, UdpSocket}, | ||||||
|  |     ptr::NonNull, | ||||||
|  | }; | ||||||
| 
 | 
 | ||||||
| /// Creates a new instance of [UdpSocket].
 | /// Creates a new instance of [UdpSocket].
 | ||||||
| ///
 | ///
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vinzenz Schroeter
						Vinzenz Schroeter