Mathias Agopian | 8c4ab1f | 2009-06-11 16:32:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | #include <cutils/log.h> |
| 18 | |
| 19 | #include "allocator.h" |
| 20 | |
| 21 | |
| 22 | // align all the memory blocks on a cache-line boundary |
| 23 | const int SimpleBestFitAllocator::kMemoryAlign = 32; |
| 24 | |
| 25 | SimpleBestFitAllocator::SimpleBestFitAllocator(size_t size) |
| 26 | { |
| 27 | size_t pagesize = getpagesize(); |
| 28 | mHeapSize = ((size + pagesize-1) & ~(pagesize-1)); |
| 29 | |
| 30 | chunk_t* node = new chunk_t(0, mHeapSize / kMemoryAlign); |
| 31 | mList.insertHead(node); |
| 32 | } |
| 33 | |
| 34 | SimpleBestFitAllocator::~SimpleBestFitAllocator() |
| 35 | { |
| 36 | while(!mList.isEmpty()) { |
| 37 | delete mList.remove(mList.head()); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | size_t SimpleBestFitAllocator::size() const |
| 42 | { |
| 43 | return mHeapSize; |
| 44 | } |
| 45 | |
| 46 | size_t SimpleBestFitAllocator::allocate(size_t size, uint32_t flags) |
| 47 | { |
| 48 | Locker::Autolock _l(mLock); |
| 49 | ssize_t offset = alloc(size, flags); |
| 50 | return offset; |
| 51 | } |
| 52 | |
| 53 | ssize_t SimpleBestFitAllocator::deallocate(size_t offset) |
| 54 | { |
| 55 | Locker::Autolock _l(mLock); |
| 56 | chunk_t const * const freed = dealloc(offset); |
| 57 | if (freed) { |
| 58 | return 0; |
| 59 | } |
| 60 | return -ENOENT; |
| 61 | } |
| 62 | |
| 63 | ssize_t SimpleBestFitAllocator::alloc(size_t size, uint32_t flags) |
| 64 | { |
| 65 | if (size == 0) { |
| 66 | return 0; |
| 67 | } |
| 68 | size = (size + kMemoryAlign-1) / kMemoryAlign; |
| 69 | chunk_t* free_chunk = 0; |
| 70 | chunk_t* cur = mList.head(); |
| 71 | |
| 72 | size_t pagesize = getpagesize(); |
| 73 | while (cur) { |
| 74 | int extra = ( -cur->start & ((pagesize/kMemoryAlign)-1) ) ; |
| 75 | |
| 76 | // best fit |
| 77 | if (cur->free && (cur->size >= (size+extra))) { |
| 78 | if ((!free_chunk) || (cur->size < free_chunk->size)) { |
| 79 | free_chunk = cur; |
| 80 | } |
| 81 | if (cur->size == size) { |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | cur = cur->next; |
| 86 | } |
| 87 | |
| 88 | if (free_chunk) { |
| 89 | const size_t free_size = free_chunk->size; |
| 90 | free_chunk->free = 0; |
| 91 | free_chunk->size = size; |
| 92 | if (free_size > size) { |
| 93 | int extra = ( -free_chunk->start & ((pagesize/kMemoryAlign)-1) ) ; |
| 94 | if (extra) { |
| 95 | chunk_t* split = new chunk_t(free_chunk->start, extra); |
| 96 | free_chunk->start += extra; |
| 97 | mList.insertBefore(free_chunk, split); |
| 98 | } |
| 99 | |
| 100 | LOGE_IF(((free_chunk->start*kMemoryAlign)&(pagesize-1)), |
| 101 | "page is not aligned!!!"); |
| 102 | |
| 103 | const ssize_t tail_free = free_size - (size+extra); |
| 104 | if (tail_free > 0) { |
| 105 | chunk_t* split = new chunk_t( |
| 106 | free_chunk->start + free_chunk->size, tail_free); |
| 107 | mList.insertAfter(free_chunk, split); |
| 108 | } |
| 109 | } |
| 110 | return (free_chunk->start)*kMemoryAlign; |
| 111 | } |
| 112 | return -ENOMEM; |
| 113 | } |
| 114 | |
| 115 | SimpleBestFitAllocator::chunk_t* SimpleBestFitAllocator::dealloc(size_t start) |
| 116 | { |
| 117 | start = start / kMemoryAlign; |
| 118 | chunk_t* cur = mList.head(); |
| 119 | while (cur) { |
| 120 | if (cur->start == start) { |
| 121 | LOG_FATAL_IF(cur->free, |
| 122 | "block at offset 0x%08lX of size 0x%08lX already freed", |
| 123 | cur->start*kMemoryAlign, cur->size*kMemoryAlign); |
| 124 | |
| 125 | // merge freed blocks together |
| 126 | chunk_t* freed = cur; |
| 127 | cur->free = 1; |
| 128 | do { |
| 129 | chunk_t* const p = cur->prev; |
| 130 | chunk_t* const n = cur->next; |
| 131 | if (p && (p->free || !cur->size)) { |
| 132 | freed = p; |
| 133 | p->size += cur->size; |
| 134 | mList.remove(cur); |
| 135 | delete cur; |
| 136 | } |
| 137 | cur = n; |
| 138 | } while (cur && cur->free); |
| 139 | |
| 140 | #ifndef NDEBUG |
| 141 | if (!freed->free) { |
| 142 | dump_l("dealloc (!freed->free)"); |
| 143 | } |
| 144 | #endif |
| 145 | LOG_FATAL_IF(!freed->free, |
| 146 | "freed block at offset 0x%08lX of size 0x%08lX is not free!", |
| 147 | freed->start * kMemoryAlign, freed->size * kMemoryAlign); |
| 148 | |
| 149 | return freed; |
| 150 | } |
| 151 | cur = cur->next; |
| 152 | } |
| 153 | return 0; |
| 154 | } |