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 ~AddressSpaceHostMemoryAllocatorContext(); 33 34 void perform(AddressSpaceDevicePingInfo *info) override; 35 36 AddressSpaceDeviceType getDeviceType() const override; 37 void save(base::Stream* stream) const override; 38 bool load(base::Stream* stream) override; 39 40 static void globalStateSave(base::Stream* stream); 41 static bool globalStateLoad(base::Stream* stream, 42 const address_space_device_control_ops *ops, 43 const AddressSpaceHwFuncs* hw); 44 static void globalStateClear(); 45 46 private: 47 uint64_t allocate(AddressSpaceDevicePingInfo *info); 48 uint64_t unallocate(AddressSpaceDevicePingInfo *info); 49 void *allocate_impl(uint64_t phys_addr, uint64_t size); 50 void clear(); 51 52 std::unordered_map<uint64_t, std::pair<void *, size_t>> m_paddr2ptr; 53 const address_space_device_control_ops *m_ops; // do not save/load 54 }; 55 56 } // namespace emulation 57 } // namespace android 58