Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 41066ed5 authored by Steve Kondik's avatar Steve Kondik
Browse files

HACK: Allow use of Eclair MemoryDealer for compatibility.

Take #2.
parent 8009c68c
Loading
Loading
Loading
Loading
+17 −2
Original line number Diff line number Diff line
@@ -18,9 +18,12 @@
#define LOG_TAG "CursorWindow"

#include <utils/Log.h>
#ifdef USE_ECLAIR_MEMORYDEALER
#include <binder/MemoryDealer.h>
#else
#include <binder/MemoryHeapBase.h>
#include <binder/MemoryBase.h>

#endif
#include <assert.h>
#include <string.h>
#include <stdlib.h>
@@ -38,7 +41,11 @@ CursorWindow::CursorWindow(size_t maxSize) :
{
}

#ifdef USE_ECLAIR_MEMORYDEALER
bool CursorWindow::setMemory(sp<IMemory> memory)
#else
bool CursorWindow::setMemory(const sp<IMemory>& memory)
#endif
{
    mMemory = memory;
    mData = (uint8_t *) memory->pointer();
@@ -48,6 +55,9 @@ bool CursorWindow::setMemory(const sp<IMemory>& memory)
    mHeader = (window_header_t *) mData;

    // Make the window read-only
#ifdef USE_ECLAIR_MEMORYDEALER
    mHeap = NULL;
#endif
    ssize_t size = memory->size();
    mSize = size;
    mMaxSize = size;
@@ -59,11 +69,16 @@ LOG_WINDOW("Created CursorWindow from existing IMemory: mFreeOffset = %d, numRow
bool CursorWindow::initBuffer(bool localOnly)
{
    //TODO Use a non-memory dealer mmap region for localOnly

#ifdef USE_ECLAIR_MEMORYDEALER
    mHeap = new MemoryDealer(new SharedHeap(mMaxSize, 0, "CursorWindow"));
    if (mHeap != NULL) {
        mMemory = mHeap->allocate(mMaxSize);
#else
    sp<MemoryHeapBase> heap;
    heap = new MemoryHeapBase(mMaxSize, 0, "CursorWindow");
    if (heap != NULL) {
        mMemory = new MemoryBase(heap, 0, mMaxSize);
#endif
        if (mMemory != NULL) {
            mData = (uint8_t *) mMemory->pointer();
            if (mData) {
+11 −1
Original line number Diff line number Diff line
@@ -21,9 +21,12 @@
#include <stddef.h>
#include <stdint.h>

#ifdef USE_ECLAIR_MEMORYDEALER
#include <binder/MemoryDealer.h>
#else
#include <binder/IMemory.h>
#include <utils/RefBase.h>

#endif
#include <jni.h>

#define DEFAULT_WINDOW_SIZE 4096
@@ -101,7 +104,11 @@ class CursorWindow
public:
                        CursorWindow(size_t maxSize);
                        CursorWindow(){}
#ifdef USE_ECLAIR_MEMORYDEALER
    bool                setMemory(sp<IMemory>);
#else
    bool                setMemory(const sp<IMemory>&);
#endif
                        ~CursorWindow();

    bool                initBuffer(bool localOnly);
@@ -189,6 +196,9 @@ private:
    size_t mSize;
    size_t mMaxSize;
    window_header_t * mHeader;
#ifdef USE_ECLAIR_MEMORYDEALER
    sp<MemoryDealer> mHeap;
#endif
    sp<IMemory> mMemory;

    /**
+5 −0
Original line number Diff line number Diff line
@@ -14,6 +14,10 @@
 * limitations under the License.
 */

#ifdef USE_ECLAIR_MEMORYDEALER
#include <binder/MemoryDealerEclair.h>
#else

#ifndef ANDROID_MEMORY_DEALER_H
#define ANDROID_MEMORY_DEALER_H

@@ -58,3 +62,4 @@ private:
}; // namespace android

#endif // ANDROID_MEMORY_DEALER_H
#endif // USE_ECLAIR_MEMORYDEALER
+257 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_MEMORY_DEALER_H
#define ANDROID_MEMORY_DEALER_H


#include <stdint.h>
#include <sys/types.h>

#include <binder/IMemory.h>
#include <utils/threads.h>
#include <binder/MemoryHeapBase.h>

namespace android {
// ----------------------------------------------------------------------------
class String8;

/*
 * interface for implementing a "heap". A heap basically provides
 * the IMemoryHeap interface for cross-process sharing and the
 * ability to map/unmap pages within the heap.
 */
class HeapInterface : public virtual BnMemoryHeap
{
public:
    // all values must be page-aligned
    virtual sp<IMemory> mapMemory(size_t offset, size_t size) = 0;

    HeapInterface();
protected:
    virtual ~HeapInterface();
};

// ----------------------------------------------------------------------------

/*
 * interface for implementing an allocator. An allocator provides
 * methods for allocating and freeing memory blocks and dumping
 * its state.
 */
class AllocatorInterface : public RefBase
{
public:
    enum {
        PAGE_ALIGNED = 0x00000001
    };

    virtual size_t      allocate(size_t size, uint32_t flags = 0) = 0;
    virtual status_t    deallocate(size_t offset) = 0;
    virtual size_t      size() const = 0;
    virtual void        dump(const char* what, uint32_t flags = 0) const = 0;
    virtual void        dump(String8& res,
            const char* what, uint32_t flags = 0) const = 0;

    AllocatorInterface();
protected:
    virtual ~AllocatorInterface();
};

// ----------------------------------------------------------------------------

/*
 * concrete implementation of HeapInterface on top of mmap() 
 */
class SharedHeap : public HeapInterface, public MemoryHeapBase
{
public:
                        SharedHeap();
                        SharedHeap(size_t size, uint32_t flags = 0, char const * name = NULL);
    virtual             ~SharedHeap();
    virtual sp<IMemory> mapMemory(size_t offset, size_t size);
};

// ----------------------------------------------------------------------------

/*
 * A simple templatized doubly linked-list implementation
 */

template <typename NODE>
class LinkedList
{
    NODE*  mFirst;
    NODE*  mLast;

public:
                LinkedList() : mFirst(0), mLast(0) { }
    bool        isEmpty() const { return mFirst == 0; }
    NODE const* head() const { return mFirst; }
    NODE*       head() { return mFirst; }
    NODE const* tail() const { return mLast; }
    NODE*       tail() { return mLast; }

    void insertAfter(NODE* node, NODE* newNode) {
        newNode->prev = node;
        newNode->next = node->next;
        if (node->next == 0) mLast = newNode;
        else                 node->next->prev = newNode;
        node->next = newNode;
    }

    void insertBefore(NODE* node, NODE* newNode) {
         newNode->prev = node->prev;
         newNode->next = node;
         if (node->prev == 0)   mFirst = newNode;
         else                   node->prev->next = newNode;
         node->prev = newNode;
    }

    void insertHead(NODE* newNode) {
        if (mFirst == 0) {
            mFirst = mLast = newNode;
            newNode->prev = newNode->next = 0;
        } else {
            newNode->prev = 0;
            newNode->next = mFirst;
            mFirst->prev = newNode;
            mFirst = newNode;
        }
    }
    
    void insertTail(NODE* newNode) {
        if (mLast == 0) {
            insertHead(newNode);
        } else {
            newNode->prev = mLast;
            newNode->next = 0;
            mLast->next = newNode;
            mLast = newNode;
        }
    }

    NODE* remove(NODE* node) {
        if (node->prev == 0)    mFirst = node->next;
        else                    node->prev->next = node->next;
        if (node->next == 0)    mLast = node->prev;
        else                    node->next->prev = node->prev;
        return node;
    }
};


/*
 * concrete implementation of AllocatorInterface using a simple
 * best-fit allocation scheme
 */
class SimpleBestFitAllocator : public AllocatorInterface
{
public:

                        SimpleBestFitAllocator(size_t size);
    virtual             ~SimpleBestFitAllocator();

    virtual size_t      allocate(size_t size, uint32_t flags = 0);
    virtual status_t    deallocate(size_t offset);
    virtual size_t      size() const;
    virtual void        dump(const char* what, uint32_t flags = 0) const;
    virtual void        dump(String8& res,
            const char* what, uint32_t flags = 0) const;

private:

    struct chunk_t {
        chunk_t(size_t start, size_t size) 
            : start(start), size(size), free(1), prev(0), next(0) {
        }
        size_t              start;
        size_t              size : 28;
        int                 free : 4;
        mutable chunk_t*    prev;
        mutable chunk_t*    next;
    };

    ssize_t  alloc(size_t size, uint32_t flags);
    chunk_t* dealloc(size_t start);
    void     dump_l(const char* what, uint32_t flags = 0) const;
    void     dump_l(String8& res, const char* what, uint32_t flags = 0) const;

    static const int    kMemoryAlign;
    mutable Mutex       mLock;
    LinkedList<chunk_t> mList;
    size_t              mHeapSize;
};

// ----------------------------------------------------------------------------

class MemoryDealer : public RefBase
{
public:

    enum {
        READ_ONLY = MemoryHeapBase::READ_ONLY,
        PAGE_ALIGNED = AllocatorInterface::PAGE_ALIGNED
    };

    // creates a memory dealer with the SharedHeap and SimpleBestFitAllocator
    MemoryDealer(size_t size, uint32_t flags = 0, const char* name = 0);

    // provide a custom heap but use the SimpleBestFitAllocator
    MemoryDealer(const sp<HeapInterface>& heap);

    // provide both custom heap and allocotar
    MemoryDealer(
            const sp<HeapInterface>& heap,
            const sp<AllocatorInterface>& allocator);

    virtual sp<IMemory> allocate(size_t size, uint32_t flags = 0);
    virtual void        deallocate(size_t offset);
    virtual void        dump(const char* what, uint32_t flags = 0) const;


    sp<IMemoryHeap> getMemoryHeap() const { return heap(); }
    sp<AllocatorInterface> getAllocator() const { return allocator(); }

protected:
    virtual ~MemoryDealer();

private:    
    const sp<HeapInterface>&        heap() const;
    const sp<AllocatorInterface>&   allocator() const;

    class Allocation : public BnMemory {
    public:
        Allocation(const sp<MemoryDealer>& dealer,
                ssize_t offset, size_t size, const sp<IMemory>& memory);
        virtual ~Allocation();
        virtual sp<IMemoryHeap> getMemory(ssize_t* offset, size_t* size) const;
    private:
        sp<MemoryDealer>        mDealer;
        ssize_t                 mOffset;
        size_t                  mSize;
        sp<IMemory>             mMemory;
    };

    sp<HeapInterface>           mHeap;
    sp<AllocatorInterface>      mAllocator;
};


// ----------------------------------------------------------------------------
}; // namespace android

#endif // ANDROID_MEMORY_DEALER_H
+9 −1
Original line number Diff line number Diff line
@@ -20,18 +20,26 @@
#include <stdlib.h>
#include <stdint.h>

#ifdef USE_ECLAIR_MEMORYDEALER
#include <binder/MemoryDealer.h>
#endif
#include <binder/MemoryHeapBase.h>
#include <binder/IMemory.h>
#include <utils/SortedVector.h>
#ifndef USE_ECLAIR_MEMORYDEALER
#include <utils/threads.h>
#endif

namespace android {

class MemoryHeapBase;

// ---------------------------------------------------------------------------

#ifdef USE_ECLAIR_MEMORYDEALER
class MemoryHeapPmem : public HeapInterface, public MemoryHeapBase
#else
class MemoryHeapPmem : public MemoryHeapBase
#endif
{
public:
    class MemoryPmem : public BnMemory {
Loading