diff --git a/include/utils/MemoryHeapBase.h b/include/utils/MemoryHeapBase.h index ff8973878..574acf4f9 100644 --- a/include/utils/MemoryHeapBase.h +++ b/include/utils/MemoryHeapBase.h @@ -32,7 +32,10 @@ class MemoryHeapBase : public virtual BnMemoryHeap public: enum { READ_ONLY = IMemoryHeap::READ_ONLY, - MAP_ONCE = IMemoryHeap::MAP_ONCE + MAP_ONCE = IMemoryHeap::MAP_ONCE, + // memory won't be mapped locally, but will be mapped in the remote + // process. + DONT_MAP_LOCALLY = 0x00000100 }; /* diff --git a/include/utils/RefBase.h b/include/utils/RefBase.h index e37b56f5b..cbda0fd80 100644 --- a/include/utils/RefBase.h +++ b/include/utils/RefBase.h @@ -17,6 +17,7 @@ #ifndef ANDROID_REF_BASE_H #define ANDROID_REF_BASE_H +#include #include #include @@ -142,6 +143,29 @@ private: // --------------------------------------------------------------------------- +template +class LightRefBase +{ +public: + inline LightRefBase() : mCount(0) { } + inline void incStrong(const void* id) const { + android_atomic_inc(&mCount); + } + inline void decStrong(const void* id) const { + if (android_atomic_dec(&mCount) == 1) { + delete static_cast(this); + } + } + +protected: + inline ~LightRefBase() { } + +private: + mutable volatile int32_t mCount; +}; + +// --------------------------------------------------------------------------- + template class sp { diff --git a/libs/utils/MemoryHeapBase.cpp b/libs/utils/MemoryHeapBase.cpp index 59963c92b..825172819 100644 --- a/libs/utils/MemoryHeapBase.cpp +++ b/libs/utils/MemoryHeapBase.cpp @@ -119,19 +119,24 @@ status_t MemoryHeapBase::mapfd(int fd, size_t size) // if it didn't work, let mmap() fail. } - void* base = (uint8_t*)mmap(0, size, - PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); - if (base == MAP_FAILED) { - LOGE("mmap(fd=%d, size=%u) failed (%s)", - fd, uint32_t(size), strerror(errno)); - close(fd); - return -errno; + if ((mFlags & DONT_MAP_LOCALLY) == 0) { + void* base = (uint8_t*)mmap(0, size, + PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + if (base == MAP_FAILED) { + LOGE("mmap(fd=%d, size=%u) failed (%s)", + fd, uint32_t(size), strerror(errno)); + close(fd); + return -errno; + } + //LOGD("mmap(fd=%d, base=%p, size=%lu)", fd, base, size); + mBase = base; + mNeedUnmap = true; + } else { + mBase = 0; // not MAP_FAILED + mNeedUnmap = false; } - //LOGD("mmap(fd=%d, base=%p, size=%lu)", fd, base, size); mFD = fd; - mBase = base; mSize = size; - mNeedUnmap = true; return NO_ERROR; }