The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 24 | #include <binder/IMemory.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <utils/threads.h> |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 26 | #include <binder/MemoryHeapBase.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | // ---------------------------------------------------------------------------- |
| 30 | class 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 | */ |
| 37 | class HeapInterface : public virtual BnMemoryHeap |
| 38 | { |
| 39 | public: |
| 40 | // all values must be page-aligned |
| 41 | virtual sp<IMemory> mapMemory(size_t offset, size_t size) = 0; |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 42 | |
| 43 | HeapInterface(); |
| 44 | protected: |
| 45 | virtual ~HeapInterface(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
| 55 | class AllocatorInterface : public RefBase |
| 56 | { |
| 57 | public: |
| 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; |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 68 | |
| 69 | AllocatorInterface(); |
| 70 | protected: |
| 71 | virtual ~AllocatorInterface(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | // ---------------------------------------------------------------------------- |
| 75 | |
| 76 | /* |
| 77 | * concrete implementation of HeapInterface on top of mmap() |
| 78 | */ |
| 79 | class SharedHeap : public HeapInterface, public MemoryHeapBase |
| 80 | { |
| 81 | public: |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 82 | SharedHeap(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 94 | template <typename NODE> |
| 95 | class LinkedList |
| 96 | { |
| 97 | NODE* mFirst; |
| 98 | NODE* mLast; |
| 99 | |
| 100 | public: |
| 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 { |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 129 | newNode->prev = 0; |
| 130 | newNode->next = mFirst; |
| 131 | mFirst->prev = newNode; |
| 132 | mFirst = newNode; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | void insertTail(NODE* newNode) { |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 137 | if (mLast == 0) { |
| 138 | insertHead(newNode); |
| 139 | } else { |
| 140 | newNode->prev = mLast; |
| 141 | newNode->next = 0; |
| 142 | mLast->next = newNode; |
| 143 | mLast = newNode; |
| 144 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | NODE* remove(NODE* node) { |
| 148 | if (node->prev == 0) mFirst = node->next; |
| 149 | else node->prev->next = node->next; |
| 150 | if (node->next == 0) mLast = node->prev; |
| 151 | else node->next->prev = node->prev; |
| 152 | return node; |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | |
| 157 | /* |
| 158 | * concrete implementation of AllocatorInterface using a simple |
| 159 | * best-fit allocation scheme |
| 160 | */ |
| 161 | class SimpleBestFitAllocator : public AllocatorInterface |
| 162 | { |
| 163 | public: |
| 164 | |
| 165 | SimpleBestFitAllocator(size_t size); |
| 166 | virtual ~SimpleBestFitAllocator(); |
| 167 | |
| 168 | virtual size_t allocate(size_t size, uint32_t flags = 0); |
| 169 | virtual status_t deallocate(size_t offset); |
| 170 | virtual size_t size() const; |
| 171 | virtual void dump(const char* what, uint32_t flags = 0) const; |
| 172 | virtual void dump(String8& res, |
| 173 | const char* what, uint32_t flags = 0) const; |
| 174 | |
| 175 | private: |
| 176 | |
| 177 | struct chunk_t { |
| 178 | chunk_t(size_t start, size_t size) |
| 179 | : start(start), size(size), free(1), prev(0), next(0) { |
| 180 | } |
| 181 | size_t start; |
| 182 | size_t size : 28; |
| 183 | int free : 4; |
| 184 | mutable chunk_t* prev; |
| 185 | mutable chunk_t* next; |
| 186 | }; |
| 187 | |
| 188 | ssize_t alloc(size_t size, uint32_t flags); |
| 189 | chunk_t* dealloc(size_t start); |
| 190 | void dump_l(const char* what, uint32_t flags = 0) const; |
| 191 | void dump_l(String8& res, const char* what, uint32_t flags = 0) const; |
| 192 | |
| 193 | static const int kMemoryAlign; |
| 194 | mutable Mutex mLock; |
| 195 | LinkedList<chunk_t> mList; |
| 196 | size_t mHeapSize; |
| 197 | }; |
| 198 | |
| 199 | // ---------------------------------------------------------------------------- |
| 200 | |
| 201 | class MemoryDealer : public RefBase |
| 202 | { |
| 203 | public: |
| 204 | |
| 205 | enum { |
| 206 | READ_ONLY = MemoryHeapBase::READ_ONLY, |
| 207 | PAGE_ALIGNED = AllocatorInterface::PAGE_ALIGNED |
| 208 | }; |
| 209 | |
| 210 | // creates a memory dealer with the SharedHeap and SimpleBestFitAllocator |
| 211 | MemoryDealer(size_t size, uint32_t flags = 0, const char* name = 0); |
| 212 | |
| 213 | // provide a custom heap but use the SimpleBestFitAllocator |
| 214 | MemoryDealer(const sp<HeapInterface>& heap); |
| 215 | |
| 216 | // provide both custom heap and allocotar |
| 217 | MemoryDealer( |
| 218 | const sp<HeapInterface>& heap, |
| 219 | const sp<AllocatorInterface>& allocator); |
| 220 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | virtual sp<IMemory> allocate(size_t size, uint32_t flags = 0); |
| 222 | virtual void deallocate(size_t offset); |
| 223 | virtual void dump(const char* what, uint32_t flags = 0) const; |
| 224 | |
| 225 | |
| 226 | sp<IMemoryHeap> getMemoryHeap() const { return heap(); } |
| 227 | sp<AllocatorInterface> getAllocator() const { return allocator(); } |
| 228 | |
Andreas Huber | f0adf84 | 2009-07-16 10:05:57 -0700 | [diff] [blame] | 229 | protected: |
| 230 | virtual ~MemoryDealer(); |
| 231 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | private: |
| 233 | const sp<HeapInterface>& heap() const; |
| 234 | const sp<AllocatorInterface>& allocator() const; |
| 235 | |
| 236 | class Allocation : public BnMemory { |
| 237 | public: |
| 238 | Allocation(const sp<MemoryDealer>& dealer, |
| 239 | ssize_t offset, size_t size, const sp<IMemory>& memory); |
| 240 | virtual ~Allocation(); |
| 241 | virtual sp<IMemoryHeap> getMemory(ssize_t* offset, size_t* size) const; |
| 242 | private: |
| 243 | sp<MemoryDealer> mDealer; |
| 244 | ssize_t mOffset; |
| 245 | size_t mSize; |
| 246 | sp<IMemory> mMemory; |
| 247 | }; |
| 248 | |
| 249 | sp<HeapInterface> mHeap; |
| 250 | sp<AllocatorInterface> mAllocator; |
| 251 | }; |
| 252 | |
| 253 | |
| 254 | // ---------------------------------------------------------------------------- |
| 255 | }; // namespace android |
| 256 | |
| 257 | #endif // ANDROID_MEMORY_DEALER_H |