1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef UTILS_BASE_ASHMEM_H 17 #define UTILS_BASE_ASHMEM_H 18 19 #include <stddef.h> 20 #include <linux/ashmem.h> 21 #include "refbase.h" 22 #include "parcel.h" 23 24 namespace OHOS { 25 int AshmemCreate(const char *name, size_t size); 26 int AshmemSetProt(int fd, int prot); 27 int AshmemGetSize(int fd); 28 29 class Ashmem : public virtual RefBase { 30 public: 31 static sptr<Ashmem> CreateAshmem(const char *name, int32_t size); 32 void CloseAshmem(); 33 bool MapAshmem(int mapType); 34 bool MapReadAndWriteAshmem(); 35 bool MapReadOnlyAshmem(); 36 void UnmapAshmem(); 37 bool SetProtection(int protectionType); 38 int GetProtection(); 39 int32_t GetAshmemSize(); 40 bool WriteToAshmem(const void *data, int32_t size, int32_t offset); 41 const void *ReadFromAshmem(int32_t size, int32_t offset); 42 Ashmem(int fd, int32_t size); 43 ~Ashmem(); GetAshmemFd()44 int GetAshmemFd() const 45 { 46 return memoryFd_; 47 }; 48 49 private: 50 int memoryFd_; 51 int32_t memorySize_; 52 int flag_; 53 void *startAddr_; 54 bool CheckValid(int32_t size, int32_t offset, int cmd); 55 }; 56 } // namespace OHOS 57 #endif 58