• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2023 Huawei Device Co., Ltd.
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  *     http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14 
15 /*!
16  * Ashmem provides interfaces for operating shared memory.
17  */
18 
19 use cxx::SharedPtr;
20 use std::ffi::{c_char, CString};
21 
22 /// Memory protection corresponding to PROT_NONE in C code.
23 pub const PROT_NONE: i32 = 0;
24 /// Memory protection corresponding to PROT_READ in C code.
25 pub const PROT_READ: i32 = 1;
26 /// Memory protection corresponding to PROT_WRITE in C code.
27 pub const PROT_WRITE: i32 = 2;
28 /// Memory protection corresponding to PROT_EXEC in C code.
29 pub const PROT_EXEC: i32 = 4;
30 
31 #[cxx::bridge(namespace = "OHOS")]
32 /// Module Ashmem::ffi. Includes interfaces which will call c++ counterparts via FFI.
33 pub mod ffi {
34     #[allow(dead_code)]
35     unsafe extern "C++" {
36         include!("commonlibrary/c_utils/base/include/ashmem.h");
37 
38         // global function
39         /**
40          * Create an C++ Ashmem object managed by std::shared_ptr.
41          * # Safety
42          * Requires C-style string as parameter to specify the name of Ashmem.
43          */
CreateAshmemStd(name: *const c_char, size: i32) -> SharedPtr<Ashmem>44         pub unsafe fn CreateAshmemStd(name: *const c_char, size: i32) -> SharedPtr<Ashmem>;
45 
46         /// Set protection flag of created ashmem specified by FD.
AshmemSetProt(fd: i32, prot: i32) -> i3247         pub fn AshmemSetProt(fd: i32, prot: i32) -> i32;
48 
49         /// Get size of created ashmem specified by FD.
AshmemGetSize(fd: i32) -> i3250         pub fn AshmemGetSize(fd: i32) -> i32;
51 
52         /// C++ void type.
53         pub type c_void;
54         /**
55          * Cast c_char to c_void
56          * # Safety
57          */
AsVoidPtr(inPtr: *const c_char) -> *const c_void58         pub unsafe fn AsVoidPtr(inPtr: *const c_char) -> *const c_void;
59 
60         /**
61          * Cast c_char to c_void
62          * # Safety
63          */
AsCharPtr(inPtr: *const c_void) -> *const c_char64         pub unsafe fn AsCharPtr(inPtr: *const c_void) -> *const c_char;
65 
66         /// C++ Ashmem class.
67         pub type Ashmem;
68 
69         // member function
70         /// Close inner ashmem.
CloseAshmem(self: &Ashmem) -> ()71         pub fn CloseAshmem(self: &Ashmem) -> ();
72 
73         /// Map inner ashmem to user-space memory with specified map type.
MapAshmem(self: &Ashmem, mapType: i32) -> bool74         pub fn MapAshmem(self: &Ashmem, mapType: i32) -> bool;
75 
76         /// Map inner ashmem to user-space memory with read-write type.
MapReadAndWriteAshmem(self: &Ashmem) -> bool77         pub fn MapReadAndWriteAshmem(self: &Ashmem) -> bool;
78 
79         /// Map inner ashmem to user-space memory with read-only type.
MapReadOnlyAshmem(self: &Ashmem) -> bool80         pub fn MapReadOnlyAshmem(self: &Ashmem) -> bool;
81 
82         /// UnMap inner ashmem.
UnmapAshmem(self: &Ashmem) -> ()83         pub fn UnmapAshmem(self: &Ashmem) -> ();
84 
85         /// Set protection flag of inner ashmem.
SetProtection(self: &Ashmem, protType: i32) -> bool86         pub fn SetProtection(self: &Ashmem, protType: i32) -> bool;
87 
88         /// Get protection flag of inner ashmem.
GetProtection(self: &Ashmem) -> i3289         pub fn GetProtection(self: &Ashmem) -> i32;
90 
91         /// Get size of inner ashmem.
GetAshmemSize(self: &Ashmem) -> i3292         pub fn GetAshmemSize(self: &Ashmem) -> i32;
93 
94         /**
95          * Write data to inner ashmem.
96          * # Safety
97          * Requires a C++-style void pointer as parameter to indicates data expected to be written.
98          */
WriteToAshmem(self: &Ashmem, data: *const c_void, size: i32, offset: i32) -> bool99         pub unsafe fn WriteToAshmem(self: &Ashmem, data: *const c_void, size: i32, offset: i32) -> bool;
100 
101         /**
102          * Read data from inner ashmem.
103          * # Safety
104          * Returns a C++-style void pointer to indicates data expected to be read.
105          */
ReadFromAshmem(self: &Ashmem, size: i32, offset: i32) -> *const c_void106         pub unsafe fn ReadFromAshmem(self: &Ashmem, size: i32, offset: i32) -> *const c_void;
107 
108         /// Get FD of inner ashmem.
GetAshmemFd(self: &Ashmem) -> i32109         pub fn GetAshmemFd(self: &Ashmem) -> i32;
110     }
111 }
112 
113 /// Ashmem in rust.
114 #[allow(dead_code)]
115 pub struct Ashmem {
116     c_ashmem: SharedPtr<ffi::Ashmem>
117 }
118 
119 /// Ashmem implementation.
120 #[allow(dead_code)]
121 impl Ashmem {
122     /// Create an ashmem object.
new(c_ashmem: SharedPtr<ffi::Ashmem>) -> Ashmem123     pub fn new(c_ashmem: SharedPtr<ffi::Ashmem>) -> Ashmem {
124         Ashmem {
125             c_ashmem
126         }
127     }
128 
129     /// Get corresponding fd.
get_ashmem_fd(&self) -> i32130     pub fn get_ashmem_fd(&self) -> i32 {
131         self.c_ashmem.GetAshmemFd()
132     }
133 
134     /// Get size of the shared memory.
get_ashmem_size(&self) -> i32135     pub fn get_ashmem_size(&self) -> i32 {
136         self.c_ashmem.GetAshmemSize()
137     }
138 
139     /// Get memory protection flags.
get_protection(&self) -> i32140     pub fn get_protection(&self) -> i32 {
141         self.c_ashmem.GetProtection()
142     }
143 
144     /// Set memory protection flags.
set_protection(&self, prot_type: i32) -> bool145     pub fn set_protection(&self, prot_type: i32) -> bool {
146         self.c_ashmem.SetProtection(prot_type)
147     }
148 
149     /// Map the shared memory to user-space.
map_ashmem(&self, prot_type: i32) -> bool150     pub fn map_ashmem(&self, prot_type: i32) -> bool {
151         self.c_ashmem.MapAshmem(prot_type)
152     }
153 
154     /// Map ashmem in read&write mode.
map_read_write_ashmem(&self) -> bool155     pub fn map_read_write_ashmem(&self) -> bool {
156         self.c_ashmem.MapReadAndWriteAshmem()
157     }
158 
159     /// Map ashmem in read-only mode.
map_read_only_ashmem(&self) -> bool160     pub fn map_read_only_ashmem(&self) -> bool {
161         self.c_ashmem.MapReadOnlyAshmem()
162     }
163 
164     /// Unmap ashmem.
unmap_ashmem(&self)165     pub fn unmap_ashmem(&self) {
166         self.c_ashmem.UnmapAshmem()
167     }
168 
169     /// Close ashmem.
close_ashmem(&self)170     pub fn close_ashmem(&self) {
171         self.c_ashmem.CloseAshmem()
172     }
173 
174     /**
175      * Write data to ashmem.
176      * # Safety
177      * Requires c-style data(*const c_char)
178      */
179     /// # Safety
write_to_ashmem(&self, data: *const c_char, size: i32, offset: i32) -> bool180     pub unsafe fn write_to_ashmem(&self, data: *const c_char, size: i32, offset: i32) -> bool {
181         let c_void_ptr = ffi::AsVoidPtr(data);
182         self.c_ashmem.WriteToAshmem(c_void_ptr, size, offset)
183     }
184 
185     /**
186      * Read data from ashmem.
187      * # Safety
188      * Returns c-style data(*const c_char)
189      */
190     /// # Safety
read_from_ashmem(&self, size: i32, offset: i32) -> *const c_char191     pub unsafe fn read_from_ashmem(&self, size: i32, offset: i32) -> *const c_char {
192         let c_void_ptr = self.c_ashmem.ReadFromAshmem(size, offset);
193         ffi::AsCharPtr(c_void_ptr)
194     }
195 }
196 
197 /**
198  * Create Ashmem struct in Rust, which holds a refrence to c++ Ashmem object.
199  * # Safety
200  * Transmits c-style string of `name`.
201  */
202 #[allow(dead_code)]
create_ashmem_instance(name: &str, size: i32) -> Option<Ashmem>203 pub unsafe fn create_ashmem_instance(name: &str, size: i32) -> Option<Ashmem> {
204     let c_name = CString::new(name).expect("CString::new Failed!");
205     let name_ptr = c_name.as_ptr();
206     let c_ashmem_ptr = ffi::CreateAshmemStd(name_ptr, size);
207 
208     if c_ashmem_ptr.is_null() {
209         None
210     } else {
211         Some(Ashmem::new(c_ashmem_ptr))
212     }
213 }