blob: 6628f751b61dcd98a6ed2526957d7ebb90be0e84 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Agopian07952722009-05-19 19:08:10 -070024#include <binder/IMemory.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <utils/threads.h>
Mathias Agopian07952722009-05-19 19:08:10 -070026#include <binder/MemoryHeapBase.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
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;
Mathias Agopianaaf834a2009-05-22 19:00:22 -070042
43 HeapInterface();
44protected:
45 virtual ~HeapInterface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046};
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;
Mathias Agopianaaf834a2009-05-22 19:00:22 -070068
69 AllocatorInterface();
70protected:
71 virtual ~AllocatorInterface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072};
73
74// ----------------------------------------------------------------------------
75
76/*
77 * concrete implementation of HeapInterface on top of mmap()
78 */
79class SharedHeap : public HeapInterface, public MemoryHeapBase
80{
81public:
Mathias Agopianaaf834a2009-05-22 19:00:22 -070082 SharedHeap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 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 {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700129 newNode->prev = 0;
130 newNode->next = mFirst;
131 mFirst->prev = newNode;
132 mFirst = newNode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 }
134 }
135
136 void insertTail(NODE* newNode) {
Mathias Agopian6ead5d92009-04-20 19:39:12 -0700137 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 Project9066cfe2009-03-03 19:31:44 -0800145 }
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 */
161class SimpleBestFitAllocator : public AllocatorInterface
162{
163public:
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
175private:
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
201class MemoryDealer : public RefBase
202{
203public:
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
221 virtual ~MemoryDealer();
222
223 virtual sp<IMemory> allocate(size_t size, uint32_t flags = 0);
224 virtual void deallocate(size_t offset);
225 virtual void dump(const char* what, uint32_t flags = 0) const;
226
227
228 sp<IMemoryHeap> getMemoryHeap() const { return heap(); }
229 sp<AllocatorInterface> getAllocator() const { return allocator(); }
230
231private:
232 const sp<HeapInterface>& heap() const;
233 const sp<AllocatorInterface>& allocator() const;
234
235 class Allocation : public BnMemory {
236 public:
237 Allocation(const sp<MemoryDealer>& dealer,
238 ssize_t offset, size_t size, const sp<IMemory>& memory);
239 virtual ~Allocation();
240 virtual sp<IMemoryHeap> getMemory(ssize_t* offset, size_t* size) const;
241 private:
242 sp<MemoryDealer> mDealer;
243 ssize_t mOffset;
244 size_t mSize;
245 sp<IMemory> mMemory;
246 };
247
248 sp<HeapInterface> mHeap;
249 sp<AllocatorInterface> mAllocator;
250};
251
252
253// ----------------------------------------------------------------------------
254}; // namespace android
255
256#endif // ANDROID_MEMORY_DEALER_H