1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include "allocator_service_stub.h"
17094332d3Sopenharmony_ci#include "hdf_base.h"
18094332d3Sopenharmony_ci#include "hdf_device_desc.h"
19094332d3Sopenharmony_ci#include "hdf_log.h"
20094332d3Sopenharmony_ci#include "osal_mem.h"
21094332d3Sopenharmony_ci
22094332d3Sopenharmony_ciusing HdfAllocatorService = struct HdfAllocatorService_ {
23094332d3Sopenharmony_ci    struct IDeviceIoService ioservice;
24094332d3Sopenharmony_ci    void *instance;
25094332d3Sopenharmony_ci};
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_cistatic int32_t AllocatorServiceDispatch(struct HdfDeviceIoClient *client, int cmdId,
28094332d3Sopenharmony_ci    struct HdfSBuf *data, struct HdfSBuf *reply)
29094332d3Sopenharmony_ci{
30094332d3Sopenharmony_ci    HdfAllocatorService *allocatorService = CONTAINER_OF(
31094332d3Sopenharmony_ci        client->device->service, HdfAllocatorService, ioservice);
32094332d3Sopenharmony_ci    return AllocatorServiceOnRemoteRequest(allocatorService->instance, cmdId, *data, *reply);
33094332d3Sopenharmony_ci}
34094332d3Sopenharmony_ci
35094332d3Sopenharmony_ciint HdfAllocatorDriverInit(struct HdfDeviceObject *deviceObject)
36094332d3Sopenharmony_ci{
37094332d3Sopenharmony_ci    (void)deviceObject;
38094332d3Sopenharmony_ci    return HDF_SUCCESS;
39094332d3Sopenharmony_ci}
40094332d3Sopenharmony_ci
41094332d3Sopenharmony_ciint HdfAllocatorDriverBind(struct HdfDeviceObject *deviceObject)
42094332d3Sopenharmony_ci{
43094332d3Sopenharmony_ci    HdfAllocatorService *allocatorService =
44094332d3Sopenharmony_ci        reinterpret_cast<HdfAllocatorService *>(OsalMemAlloc(sizeof(HdfAllocatorService)));
45094332d3Sopenharmony_ci    if (allocatorService == nullptr) {
46094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: OsalMemAlloc failed", __func__);
47094332d3Sopenharmony_ci        return HDF_FAILURE;
48094332d3Sopenharmony_ci    }
49094332d3Sopenharmony_ci
50094332d3Sopenharmony_ci    allocatorService->ioservice.Dispatch = AllocatorServiceDispatch;
51094332d3Sopenharmony_ci    allocatorService->ioservice.Open = NULL;
52094332d3Sopenharmony_ci    allocatorService->ioservice.Release = NULL;
53094332d3Sopenharmony_ci    allocatorService->instance = AllocatorServiceInstance();
54094332d3Sopenharmony_ci
55094332d3Sopenharmony_ci    deviceObject->service = &allocatorService->ioservice;
56094332d3Sopenharmony_ci    HDF_LOGI("%{public}s: exit succ", __func__);
57094332d3Sopenharmony_ci    return HDF_SUCCESS;
58094332d3Sopenharmony_ci}
59094332d3Sopenharmony_ci
60094332d3Sopenharmony_civoid HdfAllocatorDriverRelease(struct HdfDeviceObject *deviceObject)
61094332d3Sopenharmony_ci{
62094332d3Sopenharmony_ci    HdfAllocatorService *allocatorService = CONTAINER_OF(deviceObject->service, HdfAllocatorService, ioservice);
63094332d3Sopenharmony_ci    AllocatorServiceRelease(allocatorService->instance);
64094332d3Sopenharmony_ci    OsalMemFree(allocatorService);
65094332d3Sopenharmony_ci}
66094332d3Sopenharmony_ci
67094332d3Sopenharmony_cistruct HdfDriverEntry g_AllocatorDriverEntry = {
68094332d3Sopenharmony_ci    .moduleVersion = 1,
69094332d3Sopenharmony_ci    .moduleName = "allocator_service",
70094332d3Sopenharmony_ci    .Bind = HdfAllocatorDriverBind,
71094332d3Sopenharmony_ci    .Init = HdfAllocatorDriverInit,
72094332d3Sopenharmony_ci    .Release = HdfAllocatorDriverRelease,
73094332d3Sopenharmony_ci};
74094332d3Sopenharmony_ci
75094332d3Sopenharmony_ci#ifdef __cplusplus
76094332d3Sopenharmony_ciextern "C" {
77094332d3Sopenharmony_ci#endif /* __cplusplus */
78094332d3Sopenharmony_ci
79094332d3Sopenharmony_ciHDF_INIT(g_AllocatorDriverEntry);
80094332d3Sopenharmony_ci
81094332d3Sopenharmony_ci#ifdef __cplusplus
82094332d3Sopenharmony_ci}
83094332d3Sopenharmony_ci#endif /* __cplusplus */
84