Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <cutils/log.h> |
| 19 | |
| 20 | #include "pmem_bestfit_alloc.h" |
| 21 | |
| 22 | |
| 23 | // align all the memory blocks on a cache-line boundary |
| 24 | const int SimpleBestFitAllocator::kMemoryAlign = 32; |
| 25 | |
| 26 | SimpleBestFitAllocator::SimpleBestFitAllocator() |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 27 | : mHeapSize(0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 28 | { |
| 29 | } |
| 30 | |
| 31 | SimpleBestFitAllocator::SimpleBestFitAllocator(size_t size) |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 32 | : mHeapSize(0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 33 | { |
| 34 | setSize(size); |
| 35 | } |
| 36 | |
| 37 | SimpleBestFitAllocator::~SimpleBestFitAllocator() |
| 38 | { |
| 39 | while(!mList.isEmpty()) { |
| 40 | delete mList.remove(mList.head()); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | ssize_t SimpleBestFitAllocator::setSize(size_t size) |
| 45 | { |
| 46 | Locker::Autolock _l(mLock); |
| 47 | if (mHeapSize != 0) return -EINVAL; |
| 48 | size_t pagesize = getpagesize(); |
| 49 | mHeapSize = ((size + pagesize-1) & ~(pagesize-1)); |
| 50 | chunk_t* node = new chunk_t(0, mHeapSize / kMemoryAlign); |
| 51 | mList.insertHead(node); |
| 52 | return size; |
| 53 | } |
| 54 | |
| 55 | size_t SimpleBestFitAllocator::size() const |
| 56 | { |
| 57 | return mHeapSize; |
| 58 | } |
| 59 | |
| 60 | ssize_t SimpleBestFitAllocator::allocate(size_t size, uint32_t flags) |
| 61 | { |
| 62 | Locker::Autolock _l(mLock); |
| 63 | if (mHeapSize == 0) return -EINVAL; |
| 64 | ssize_t offset = alloc(size, flags); |
| 65 | return offset; |
| 66 | } |
| 67 | |
| 68 | ssize_t SimpleBestFitAllocator::deallocate(size_t offset) |
| 69 | { |
| 70 | Locker::Autolock _l(mLock); |
| 71 | if (mHeapSize == 0) return -EINVAL; |
| 72 | chunk_t const * const freed = dealloc(offset); |
| 73 | if (freed) { |
| 74 | return 0; |
| 75 | } |
| 76 | return -ENOENT; |
| 77 | } |
| 78 | |
| 79 | ssize_t SimpleBestFitAllocator::alloc(size_t size, uint32_t flags) |
| 80 | { |
| 81 | if (size == 0) { |
| 82 | return 0; |
| 83 | } |
| 84 | size = (size + kMemoryAlign-1) / kMemoryAlign; |
| 85 | chunk_t* free_chunk = 0; |
| 86 | chunk_t* cur = mList.head(); |
| 87 | |
| 88 | size_t pagesize = getpagesize(); |
| 89 | while (cur) { |
| 90 | int extra = ( -cur->start & ((pagesize/kMemoryAlign)-1) ) ; |
| 91 | |
| 92 | // best fit |
| 93 | if (cur->free && (cur->size >= (size+extra))) { |
| 94 | if ((!free_chunk) || (cur->size < free_chunk->size)) { |
| 95 | free_chunk = cur; |
| 96 | } |
| 97 | if (cur->size == size) { |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | cur = cur->next; |
| 102 | } |
| 103 | |
| 104 | if (free_chunk) { |
| 105 | const size_t free_size = free_chunk->size; |
| 106 | free_chunk->free = 0; |
| 107 | free_chunk->size = size; |
| 108 | if (free_size > size) { |
| 109 | int extra = ( -free_chunk->start & ((pagesize/kMemoryAlign)-1) ) ; |
| 110 | if (extra) { |
| 111 | chunk_t* split = new chunk_t(free_chunk->start, extra); |
| 112 | free_chunk->start += extra; |
| 113 | mList.insertBefore(free_chunk, split); |
| 114 | } |
| 115 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 116 | ALOGE_IF(((free_chunk->start*kMemoryAlign)&(pagesize-1)), |
| 117 | "page is not aligned!!!"); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 118 | |
| 119 | const ssize_t tail_free = free_size - (size+extra); |
| 120 | if (tail_free > 0) { |
| 121 | chunk_t* split = new chunk_t( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 122 | free_chunk->start + free_chunk->size, tail_free); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 123 | mList.insertAfter(free_chunk, split); |
| 124 | } |
| 125 | } |
| 126 | return (free_chunk->start)*kMemoryAlign; |
| 127 | } |
| 128 | // we are out of PMEM. Print pmem stats |
| 129 | // check if there is any leak or fragmentation |
| 130 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 131 | ALOGD (" Out of PMEM. Dumping PMEM stats for debugging"); |
| 132 | ALOGD (" ------------- PRINT PMEM STATS --------------"); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 133 | |
| 134 | cur = mList.head(); |
| 135 | static uint32_t node_count; |
| 136 | static uint64_t allocated, free_space; |
| 137 | |
| 138 | while (cur) { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 139 | ALOGD (" Node %d -> Start Address : %u Size %u Free info %d",\ |
| 140 | node_count++, cur->start, cur->size, cur->free); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 141 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 142 | // if cur-> free is 1 , the node is free |
| 143 | // calculate the total allocated and total free stats also |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 144 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 145 | if (cur->free) |
| 146 | free_space += cur->size; |
| 147 | else |
| 148 | allocated += cur->size; |
| 149 | // read next node |
| 150 | cur = cur->next; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 151 | } |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 152 | ALOGD (" Total Allocated: %l Total Free: %l", allocated, free_space ); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 153 | |
| 154 | node_count = 0; |
| 155 | allocated = 0; |
| 156 | free_space = 0; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 157 | ALOGD ("----------------------------------------------"); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 158 | return -ENOMEM; |
| 159 | } |
| 160 | |
| 161 | SimpleBestFitAllocator::chunk_t* SimpleBestFitAllocator::dealloc(size_t start) |
| 162 | { |
| 163 | start = start / kMemoryAlign; |
| 164 | chunk_t* cur = mList.head(); |
| 165 | while (cur) { |
| 166 | if (cur->start == start) { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 167 | ALOG_FATAL_IF(cur->free, |
| 168 | "block at offset 0x%08lX of size 0x%08lX already freed", |
| 169 | cur->start*kMemoryAlign, cur->size*kMemoryAlign); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 170 | |
| 171 | // merge freed blocks together |
| 172 | chunk_t* freed = cur; |
| 173 | cur->free = 1; |
| 174 | do { |
| 175 | chunk_t* const p = cur->prev; |
| 176 | chunk_t* const n = cur->next; |
| 177 | if (p && (p->free || !cur->size)) { |
| 178 | freed = p; |
| 179 | p->size += cur->size; |
| 180 | mList.remove(cur); |
| 181 | delete cur; |
| 182 | } |
| 183 | cur = n; |
| 184 | } while (cur && cur->free); |
| 185 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame^] | 186 | ALOG_FATAL_IF(!freed->free, |
| 187 | "freed block at offset 0x%08lX of size 0x%08lX is not free!", |
| 188 | freed->start * kMemoryAlign, freed->size * kMemoryAlign); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 189 | |
| 190 | return freed; |
| 191 | } |
| 192 | cur = cur->next; |
| 193 | } |
| 194 | return 0; |
| 195 | } |