| 132 | #endif |
| 133 | |
| 134 | int ASharedMemory_create(const char *name, size_t size) { |
| 135 | #ifndef MMKV_OHOS |
| 136 | if (g_android_api >= __ANDROID_API_O__ || g_android_api >= __ANDROID_API_M__) { |
| 137 | static auto handle = loadLibrary(); |
| 138 | static AShmem_create_t funcPtr = |
| 139 | (handle != nullptr) ? reinterpret_cast<AShmem_create_t>(dlsym(handle, "ASharedMemory_create")) : nullptr; |
| 140 | if (funcPtr) { |
| 141 | int fd = funcPtr(name, size); |
| 142 | if (fd < 0) { |
| 143 | MMKVError("fail to ASharedMemory_create %s with size %zu, errno:%s", name, size, strerror(errno)); |
| 144 | } else { |
| 145 | MMKVInfo("ASharedMemory_create %s with size %zu, fd:%d", name, size, fd); |
| 146 | return fd; |
| 147 | } |
| 148 | } else if (g_android_api >= __ANDROID_API_O__) { |
| 149 | MMKVWarning("fail to locate ASharedMemory_create() from loading libandroid.so"); |
| 150 | } |
| 151 | |
| 152 | static AShmem_create_t regionFuncPtr = |
| 153 | (handle != nullptr) ? reinterpret_cast<AShmem_create_t>(dlsym(handle, "ashmem_create_region")) : nullptr; |
| 154 | if (regionFuncPtr) { |
| 155 | int fd = regionFuncPtr(name, size); |
| 156 | if (fd < 0) { |
| 157 | MMKVError("fail to ashmem_create_region %s with size %zu, errno:%s", name, size, strerror(errno)); |
| 158 | } else { |
| 159 | MMKVInfo("ashmem_create_region %s with size %zu, fd:%d", name, size, fd); |
| 160 | return fd; |
| 161 | } |
| 162 | } else { |
| 163 | MMKVWarning("fail to locate ashmem_create_region() from loading libandroid.so"); |
| 164 | } |
| 165 | } |
| 166 | #endif |
| 167 | int fd = open(ASHMEM_NAME_DEF, O_RDWR | O_CLOEXEC); |
| 168 | if (fd < 0) { |
| 169 | MMKVError("fail to open ashmem:%s, %s", name, strerror(errno)); |
| 170 | } else { |
| 171 | if (ioctl(fd, ASHMEM_SET_NAME, name) != 0) { |
| 172 | MMKVError("fail to set ashmem name:%s, %s", name, strerror(errno)); |
| 173 | } else if (ioctl(fd, ASHMEM_SET_SIZE, size) != 0) { |
| 174 | MMKVError("fail to set ashmem:%s, size %zu, %s", name, size, strerror(errno)); |
| 175 | } |
| 176 | } |
| 177 | return fd; |
| 178 | } |
| 179 | |
| 180 | size_t ASharedMemory_getSize(int fd) { |
| 181 | size_t size = 0; |