MemoryDealer.h revision 83c0446f27b9542d6c2e724817b2b2d8d1f55085
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_MEMORY_DEALER_H 18#define ANDROID_MEMORY_DEALER_H 19 20 21#include <stdint.h> 22#include <sys/types.h> 23 24#include <binder/IMemory.h> 25#include <utils/threads.h> 26#include <binder/MemoryHeapBase.h> 27 28namespace android { 29// ---------------------------------------------------------------------------- 30class String8; 31 32/* 33 * interface for implementing a "heap". A heap basically provides 34 * the IMemoryHeap interface for cross-process sharing and the 35 * ability to map/unmap pages within the heap. 36 */ 37class HeapInterface : public virtual BnMemoryHeap 38{ 39public: 40 // all values must be page-aligned 41 virtual sp<IMemory> mapMemory(size_t offset, size_t size) = 0; 42 43 HeapInterface(); 44protected: 45 virtual ~HeapInterface(); 46}; 47 48// ---------------------------------------------------------------------------- 49 50/* 51 * interface for implementing an allocator. An allocator provides 52 * methods for allocating and freeing memory blocks and dumping 53 * its state. 54 */ 55class AllocatorInterface : public RefBase 56{ 57public: 58 enum { 59 PAGE_ALIGNED = 0x00000001 60 }; 61 62 virtual size_t allocate(size_t size, uint32_t flags = 0) = 0; 63 virtual status_t deallocate(size_t offset) = 0; 64 virtual size_t size() const = 0; 65 virtual void dump(const char* what, uint32_t flags = 0) const = 0; 66 virtual void dump(String8& res, 67 const char* what, uint32_t flags = 0) const = 0; 68 69 AllocatorInterface(); 70protected: 71 virtual ~AllocatorInterface(); 72}; 73 74// ---------------------------------------------------------------------------- 75 76/* 77 * concrete implementation of HeapInterface on top of mmap() 78 */ 79class SharedHeap : public HeapInterface, public MemoryHeapBase 80{ 81public: 82 SharedHeap(); 83 SharedHeap(size_t size, uint32_t flags = 0, char const * name = NULL); 84 virtual ~SharedHeap(); 85 virtual sp<IMemory> mapMemory(size_t offset, size_t size); 86}; 87 88// ---------------------------------------------------------------------------- 89 90/* 91 * A simple templatized doubly linked-list implementation 92 */ 93 94template <typename NODE> 95class LinkedList 96{ 97 NODE* mFirst; 98 NODE* mLast; 99 100public: 101 LinkedList() : mFirst(0), mLast(0) { } 102 bool isEmpty() const { return mFirst == 0; } 103 NODE const* head() const { return mFirst; } 104 NODE* head() { return mFirst; } 105 NODE const* tail() const { return mLast; } 106 NODE* tail() { return mLast; } 107 108 void insertAfter(NODE* node, NODE* newNode) { 109 newNode->prev = node; 110 newNode->next = node->next; 111 if (node->next == 0) mLast = newNode; 112 else node->next->prev = newNode; 113 node->next = newNode; 114 } 115 116 void insertBefore(NODE* node, NODE* newNode) { 117 newNode->prev = node->prev; 118 newNode->next = node; 119 if (node->prev == 0) mFirst = newNode; 120 else node->prev->next = newNode; 121 node->prev = newNode; 122 } 123 124 void insertHead(NODE* newNode) { 125 if (mFirst == 0) { 126 mFirst = mLast = newNode; 127 newNode->prev = newNode->next = 0; 128 } else { 129 insertBefore(mFirst, newNode); 130 } 131 } 132 133 void insertTail(NODE* newNode) { 134 if (mLast == 0) insertBeginning(newNode); 135 else insertAfter(mLast, newNode); 136 } 137 138 NODE* remove(NODE* node) { 139 if (node->prev == 0) mFirst = node->next; 140 else node->prev->next = node->next; 141 if (node->next == 0) mLast = node->prev; 142 else node->next->prev = node->prev; 143 return node; 144 } 145}; 146 147 148/* 149 * concrete implementation of AllocatorInterface using a simple 150 * best-fit allocation scheme 151 */ 152class SimpleBestFitAllocator : public AllocatorInterface 153{ 154public: 155 156 SimpleBestFitAllocator(size_t size); 157 virtual ~SimpleBestFitAllocator(); 158 159 virtual size_t allocate(size_t size, uint32_t flags = 0); 160 virtual status_t deallocate(size_t offset); 161 virtual size_t size() const; 162 virtual void dump(const char* what, uint32_t flags = 0) const; 163 virtual void dump(String8& res, 164 const char* what, uint32_t flags = 0) const; 165 166private: 167 168 struct chunk_t { 169 chunk_t(size_t start, size_t size) 170 : start(start), size(size), free(1), prev(0), next(0) { 171 } 172 size_t start; 173 size_t size : 28; 174 int free : 4; 175 mutable chunk_t* prev; 176 mutable chunk_t* next; 177 }; 178 179 ssize_t alloc(size_t size, uint32_t flags); 180 chunk_t* dealloc(size_t start); 181 void dump_l(const char* what, uint32_t flags = 0) const; 182 void dump_l(String8& res, const char* what, uint32_t flags = 0) const; 183 184 static const int kMemoryAlign; 185 mutable Mutex mLock; 186 LinkedList<chunk_t> mList; 187 size_t mHeapSize; 188}; 189 190// ---------------------------------------------------------------------------- 191 192class MemoryDealer : public RefBase 193{ 194public: 195 196 enum { 197 READ_ONLY = MemoryHeapBase::READ_ONLY, 198 PAGE_ALIGNED = AllocatorInterface::PAGE_ALIGNED 199 }; 200 201 // creates a memory dealer with the SharedHeap and SimpleBestFitAllocator 202 MemoryDealer(size_t size, uint32_t flags = 0, const char* name = 0); 203 204 // provide a custom heap but use the SimpleBestFitAllocator 205 MemoryDealer(const sp<HeapInterface>& heap); 206 207 // provide both custom heap and allocotar 208 MemoryDealer( 209 const sp<HeapInterface>& heap, 210 const sp<AllocatorInterface>& allocator); 211 212 virtual ~MemoryDealer(); 213 214 virtual sp<IMemory> allocate(size_t size, uint32_t flags = 0); 215 virtual void deallocate(size_t offset); 216 virtual void dump(const char* what, uint32_t flags = 0) const; 217 218 219 sp<IMemoryHeap> getMemoryHeap() const { return heap(); } 220 sp<AllocatorInterface> getAllocator() const { return allocator(); } 221 222private: 223 const sp<HeapInterface>& heap() const; 224 const sp<AllocatorInterface>& allocator() const; 225 226 class Allocation : public BnMemory { 227 public: 228 Allocation(const sp<MemoryDealer>& dealer, 229 ssize_t offset, size_t size, const sp<IMemory>& memory); 230 virtual ~Allocation(); 231 virtual sp<IMemoryHeap> getMemory(ssize_t* offset, size_t* size) const; 232 private: 233 sp<MemoryDealer> mDealer; 234 ssize_t mOffset; 235 size_t mSize; 236 sp<IMemory> mMemory; 237 }; 238 239 sp<HeapInterface> mHeap; 240 sp<AllocatorInterface> mAllocator; 241}; 242 243 244// ---------------------------------------------------------------------------- 245}; // namespace android 246 247#endif // ANDROID_MEMORY_DEALER_H 248