1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ANDROID_HIDL_MEMORY_DEALER_H 18 #define ANDROID_HIDL_MEMORY_DEALER_H 19 20 #include <android/hidl/memory/1.0/IMemory.h> 21 #include <android/hidl/memory/block/1.0/types.h> 22 #include <stdint.h> 23 #include <sys/types.h> 24 namespace android { 25 namespace hardware { 26 27 class SimpleBestFitAllocator; 28 29 // MemoryDealer allocates/deallocates blocks from a continuous memory region. 30 // It operates on size and offset and does not depend on any specific data types. 31 class MemoryDealer : public RefBase { 32 public: 33 /// Allocate a block with size. The allocated block is identified with an 34 /// offset. For example: 35 /// ssize_t K = dealer->allocateOffset(size); 36 /// On success, K is positive and points to a subregion K ~ (K+size-1) in the heap. 37 /// It's negative if the allocation fails. 38 virtual ssize_t allocateOffset(size_t size); 39 /// @param offset It points to the block that allocated with allocateOffset previously. 40 virtual void deallocate(size_t offset); 41 /// @param tag a string tag used to mark the dump message 42 virtual void dump(const char* tag) const; 43 44 // allocations are aligned to some value. return that value so clients can account for it. 45 static size_t getAllocationAlignment(); 46 47 MemoryDealer(size_t size); 48 virtual ~MemoryDealer(); 49 50 protected: 51 SimpleBestFitAllocator* mAllocator; 52 }; 53 54 // It extends the generic MemoryDealer and uses 55 // - sp<IMemory> to represent the main memory region. 56 // - MemoryBlock to represent the the block to allocate/deallocate 57 class HidlMemoryDealer : public MemoryDealer { 58 using IMemory = ::android::hidl::memory::V1_0::IMemory; 59 using IMemoryToken = ::android::hidl::memory::token::V1_0::IMemoryToken; 60 using MemoryBlock = ::android::hidl::memory::block::V1_0::MemoryBlock; 61 62 public: 63 static bool isOk(const MemoryBlock& memblk); 64 /// @param memory The memory size must align to 4096 bytes 65 static sp<HidlMemoryDealer> getInstance(const hidl_memory& memory); 66 virtual MemoryBlock allocate(size_t size); 67 virtual sp<IMemory> heap(); 68 69 protected: 70 /// @param heap It must be acquired with mapMemory(memory) with its 71 /// argument corresponds to the 2nd argument passed to HidlMemoryDealer. 72 HidlMemoryDealer(sp<IMemory> heap, const hidl_memory& memory); 73 sp<IMemory> mHeap; 74 sp<IMemoryToken> mToken; 75 }; 76 77 }; // namespace hardware 78 }; // namespace android 79 80 #endif // ANDROID_HIDL_MEMORY_DEALER_H 81