1 /*
2 * Copyright (c) 2022 Shenzhen Kaihong DID 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 #include "share_mem.h"
17 #include <sys/mman.h>
18 #include "ashmem_wrapper.h"
19 #include "hdf_log.h"
20
21 #define HDF_LOG_TAG codec_hdi_share_mem
22
CreateFdShareMemory(ShareMemory * shareMemory)23 int32_t CreateFdShareMemory(ShareMemory *shareMemory)
24 {
25 if (shareMemory == NULL || shareMemory->size <= 0) {
26 HDF_LOGE("%{public}s invalid param", __func__);
27 return HDF_FAILURE;
28 }
29 int32_t fd = AshmemCreateFd("", shareMemory->size);
30 if (fd < 0) {
31 HDF_LOGE("%{public}s invalid fd", __func__);
32 return HDF_FAILURE;
33 }
34 shareMemory->fd = fd;
35 shareMemory->virAddr = MapAshmemFd(fd, shareMemory->size);
36 if ((void*)shareMemory->virAddr == MAP_FAILED) {
37 HDF_LOGE("%{public}s: Failed to map fd!", __func__);
38 shareMemory->virAddr = NULL;
39 return HDF_FAILURE;
40 }
41
42 return HDF_SUCCESS;
43 }
44
OpenFdShareMemory(ShareMemory * shareMemory)45 int32_t OpenFdShareMemory(ShareMemory *shareMemory)
46 {
47 if (shareMemory == NULL || shareMemory->fd < 0) {
48 HDF_LOGE("%{public}s invalid param", __func__);
49 return HDF_FAILURE;
50 }
51 shareMemory->virAddr = MapAshmemFd(shareMemory->fd, shareMemory->size);
52 if ((void*)shareMemory->virAddr == MAP_FAILED) {
53 HDF_LOGE("%{public}s: Failed to map fd!", __func__);
54 shareMemory->virAddr = NULL;
55 return HDF_FAILURE;
56 }
57
58 return HDF_SUCCESS;
59 }
60
ReleaseFdShareMemory(ShareMemory * shareMemory)61 int32_t ReleaseFdShareMemory(ShareMemory *shareMemory)
62 {
63 if (shareMemory == NULL || shareMemory->virAddr == NULL) {
64 HDF_LOGE("%{public}s invalid param", __func__);
65 return HDF_FAILURE;
66 }
67 UnmapAshmemFd(shareMemory->virAddr, shareMemory->size);
68 CloseAshmemFd(shareMemory->fd);
69 return HDF_SUCCESS;
70 }
71
72