1 // Copyright 2019 The Android Open Source Project
2 //
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 #pragma once
15 
16 #include "host-common/AddressSpaceService.h"
17 #include "host-common/address_space_device.h"
18 
19 #include <unordered_map>
20 
21 namespace android {
22 namespace emulation {
23 
24 class AddressSpaceHostMemoryAllocatorContext : public AddressSpaceDeviceContext {
25 public:
26     enum class HostMemoryAllocatorCommand {
27         Allocate = 1,
28         Unallocate = 2
29     };
30 
31     AddressSpaceHostMemoryAllocatorContext(const address_space_device_control_ops *ops,
32                                            const AddressSpaceHwFuncs* hw);
33     ~AddressSpaceHostMemoryAllocatorContext();
34 
35     void perform(AddressSpaceDevicePingInfo *info) override;
36 
37     AddressSpaceDeviceType getDeviceType() const override;
38     void save(base::Stream* stream) const override;
39     bool load(base::Stream* stream) override;
40 
41     static void globalStateSave(base::Stream* stream);
42     static bool globalStateLoad(base::Stream* stream,
43                                 const address_space_device_control_ops *ops,
44                                 const AddressSpaceHwFuncs* hw);
45     static void globalStateClear();
46 
47 private:
48     uint64_t allocate(AddressSpaceDevicePingInfo *info);
49     uint64_t unallocate(AddressSpaceDevicePingInfo *info);
50     void *allocate_impl(uint64_t phys_addr, uint64_t size);
51     void clear();
52 
53     std::unordered_map<uint64_t, std::pair<void *, size_t>> m_paddr2ptr;
54     const address_space_device_control_ops *m_ops;  // do not save/load
55     const AddressSpaceHwFuncs* m_hw;
56 };
57 
58 }  // namespace emulation
59 }  // namespace android
60