blob: 79d7883af153e8cadb140285d0c8bf73b4298735 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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
24#include <utils/IMemory.h>
25#include <utils/threads.h>
26#include <utils/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
44// ----------------------------------------------------------------------------
45
46/*
47 * interface for implementing an allocator. An allocator provides
48 * methods for allocating and freeing memory blocks and dumping
49 * its state.
50 */
51class AllocatorInterface : public RefBase
52{
53public:
54 enum {
55 PAGE_ALIGNED = 0x00000001
56 };
57
58 virtual size_t allocate(size_t size, uint32_t flags = 0) = 0;
59 virtual status_t deallocate(size_t offset) = 0;
60 virtual size_t size() const = 0;
61 virtual void dump(const char* what, uint32_t flags = 0) const = 0;
62 virtual void dump(String8& res,
63 const char* what, uint32_t flags = 0) const = 0;
64};
65
66// ----------------------------------------------------------------------------
67
68/*
69 * concrete implementation of HeapInterface on top of mmap()
70 */
71class SharedHeap : public HeapInterface, public MemoryHeapBase
72{
73public:
74 SharedHeap(size_t size, uint32_t flags = 0, char const * name = NULL);
75 virtual ~SharedHeap();
76 virtual sp<IMemory> mapMemory(size_t offset, size_t size);
77};
78
79// ----------------------------------------------------------------------------
80
81/*
82 * A simple templatized doubly linked-list implementation
83 */
84
85template <typename NODE>
86class LinkedList
87{
88 NODE* mFirst;
89 NODE* mLast;
90
91public:
92 LinkedList() : mFirst(0), mLast(0) { }
93 bool isEmpty() const { return mFirst == 0; }
94 NODE const* head() const { return mFirst; }
95 NODE* head() { return mFirst; }
96 NODE const* tail() const { return mLast; }
97 NODE* tail() { return mLast; }
98
99 void insertAfter(NODE* node, NODE* newNode) {
100 newNode->prev = node;
101 newNode->next = node->next;
102 if (node->next == 0) mLast = newNode;
103 else node->next->prev = newNode;
104 node->next = newNode;
105 }
106
107 void insertBefore(NODE* node, NODE* newNode) {
108 newNode->prev = node->prev;
109 newNode->next = node;
110 if (node->prev == 0) mFirst = newNode;
111 else node->prev->next = newNode;
112 node->prev = newNode;
113 }
114
115 void insertHead(NODE* newNode) {
116 if (mFirst == 0) {
117 mFirst = mLast = newNode;
118 newNode->prev = newNode->next = 0;
119 } else {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700120 newNode->prev = 0;
121 newNode->next = mFirst;
122 mFirst->prev = newNode;
123 mFirst = newNode;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 }
125 }
126
127 void insertTail(NODE* newNode) {
Mathias Agopianf1d8e872009-04-20 19:39:12 -0700128 if (mLast == 0) {
129 insertHead(newNode);
130 } else {
131 newNode->prev = mLast;
132 newNode->next = 0;
133 mLast->next = newNode;
134 mLast = newNode;
135 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 }
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