MemoryDealer.h revision f0adf848ffffeca86f4c0210ea237883d9e5e8d5
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            newNode->prev = 0;
130            newNode->next = mFirst;
131            mFirst->prev = newNode;
132            mFirst = newNode;
133        }
134    }
135
136    void insertTail(NODE* newNode) {
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        }
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 */
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 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
229protected:
230    virtual ~MemoryDealer();
231
232private:
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
258