2009-03-04 03:31:44 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "MemoryDealer"
|
|
|
|
|
2009-05-20 02:08:10 +00:00
|
|
|
#include <binder/MemoryDealer.h>
|
2010-01-26 03:00:00 +00:00
|
|
|
#include <binder/IPCThreadState.h>
|
|
|
|
#include <binder/MemoryBase.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
#include <utils/Log.h>
|
|
|
|
#include <utils/SortedVector.h>
|
|
|
|
#include <utils/String8.h>
|
2010-01-26 03:00:00 +00:00
|
|
|
#include <utils/threads.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
|
|
|
|
namespace android {
|
2009-05-23 02:00:22 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-01-26 03:00:00 +00:00
|
|
|
/*
|
|
|
|
* A simple templatized doubly linked-list implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename NODE>
|
|
|
|
class LinkedList
|
|
|
|
{
|
|
|
|
NODE* mFirst;
|
|
|
|
NODE* mLast;
|
2009-05-23 02:00:22 +00:00
|
|
|
|
2010-01-26 03:00:00 +00:00
|
|
|
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;
|
|
|
|
}
|
2009-05-23 02:00:22 +00:00
|
|
|
|
2010-01-26 03:00:00 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2010-01-26 03:00:00 +00:00
|
|
|
class Allocation : public MemoryBase {
|
2009-03-04 03:31:44 +00:00
|
|
|
public:
|
2010-01-26 03:00:00 +00:00
|
|
|
Allocation(const sp<MemoryDealer>& dealer,
|
|
|
|
const sp<IMemoryHeap>& heap, ssize_t offset, size_t size);
|
|
|
|
virtual ~Allocation();
|
|
|
|
private:
|
|
|
|
sp<MemoryDealer> mDealer;
|
2009-03-04 03:31:44 +00:00
|
|
|
};
|
|
|
|
|
2010-01-26 03:00:00 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class SimpleBestFitAllocator
|
|
|
|
{
|
|
|
|
enum {
|
|
|
|
PAGE_ALIGNED = 0x00000001
|
|
|
|
};
|
|
|
|
public:
|
|
|
|
SimpleBestFitAllocator(size_t size);
|
|
|
|
~SimpleBestFitAllocator();
|
|
|
|
|
|
|
|
size_t allocate(size_t size, uint32_t flags = 0);
|
|
|
|
status_t deallocate(size_t offset);
|
|
|
|
size_t size() const;
|
|
|
|
void dump(const char* what) const;
|
|
|
|
void dump(String8& res, const char* what) 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) const;
|
|
|
|
void dump_l(String8& res, const char* what) const;
|
|
|
|
|
|
|
|
static const int kMemoryAlign;
|
|
|
|
mutable Mutex mLock;
|
|
|
|
LinkedList<chunk_t> mList;
|
|
|
|
size_t mHeapSize;
|
|
|
|
};
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2010-01-26 03:00:00 +00:00
|
|
|
Allocation::Allocation(
|
|
|
|
const sp<MemoryDealer>& dealer,
|
|
|
|
const sp<IMemoryHeap>& heap, ssize_t offset, size_t size)
|
|
|
|
: MemoryBase(heap, offset, size), mDealer(dealer)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2010-01-26 03:00:00 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
void* const start_ptr = (void*)(intptr_t(heap->base()) + offset);
|
|
|
|
memset(start_ptr, 0xda, size);
|
|
|
|
#endif
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 03:00:00 +00:00
|
|
|
Allocation::~Allocation()
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2010-01-26 03:00:00 +00:00
|
|
|
size_t freedOffset = getOffset();
|
|
|
|
size_t freedSize = getSize();
|
|
|
|
if (freedSize) {
|
2009-03-04 03:31:44 +00:00
|
|
|
/* NOTE: it's VERY important to not free allocations of size 0 because
|
|
|
|
* they're special as they don't have any record in the allocator
|
|
|
|
* and could alias some real allocation (their offset is zero). */
|
2010-01-26 03:00:00 +00:00
|
|
|
|
|
|
|
// keep the size to unmap in excess
|
|
|
|
size_t pagesize = getpagesize();
|
|
|
|
size_t start = freedOffset;
|
|
|
|
size_t end = start + freedSize;
|
|
|
|
start &= ~(pagesize-1);
|
|
|
|
end = (end + pagesize-1) & ~(pagesize-1);
|
|
|
|
|
|
|
|
// give back to the kernel the pages we don't need
|
|
|
|
size_t free_start = freedOffset;
|
|
|
|
size_t free_end = free_start + freedSize;
|
|
|
|
if (start < free_start)
|
|
|
|
start = free_start;
|
|
|
|
if (end > free_end)
|
|
|
|
end = free_end;
|
|
|
|
start = (start + pagesize-1) & ~(pagesize-1);
|
|
|
|
end &= ~(pagesize-1);
|
|
|
|
|
|
|
|
if (start < end) {
|
|
|
|
void* const start_ptr = (void*)(intptr_t(getHeap()->base()) + start);
|
|
|
|
size_t size = end-start;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2010-01-26 03:00:00 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
memset(start_ptr, 0xdf, size);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// MADV_REMOVE is not defined on Dapper based Goobuntu
|
|
|
|
#ifdef MADV_REMOVE
|
|
|
|
if (size) {
|
|
|
|
int err = madvise(start_ptr, size, MADV_REMOVE);
|
2014-02-06 01:42:44 +00:00
|
|
|
ALOGW_IF(err, "madvise(%p, %zu, MADV_REMOVE) returned %s",
|
2010-01-26 03:00:00 +00:00
|
|
|
start_ptr, size, err<0 ? strerror(errno) : "Ok");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2011-12-14 18:53:24 +00:00
|
|
|
|
|
|
|
// This should be done after madvise(MADV_REMOVE), otherwise madvise()
|
|
|
|
// might kick out the memory region that's allocated and/or written
|
|
|
|
// right after the deallocation.
|
|
|
|
mDealer->deallocate(freedOffset);
|
2010-01-26 03:00:00 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2014-03-14 23:49:51 +00:00
|
|
|
MemoryDealer::MemoryDealer(size_t size, const char* name, uint32_t flags)
|
|
|
|
: mHeap(new MemoryHeapBase(size, flags, name)),
|
2009-03-04 03:31:44 +00:00
|
|
|
mAllocator(new SimpleBestFitAllocator(size))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryDealer::~MemoryDealer()
|
|
|
|
{
|
2010-01-26 03:00:00 +00:00
|
|
|
delete mAllocator;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 03:00:00 +00:00
|
|
|
sp<IMemory> MemoryDealer::allocate(size_t size)
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
sp<IMemory> memory;
|
2010-01-26 03:00:00 +00:00
|
|
|
const ssize_t offset = allocator()->allocate(size);
|
2009-03-04 03:31:44 +00:00
|
|
|
if (offset >= 0) {
|
2010-01-26 03:00:00 +00:00
|
|
|
memory = new Allocation(this, heap(), offset, size);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
return memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryDealer::deallocate(size_t offset)
|
|
|
|
{
|
|
|
|
allocator()->deallocate(offset);
|
|
|
|
}
|
|
|
|
|
2010-01-26 03:00:00 +00:00
|
|
|
void MemoryDealer::dump(const char* what) const
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
2010-01-26 03:00:00 +00:00
|
|
|
allocator()->dump(what);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 03:00:00 +00:00
|
|
|
const sp<IMemoryHeap>& MemoryDealer::heap() const {
|
2009-03-04 03:31:44 +00:00
|
|
|
return mHeap;
|
|
|
|
}
|
|
|
|
|
2010-01-26 03:00:00 +00:00
|
|
|
SimpleBestFitAllocator* MemoryDealer::allocator() const {
|
2009-03-04 03:31:44 +00:00
|
|
|
return mAllocator;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// align all the memory blocks on a cache-line boundary
|
|
|
|
const int SimpleBestFitAllocator::kMemoryAlign = 32;
|
|
|
|
|
|
|
|
SimpleBestFitAllocator::SimpleBestFitAllocator(size_t size)
|
|
|
|
{
|
|
|
|
size_t pagesize = getpagesize();
|
|
|
|
mHeapSize = ((size + pagesize-1) & ~(pagesize-1));
|
|
|
|
|
|
|
|
chunk_t* node = new chunk_t(0, mHeapSize / kMemoryAlign);
|
|
|
|
mList.insertHead(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleBestFitAllocator::~SimpleBestFitAllocator()
|
|
|
|
{
|
|
|
|
while(!mList.isEmpty()) {
|
|
|
|
delete mList.remove(mList.head());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SimpleBestFitAllocator::size() const
|
|
|
|
{
|
|
|
|
return mHeapSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SimpleBestFitAllocator::allocate(size_t size, uint32_t flags)
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
ssize_t offset = alloc(size, flags);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t SimpleBestFitAllocator::deallocate(size_t offset)
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
chunk_t const * const freed = dealloc(offset);
|
|
|
|
if (freed) {
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
return NAME_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t SimpleBestFitAllocator::alloc(size_t size, uint32_t flags)
|
|
|
|
{
|
|
|
|
if (size == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
size = (size + kMemoryAlign-1) / kMemoryAlign;
|
|
|
|
chunk_t* free_chunk = 0;
|
|
|
|
chunk_t* cur = mList.head();
|
|
|
|
|
|
|
|
size_t pagesize = getpagesize();
|
|
|
|
while (cur) {
|
|
|
|
int extra = 0;
|
|
|
|
if (flags & PAGE_ALIGNED)
|
|
|
|
extra = ( -cur->start & ((pagesize/kMemoryAlign)-1) ) ;
|
|
|
|
|
|
|
|
// best fit
|
|
|
|
if (cur->free && (cur->size >= (size+extra))) {
|
|
|
|
if ((!free_chunk) || (cur->size < free_chunk->size)) {
|
|
|
|
free_chunk = cur;
|
|
|
|
}
|
|
|
|
if (cur->size == size) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (free_chunk) {
|
|
|
|
const size_t free_size = free_chunk->size;
|
|
|
|
free_chunk->free = 0;
|
|
|
|
free_chunk->size = size;
|
|
|
|
if (free_size > size) {
|
|
|
|
int extra = 0;
|
|
|
|
if (flags & PAGE_ALIGNED)
|
|
|
|
extra = ( -free_chunk->start & ((pagesize/kMemoryAlign)-1) ) ;
|
|
|
|
if (extra) {
|
|
|
|
chunk_t* split = new chunk_t(free_chunk->start, extra);
|
|
|
|
free_chunk->start += extra;
|
|
|
|
mList.insertBefore(free_chunk, split);
|
|
|
|
}
|
|
|
|
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE_IF((flags&PAGE_ALIGNED) &&
|
2009-03-04 03:31:44 +00:00
|
|
|
((free_chunk->start*kMemoryAlign)&(pagesize-1)),
|
|
|
|
"PAGE_ALIGNED requested, but page is not aligned!!!");
|
|
|
|
|
|
|
|
const ssize_t tail_free = free_size - (size+extra);
|
|
|
|
if (tail_free > 0) {
|
|
|
|
chunk_t* split = new chunk_t(
|
|
|
|
free_chunk->start + free_chunk->size, tail_free);
|
|
|
|
mList.insertAfter(free_chunk, split);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (free_chunk->start)*kMemoryAlign;
|
|
|
|
}
|
|
|
|
return NO_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleBestFitAllocator::chunk_t* SimpleBestFitAllocator::dealloc(size_t start)
|
|
|
|
{
|
|
|
|
start = start / kMemoryAlign;
|
|
|
|
chunk_t* cur = mList.head();
|
|
|
|
while (cur) {
|
|
|
|
if (cur->start == start) {
|
|
|
|
LOG_FATAL_IF(cur->free,
|
|
|
|
"block at offset 0x%08lX of size 0x%08lX already freed",
|
|
|
|
cur->start*kMemoryAlign, cur->size*kMemoryAlign);
|
|
|
|
|
|
|
|
// merge freed blocks together
|
|
|
|
chunk_t* freed = cur;
|
|
|
|
cur->free = 1;
|
|
|
|
do {
|
|
|
|
chunk_t* const p = cur->prev;
|
|
|
|
chunk_t* const n = cur->next;
|
|
|
|
if (p && (p->free || !cur->size)) {
|
|
|
|
freed = p;
|
|
|
|
p->size += cur->size;
|
|
|
|
mList.remove(cur);
|
|
|
|
delete cur;
|
|
|
|
}
|
|
|
|
cur = n;
|
|
|
|
} while (cur && cur->free);
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (!freed->free) {
|
|
|
|
dump_l("dealloc (!freed->free)");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
LOG_FATAL_IF(!freed->free,
|
|
|
|
"freed block at offset 0x%08lX of size 0x%08lX is not free!",
|
|
|
|
freed->start * kMemoryAlign, freed->size * kMemoryAlign);
|
|
|
|
|
|
|
|
return freed;
|
|
|
|
}
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-26 03:00:00 +00:00
|
|
|
void SimpleBestFitAllocator::dump(const char* what) const
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
2010-01-26 03:00:00 +00:00
|
|
|
dump_l(what);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 03:00:00 +00:00
|
|
|
void SimpleBestFitAllocator::dump_l(const char* what) const
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
String8 result;
|
2010-01-26 03:00:00 +00:00
|
|
|
dump_l(result, what);
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%s", result.string());
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleBestFitAllocator::dump(String8& result,
|
2010-01-26 03:00:00 +00:00
|
|
|
const char* what) const
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
2010-01-26 03:00:00 +00:00
|
|
|
dump_l(result, what);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SimpleBestFitAllocator::dump_l(String8& result,
|
2010-01-26 03:00:00 +00:00
|
|
|
const char* what) const
|
2009-03-04 03:31:44 +00:00
|
|
|
{
|
|
|
|
size_t size = 0;
|
|
|
|
int32_t i = 0;
|
|
|
|
chunk_t const* cur = mList.head();
|
|
|
|
|
|
|
|
const size_t SIZE = 256;
|
|
|
|
char buffer[SIZE];
|
|
|
|
snprintf(buffer, SIZE, " %s (%p, size=%u)\n",
|
|
|
|
what, this, (unsigned int)mHeapSize);
|
|
|
|
|
|
|
|
result.append(buffer);
|
|
|
|
|
|
|
|
while (cur) {
|
|
|
|
const char* errs[] = {"", "| link bogus NP",
|
|
|
|
"| link bogus PN", "| link bogus NP+PN" };
|
|
|
|
int np = ((cur->next) && cur->next->prev != cur) ? 1 : 0;
|
|
|
|
int pn = ((cur->prev) && cur->prev->next != cur) ? 2 : 0;
|
|
|
|
|
2013-11-05 16:53:55 +00:00
|
|
|
snprintf(buffer, SIZE, " %3u: %p | 0x%08X | 0x%08X | %s %s\n",
|
|
|
|
i, cur, int(cur->start*kMemoryAlign),
|
2009-03-04 03:31:44 +00:00
|
|
|
int(cur->size*kMemoryAlign),
|
|
|
|
int(cur->free) ? "F" : "A",
|
|
|
|
errs[np|pn]);
|
|
|
|
|
|
|
|
result.append(buffer);
|
|
|
|
|
|
|
|
if (!cur->free)
|
|
|
|
size += cur->size*kMemoryAlign;
|
|
|
|
|
|
|
|
i++;
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
2010-01-26 03:00:00 +00:00
|
|
|
snprintf(buffer, SIZE,
|
|
|
|
" size allocated: %u (%u KB)\n", int(size), int(size/1024));
|
2009-03-04 03:31:44 +00:00
|
|
|
result.append(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}; // namespace android
|