• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "hdi_buffer_handle_util.h"
17 #include <unistd.h>
18 #include "hdf_log.h"
19 #include "osal_mem.h"
20 #include "securec.h"
21 
AllocateBufferHandle(uint32_t reserveFds,uint32_t reserveInts)22 BufferHandle *AllocateBufferHandle(uint32_t reserveFds, uint32_t reserveInts)
23 {
24     size_t handleSize = sizeof(BufferHandle) + (sizeof(int32_t) * (reserveFds + reserveInts));
25     BufferHandle *handle = (BufferHandle *)(OsalMemCalloc(handleSize));
26     if (handle != NULL) {
27         handle->fd = -1;
28         handle->reserveFds = reserveFds;
29         handle->reserveInts = reserveInts;
30         for (uint32_t i = 0; i < reserveFds; i++) {
31             handle->reserve[i] = -1;
32         }
33     } else {
34         HDF_LOGE("BufferHandle malloc %zu failed", handleSize);
35     }
36     return handle;
37 }
38 
CloneBufferHandle(const BufferHandle * other)39 BufferHandle *CloneBufferHandle(const BufferHandle *other)
40 {
41     if (other == NULL) {
42         HDF_LOGW("%{public}s handle is NULL", __func__);
43         return NULL;
44     }
45 
46     BufferHandle *handle = AllocateBufferHandle(other->reserveFds, other->reserveInts);
47     if (handle == NULL) {
48         HDF_LOGW("%{public}s AllocateBufferHandle failed, handle is NULL", __func__);
49         return NULL;
50     }
51 
52     if (other->fd == -1) {
53         handle->fd = other->fd;
54     } else {
55         handle->fd = dup(other->fd);
56         if (handle->fd == -1) {
57             HDF_LOGE("CloneBufferHandle dup failed");
58             FreeBufferHandle(handle);
59             return NULL;
60         }
61     }
62     handle->width = other->width;
63     handle->stride = other->stride;
64     handle->height = other->height;
65     handle->size = other->size;
66     handle->format = other->format;
67     handle->usage = other->usage;
68     handle->phyAddr = other->phyAddr;
69     handle->key = other->key;
70 
71     for (uint32_t i = 0; i < handle->reserveFds; i++) {
72         handle->reserve[i] = dup(other->reserve[i]);
73         if (handle->reserve[i] == -1) {
74             HDF_LOGE("CloneBufferHandle dup reserveFds failed");
75             FreeBufferHandle(handle);
76             return NULL;
77         }
78     }
79 
80     if (other->reserveInts == 0) {
81         return handle;
82     }
83 
84     if (memcpy_s(&handle->reserve[handle->reserveFds], sizeof(int32_t) * handle->reserveInts,
85             &other->reserve[other->reserveFds], sizeof(int32_t) * other->reserveInts) != EOK) {
86         HDF_LOGE("CloneBufferHandle memcpy_s failed");
87         FreeBufferHandle(handle);
88         return NULL;
89     }
90     return handle;
91 }
92 
FreeBufferHandle(BufferHandle * handle)93 void FreeBufferHandle(BufferHandle *handle)
94 {
95     if (handle->fd != -1) {
96         close(handle->fd);
97     }
98     for (uint32_t i = 0; i < handle->reserveFds; i++) {
99         if (handle->reserve[i] != -1) {
100             close(handle->reserve[i]);
101         }
102     }
103     OsalMemFree(handle);
104 }
105