@@ -22,36 +22,37 @@ pub fn unlikely_last_error() -> ErrorCode {
2222}
2323
2424#[ inline]
25- fn noop ( _: * mut c_void ) {
25+ fn noop ( _: * mut c_void , _ : usize ) {
2626}
2727
2828#[ inline]
29- fn free_rust_mem ( data : * mut c_void ) {
29+ fn free_rust_mem ( data : * mut c_void , size : usize ) {
30+ let layout = alloc:: alloc:: Layout :: array :: < u8 > ( size) . unwrap_or ( BYTES_LAYOUT ) ;
3031 unsafe {
31- alloc:: alloc:: dealloc ( data as _ , BYTES_LAYOUT )
32+ alloc:: alloc:: dealloc ( data as _ , layout )
3233 }
3334}
3435
3536#[ inline]
36- fn unlock_data ( data : * mut c_void ) {
37+ fn unlock_data ( data : * mut c_void , _ : usize ) {
3738 unsafe {
3839 sys:: GlobalUnlock ( data) ;
3940 }
4041}
4142
4243#[ inline]
43- fn free_global_mem ( data : * mut c_void ) {
44+ fn free_global_mem ( data : * mut c_void , _ : usize ) {
4445 unsafe {
4546 sys:: GlobalFree ( data) ;
4647 }
4748}
4849
49- pub struct Scope < T : Copy > ( pub T , pub fn ( T ) ) ;
50+ pub struct Scope < T : Copy > ( pub T , pub fn ( T , usize ) , pub usize ) ;
5051
5152impl < T : Copy > Drop for Scope < T > {
5253 #[ inline( always) ]
5354 fn drop ( & mut self ) {
54- ( self . 1 ) ( self . 0 )
55+ ( self . 1 ) ( self . 0 , self . 2 )
5556 }
5657}
5758
@@ -67,7 +68,7 @@ impl RawMem {
6768 if mem. is_null ( ) {
6869 Err ( unlikely_last_error ( ) )
6970 } else {
70- Ok ( Self ( Scope ( mem as _ , free_rust_mem) ) )
71+ Ok ( Self ( Scope ( mem as _ , free_rust_mem, size ) ) )
7172 }
7273 }
7374
@@ -78,21 +79,26 @@ impl RawMem {
7879 if mem. is_null ( ) {
7980 Err ( unlikely_last_error ( ) )
8081 } else {
81- Ok ( Self ( Scope ( mem, free_global_mem) ) )
82+ Ok ( Self ( Scope ( mem, free_global_mem, size ) ) )
8283 }
8384 }
8485 }
8586
8687 #[ inline( always) ]
8788 pub fn from_borrowed ( ptr : ptr:: NonNull < c_void > ) -> Self {
88- Self ( Scope ( ptr. as_ptr ( ) , noop) )
89+ Self ( Scope ( ptr. as_ptr ( ) , noop, 0 ) )
8990 }
9091
9192 #[ inline( always) ]
9293 pub fn get ( & self ) -> * mut c_void {
9394 ( self . 0 ) . 0
9495 }
9596
97+ #[ inline( always) ]
98+ pub fn size ( & self ) -> usize {
99+ ( self . 0 ) . 2
100+ }
101+
96102 #[ inline( always) ]
97103 pub fn release ( self ) {
98104 mem:: forget ( self )
@@ -104,7 +110,7 @@ impl RawMem {
104110 } ;
105111
106112 match ptr:: NonNull :: new ( ptr) {
107- Some ( ptr) => Ok ( ( ptr, Scope ( self . get ( ) , unlock_data) ) ) ,
113+ Some ( ptr) => Ok ( ( ptr, Scope ( self . get ( ) , unlock_data, self . size ( ) ) ) ) ,
108114 None => Err ( ErrorCode :: last_system ( ) ) ,
109115 }
110116 }
0 commit comments