1/*
2 * Copyright (c) 2022-2023 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#include "dcamera_buffer_handle.h"
17
18#include <cerrno>
19#include <cstddef>
20#include <string>
21#include <sys/mman.h>
22
23#include "distributed_hardware_log.h"
24
25namespace OHOS {
26namespace DistributedHardware {
27void* DCameraMemoryMap(const BufferHandle *buffer)
28{
29    if (buffer == nullptr) {
30        DHLOGE("mmap the buffer handle is NULL");
31        return nullptr;
32    }
33
34    void* virAddr = mmap(NULL, buffer->size, PROT_READ | PROT_WRITE, MAP_SHARED, buffer->fd, 0);
35    if (virAddr == MAP_FAILED) {
36        DHLOGE("mmap failed errno %{public}s, fd : %{public}d", strerror(errno), buffer->fd);
37        return nullptr;
38    }
39    return virAddr;
40}
41
42void DCameraMemoryUnmap(BufferHandle *buffer)
43{
44    if (buffer == nullptr) {
45        DHLOGE("unmmap the buffer handle is NULL");
46        return;
47    }
48    if (buffer->virAddr != nullptr) {
49        int ret = munmap(buffer->virAddr, buffer->size);
50        if (ret != 0) {
51            DHLOGE("munmap failed err: %{public}s", strerror(errno));
52        }
53    }
54    buffer->virAddr = nullptr;
55}
56} // namespace DistributedHardware
57} // namespace OHOS