blob: c4475c7780d1382e77a39bc7e4ce88da6ed4d3db [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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#define LOG_TAG "MemoryDealer"
18
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070019#include <binder/MemoryDealer.h>
Mathias Agopian0dd0d292010-01-25 19:00:00 -080020#include <binder/IPCThreadState.h>
21#include <binder/MemoryBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022
23#include <utils/Log.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <utils/SortedVector.h>
25#include <utils/String8.h>
Mathias Agopian0dd0d292010-01-25 19:00:00 -080026#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <stdint.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <fcntl.h>
32#include <unistd.h>
33#include <errno.h>
34#include <string.h>
35
36#include <sys/stat.h>
37#include <sys/types.h>
38#include <sys/mman.h>
39#include <sys/file.h>
40
41namespace android {
Mathias Agopian83c04462009-05-22 19:00:22 -070042// ----------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043
Mathias Agopian0dd0d292010-01-25 19:00:00 -080044/*
45 * A simple templatized doubly linked-list implementation
46 */
Mathias Agopian83c04462009-05-22 19:00:22 -070047
Mathias Agopian0dd0d292010-01-25 19:00:00 -080048template <typename NODE>
49class LinkedList
50{
51 NODE* mFirst;
52 NODE* mLast;
Mathias Agopian83c04462009-05-22 19:00:22 -070053
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054public:
Yi Kongfdd8da92018-06-07 17:52:27 -070055 LinkedList() : mFirst(nullptr), mLast(nullptr) { }
56 bool isEmpty() const { return mFirst == nullptr; }
Mathias Agopian0dd0d292010-01-25 19:00:00 -080057 NODE const* head() const { return mFirst; }
58 NODE* head() { return mFirst; }
59 NODE const* tail() const { return mLast; }
60 NODE* tail() { return mLast; }
61
62 void insertAfter(NODE* node, NODE* newNode) {
63 newNode->prev = node;
64 newNode->next = node->next;
Yi Kongfdd8da92018-06-07 17:52:27 -070065 if (node->next == nullptr) mLast = newNode;
Mathias Agopian0dd0d292010-01-25 19:00:00 -080066 else node->next->prev = newNode;
67 node->next = newNode;
68 }
69
70 void insertBefore(NODE* node, NODE* newNode) {
71 newNode->prev = node->prev;
72 newNode->next = node;
Yi Kongfdd8da92018-06-07 17:52:27 -070073 if (node->prev == nullptr) mFirst = newNode;
Mathias Agopian0dd0d292010-01-25 19:00:00 -080074 else node->prev->next = newNode;
75 node->prev = newNode;
76 }
77
78 void insertHead(NODE* newNode) {
Yi Kongfdd8da92018-06-07 17:52:27 -070079 if (mFirst == nullptr) {
Mathias Agopian0dd0d292010-01-25 19:00:00 -080080 mFirst = mLast = newNode;
Yi Kongfdd8da92018-06-07 17:52:27 -070081 newNode->prev = newNode->next = nullptr;
Mathias Agopian0dd0d292010-01-25 19:00:00 -080082 } else {
Yi Kongfdd8da92018-06-07 17:52:27 -070083 newNode->prev = nullptr;
Mathias Agopian0dd0d292010-01-25 19:00:00 -080084 newNode->next = mFirst;
85 mFirst->prev = newNode;
86 mFirst = newNode;
87 }
88 }
89
90 void insertTail(NODE* newNode) {
91 if (mLast == 0) {
92 insertHead(newNode);
93 } else {
94 newNode->prev = mLast;
95 newNode->next = 0;
96 mLast->next = newNode;
97 mLast = newNode;
98 }
99 }
100
101 NODE* remove(NODE* node) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700102 if (node->prev == nullptr) mFirst = node->next;
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800103 else node->prev->next = node->next;
Yi Kongfdd8da92018-06-07 17:52:27 -0700104 if (node->next == nullptr) mLast = node->prev;
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800105 else node->next->prev = node->prev;
106 return node;
107 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108};
109
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800110// ----------------------------------------------------------------------------
111
112class Allocation : public MemoryBase {
113public:
114 Allocation(const sp<MemoryDealer>& dealer,
115 const sp<IMemoryHeap>& heap, ssize_t offset, size_t size);
116 virtual ~Allocation();
117private:
118 sp<MemoryDealer> mDealer;
119};
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120
121// ----------------------------------------------------------------------------
122
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800123class SimpleBestFitAllocator
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124{
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800125 enum {
126 PAGE_ALIGNED = 0x00000001
127 };
128public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700129 explicit SimpleBestFitAllocator(size_t size);
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800130 ~SimpleBestFitAllocator();
131
132 size_t allocate(size_t size, uint32_t flags = 0);
133 status_t deallocate(size_t offset);
134 size_t size() const;
135 void dump(const char* what) const;
136 void dump(String8& res, const char* what) const;
137
Lajos Molnar10010332016-03-17 14:29:18 -0700138 static size_t getAllocationAlignment() { return kMemoryAlign; }
139
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800140private:
141
142 struct chunk_t {
143 chunk_t(size_t start, size_t size)
Yi Kongfdd8da92018-06-07 17:52:27 -0700144 : start(start), size(size), free(1), prev(nullptr), next(nullptr) {
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800145 }
146 size_t start;
147 size_t size : 28;
148 int free : 4;
149 mutable chunk_t* prev;
150 mutable chunk_t* next;
151 };
152
153 ssize_t alloc(size_t size, uint32_t flags);
154 chunk_t* dealloc(size_t start);
155 void dump_l(const char* what) const;
156 void dump_l(String8& res, const char* what) const;
157
158 static const int kMemoryAlign;
159 mutable Mutex mLock;
160 LinkedList<chunk_t> mList;
161 size_t mHeapSize;
162};
163
164// ----------------------------------------------------------------------------
165
166Allocation::Allocation(
167 const sp<MemoryDealer>& dealer,
168 const sp<IMemoryHeap>& heap, ssize_t offset, size_t size)
169 : MemoryBase(heap, offset, size), mDealer(dealer)
170{
171#ifndef NDEBUG
172 void* const start_ptr = (void*)(intptr_t(heap->base()) + offset);
173 memset(start_ptr, 0xda, size);
174#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175}
176
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800177Allocation::~Allocation()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178{
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800179 size_t freedOffset = getOffset();
180 size_t freedSize = getSize();
181 if (freedSize) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 /* NOTE: it's VERY important to not free allocations of size 0 because
183 * they're special as they don't have any record in the allocator
184 * and could alias some real allocation (their offset is zero). */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800186 // keep the size to unmap in excess
187 size_t pagesize = getpagesize();
188 size_t start = freedOffset;
189 size_t end = start + freedSize;
190 start &= ~(pagesize-1);
191 end = (end + pagesize-1) & ~(pagesize-1);
192
193 // give back to the kernel the pages we don't need
194 size_t free_start = freedOffset;
195 size_t free_end = free_start + freedSize;
196 if (start < free_start)
197 start = free_start;
198 if (end > free_end)
199 end = free_end;
200 start = (start + pagesize-1) & ~(pagesize-1);
201 end &= ~(pagesize-1);
202
203 if (start < end) {
204 void* const start_ptr = (void*)(intptr_t(getHeap()->base()) + start);
205 size_t size = end-start;
206
207#ifndef NDEBUG
208 memset(start_ptr, 0xdf, size);
209#endif
210
211 // MADV_REMOVE is not defined on Dapper based Goobuntu
212#ifdef MADV_REMOVE
213 if (size) {
214 int err = madvise(start_ptr, size, MADV_REMOVE);
Colin Cross6f4f3ab2014-02-05 17:42:44 -0800215 ALOGW_IF(err, "madvise(%p, %zu, MADV_REMOVE) returned %s",
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800216 start_ptr, size, err<0 ? strerror(errno) : "Ok");
217 }
218#endif
219 }
Ji-Hwan Leec4cd5302011-12-15 03:53:24 +0900220
221 // This should be done after madvise(MADV_REMOVE), otherwise madvise()
222 // might kick out the memory region that's allocated and/or written
223 // right after the deallocation.
224 mDealer->deallocate(freedOffset);
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800225 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226}
227
228// ----------------------------------------------------------------------------
229
Glenn Kasten6546f2e2014-03-14 16:49:51 -0700230MemoryDealer::MemoryDealer(size_t size, const char* name, uint32_t flags)
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000231 : mHeap(sp<MemoryHeapBase>::make(size, flags, name)),
232 mAllocator(new SimpleBestFitAllocator(size)) {}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800234MemoryDealer::~MemoryDealer()
235{
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800236 delete mAllocator;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237}
238
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800239sp<IMemory> MemoryDealer::allocate(size_t size)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800240{
241 sp<IMemory> memory;
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800242 const ssize_t offset = allocator()->allocate(size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243 if (offset >= 0) {
Steven Moreland1a3a8ef2021-04-02 02:52:46 +0000244 memory = sp<Allocation>::make(sp<MemoryDealer>::fromExisting(this), heap(), offset, size);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 }
246 return memory;
247}
248
249void MemoryDealer::deallocate(size_t offset)
250{
251 allocator()->deallocate(offset);
252}
253
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800254void MemoryDealer::dump(const char* what) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800255{
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800256 allocator()->dump(what);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800257}
258
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800259const sp<IMemoryHeap>& MemoryDealer::heap() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800260 return mHeap;
261}
262
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800263SimpleBestFitAllocator* MemoryDealer::allocator() const {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800264 return mAllocator;
265}
266
Lajos Molnar10010332016-03-17 14:29:18 -0700267// static
268size_t MemoryDealer::getAllocationAlignment()
269{
270 return SimpleBestFitAllocator::getAllocationAlignment();
271}
272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800273// ----------------------------------------------------------------------------
274
275// align all the memory blocks on a cache-line boundary
276const int SimpleBestFitAllocator::kMemoryAlign = 32;
277
278SimpleBestFitAllocator::SimpleBestFitAllocator(size_t size)
279{
280 size_t pagesize = getpagesize();
281 mHeapSize = ((size + pagesize-1) & ~(pagesize-1));
282
283 chunk_t* node = new chunk_t(0, mHeapSize / kMemoryAlign);
284 mList.insertHead(node);
285}
286
287SimpleBestFitAllocator::~SimpleBestFitAllocator()
288{
289 while(!mList.isEmpty()) {
Luis A. Lozano8196d2c2017-09-19 17:33:48 -0700290 chunk_t* removed = mList.remove(mList.head());
291#ifdef __clang_analyzer__
292 // Clang static analyzer gets confused in this loop
293 // and generates a false positive warning about accessing
294 // memory that is already freed.
295 // Add an "assert" to avoid the confusion.
296 LOG_ALWAYS_FATAL_IF(mList.head() == removed);
297#endif
298 delete removed;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299 }
300}
301
302size_t SimpleBestFitAllocator::size() const
303{
304 return mHeapSize;
305}
306
307size_t SimpleBestFitAllocator::allocate(size_t size, uint32_t flags)
308{
309 Mutex::Autolock _l(mLock);
310 ssize_t offset = alloc(size, flags);
311 return offset;
312}
313
314status_t SimpleBestFitAllocator::deallocate(size_t offset)
315{
316 Mutex::Autolock _l(mLock);
317 chunk_t const * const freed = dealloc(offset);
318 if (freed) {
319 return NO_ERROR;
320 }
321 return NAME_NOT_FOUND;
322}
323
324ssize_t SimpleBestFitAllocator::alloc(size_t size, uint32_t flags)
325{
326 if (size == 0) {
327 return 0;
328 }
329 size = (size + kMemoryAlign-1) / kMemoryAlign;
Yi Kongfdd8da92018-06-07 17:52:27 -0700330 chunk_t* free_chunk = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800331 chunk_t* cur = mList.head();
332
333 size_t pagesize = getpagesize();
334 while (cur) {
335 int extra = 0;
336 if (flags & PAGE_ALIGNED)
337 extra = ( -cur->start & ((pagesize/kMemoryAlign)-1) ) ;
338
339 // best fit
340 if (cur->free && (cur->size >= (size+extra))) {
341 if ((!free_chunk) || (cur->size < free_chunk->size)) {
342 free_chunk = cur;
343 }
344 if (cur->size == size) {
345 break;
346 }
347 }
348 cur = cur->next;
349 }
350
351 if (free_chunk) {
352 const size_t free_size = free_chunk->size;
353 free_chunk->free = 0;
354 free_chunk->size = size;
355 if (free_size > size) {
356 int extra = 0;
357 if (flags & PAGE_ALIGNED)
358 extra = ( -free_chunk->start & ((pagesize/kMemoryAlign)-1) ) ;
359 if (extra) {
360 chunk_t* split = new chunk_t(free_chunk->start, extra);
361 free_chunk->start += extra;
362 mList.insertBefore(free_chunk, split);
363 }
364
Steve Blocke6f43dd2012-01-06 19:20:56 +0000365 ALOGE_IF((flags&PAGE_ALIGNED) &&
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800366 ((free_chunk->start*kMemoryAlign)&(pagesize-1)),
367 "PAGE_ALIGNED requested, but page is not aligned!!!");
368
369 const ssize_t tail_free = free_size - (size+extra);
370 if (tail_free > 0) {
371 chunk_t* split = new chunk_t(
372 free_chunk->start + free_chunk->size, tail_free);
373 mList.insertAfter(free_chunk, split);
374 }
375 }
376 return (free_chunk->start)*kMemoryAlign;
377 }
378 return NO_MEMORY;
379}
380
381SimpleBestFitAllocator::chunk_t* SimpleBestFitAllocator::dealloc(size_t start)
382{
383 start = start / kMemoryAlign;
384 chunk_t* cur = mList.head();
385 while (cur) {
386 if (cur->start == start) {
387 LOG_FATAL_IF(cur->free,
Mitch Phillipse5d85962020-07-01 10:10:21 -0700388 "block at offset 0x%08lX of size 0x%08X already freed",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800389 cur->start*kMemoryAlign, cur->size*kMemoryAlign);
390
391 // merge freed blocks together
392 chunk_t* freed = cur;
393 cur->free = 1;
394 do {
395 chunk_t* const p = cur->prev;
396 chunk_t* const n = cur->next;
397 if (p && (p->free || !cur->size)) {
398 freed = p;
399 p->size += cur->size;
400 mList.remove(cur);
401 delete cur;
402 }
403 cur = n;
404 } while (cur && cur->free);
405
406 #ifndef NDEBUG
407 if (!freed->free) {
408 dump_l("dealloc (!freed->free)");
409 }
410 #endif
411 LOG_FATAL_IF(!freed->free,
Mitch Phillipse5d85962020-07-01 10:10:21 -0700412 "freed block at offset 0x%08lX of size 0x%08X is not free!",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413 freed->start * kMemoryAlign, freed->size * kMemoryAlign);
414
415 return freed;
416 }
417 cur = cur->next;
418 }
Yi Kongfdd8da92018-06-07 17:52:27 -0700419 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800420}
421
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800422void SimpleBestFitAllocator::dump(const char* what) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800423{
424 Mutex::Autolock _l(mLock);
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800425 dump_l(what);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800426}
427
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800428void SimpleBestFitAllocator::dump_l(const char* what) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800429{
430 String8 result;
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800431 dump_l(result, what);
Steve Block9d453682011-12-20 16:23:08 +0000432 ALOGD("%s", result.string());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800433}
434
435void SimpleBestFitAllocator::dump(String8& result,
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800436 const char* what) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800437{
438 Mutex::Autolock _l(mLock);
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800439 dump_l(result, what);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800440}
441
442void SimpleBestFitAllocator::dump_l(String8& result,
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800443 const char* what) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800444{
445 size_t size = 0;
446 int32_t i = 0;
447 chunk_t const* cur = mList.head();
448
449 const size_t SIZE = 256;
450 char buffer[SIZE];
451 snprintf(buffer, SIZE, " %s (%p, size=%u)\n",
452 what, this, (unsigned int)mHeapSize);
453
454 result.append(buffer);
455
456 while (cur) {
457 const char* errs[] = {"", "| link bogus NP",
458 "| link bogus PN", "| link bogus NP+PN" };
459 int np = ((cur->next) && cur->next->prev != cur) ? 1 : 0;
460 int pn = ((cur->prev) && cur->prev->next != cur) ? 2 : 0;
461
Serban Constantinescuf683e012013-11-05 16:53:55 +0000462 snprintf(buffer, SIZE, " %3u: %p | 0x%08X | 0x%08X | %s %s\n",
463 i, cur, int(cur->start*kMemoryAlign),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800464 int(cur->size*kMemoryAlign),
465 int(cur->free) ? "F" : "A",
466 errs[np|pn]);
467
468 result.append(buffer);
469
470 if (!cur->free)
471 size += cur->size*kMemoryAlign;
472
473 i++;
474 cur = cur->next;
475 }
Mathias Agopian0dd0d292010-01-25 19:00:00 -0800476 snprintf(buffer, SIZE,
477 " size allocated: %u (%u KB)\n", int(size), int(size/1024));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478 result.append(buffer);
479}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800480
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481
Steven Moreland61ff8492019-09-26 16:05:45 -0700482} // namespace android