blob: e6a4c03156b440dfcc79af6da1aea2401a401139 [file] [log] [blame]
John Reck1ed72372015-04-23 15:45:54 -07001/*
2 * Copyright 2012, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#define LOG_NDEBUG 1
27
28#include "utils/LinearAllocator.h"
29
30#include <stdlib.h>
31#include <utils/Log.h>
32
33
34// The ideal size of a page allocation (these need to be multiples of 8)
John Reckafb05212015-07-30 14:13:22 -070035#define INITIAL_PAGE_SIZE ((size_t)512) // 512b
John Reck1ed72372015-04-23 15:45:54 -070036#define MAX_PAGE_SIZE ((size_t)131072) // 128kb
37
38// The maximum amount of wasted space we can have per page
39// Allocations exceeding this will have their own dedicated page
40// If this is too low, we will malloc too much
41// Too high, and we may waste too much space
42// Must be smaller than INITIAL_PAGE_SIZE
John Reckafb05212015-07-30 14:13:22 -070043#define MAX_WASTE_RATIO (0.5f)
John Reck1ed72372015-04-23 15:45:54 -070044
45#if ALIGN_DOUBLE
46#define ALIGN_SZ (sizeof(double))
47#else
48#define ALIGN_SZ (sizeof(int))
49#endif
50
51#define ALIGN(x) ((x + ALIGN_SZ - 1 ) & ~(ALIGN_SZ - 1))
52#define ALIGN_PTR(p) ((void*)(ALIGN((size_t)p)))
53
54#if LOG_NDEBUG
Chris Craik81a1d2a2015-10-15 17:13:00 -070055#define ADD_ALLOCATION()
56#define RM_ALLOCATION()
John Reck1ed72372015-04-23 15:45:54 -070057#else
58#include <utils/Thread.h>
59#include <utils/Timers.h>
60static size_t s_totalAllocations = 0;
61static nsecs_t s_nextLog = 0;
62static android::Mutex s_mutex;
63
64static void _logUsageLocked() {
65 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
66 if (now > s_nextLog) {
67 s_nextLog = now + milliseconds_to_nanoseconds(10);
Chris Craik81a1d2a2015-10-15 17:13:00 -070068 ALOGV("Total pages allocated: %zu", s_totalAllocations);
John Reck1ed72372015-04-23 15:45:54 -070069 }
70}
71
Chris Craik81a1d2a2015-10-15 17:13:00 -070072static void _addAllocation(int count) {
John Reck1ed72372015-04-23 15:45:54 -070073 android::AutoMutex lock(s_mutex);
Chris Craik81a1d2a2015-10-15 17:13:00 -070074 s_totalAllocations += count;
John Reck1ed72372015-04-23 15:45:54 -070075 _logUsageLocked();
76}
77
Chris Craik81a1d2a2015-10-15 17:13:00 -070078#define ADD_ALLOCATION(size) _addAllocation(1);
79#define RM_ALLOCATION(size) _addAllocation(-1);
John Reck1ed72372015-04-23 15:45:54 -070080#endif
81
82#define min(x,y) (((x) < (y)) ? (x) : (y))
83
John Reckb5bc4542015-04-23 15:51:55 -070084void* operator new(std::size_t size, android::uirenderer::LinearAllocator& la) {
85 return la.alloc(size);
86}
87
John Reck1ed72372015-04-23 15:45:54 -070088namespace android {
89namespace uirenderer {
90
91class LinearAllocator::Page {
92public:
93 Page* next() { return mNextPage; }
94 void setNext(Page* next) { mNextPage = next; }
95
96 Page()
97 : mNextPage(0)
98 {}
99
100 void* operator new(size_t /*size*/, void* buf) { return buf; }
101
102 void* start() {
103 return (void*) (((size_t)this) + sizeof(Page));
104 }
105
106 void* end(int pageSize) {
107 return (void*) (((size_t)start()) + pageSize);
108 }
109
110private:
111 Page(const Page& /*other*/) {}
112 Page* mNextPage;
113};
114
115LinearAllocator::LinearAllocator()
116 : mPageSize(INITIAL_PAGE_SIZE)
John Reckafb05212015-07-30 14:13:22 -0700117 , mMaxAllocSize(INITIAL_PAGE_SIZE * MAX_WASTE_RATIO)
John Reck1ed72372015-04-23 15:45:54 -0700118 , mNext(0)
119 , mCurrentPage(0)
120 , mPages(0)
121 , mTotalAllocated(0)
122 , mWastedSpace(0)
123 , mPageCount(0)
124 , mDedicatedPageCount(0) {}
125
126LinearAllocator::~LinearAllocator(void) {
John Reckb5bc4542015-04-23 15:51:55 -0700127 while (mDtorList) {
128 auto node = mDtorList;
129 mDtorList = node->next;
130 node->dtor(node->addr);
131 }
John Reck1ed72372015-04-23 15:45:54 -0700132 Page* p = mPages;
133 while (p) {
134 Page* next = p->next();
135 p->~Page();
136 free(p);
Chris Craik81a1d2a2015-10-15 17:13:00 -0700137 RM_ALLOCATION();
John Reck1ed72372015-04-23 15:45:54 -0700138 p = next;
139 }
140}
141
142void* LinearAllocator::start(Page* p) {
Chris Craik25c8d5b2015-09-02 16:20:56 -0700143 return ALIGN_PTR((size_t)p + sizeof(Page));
John Reck1ed72372015-04-23 15:45:54 -0700144}
145
146void* LinearAllocator::end(Page* p) {
147 return ((char*)p) + mPageSize;
148}
149
150bool LinearAllocator::fitsInCurrentPage(size_t size) {
151 return mNext && ((char*)mNext + size) <= end(mCurrentPage);
152}
153
154void LinearAllocator::ensureNext(size_t size) {
155 if (fitsInCurrentPage(size)) return;
156
157 if (mCurrentPage && mPageSize < MAX_PAGE_SIZE) {
158 mPageSize = min(MAX_PAGE_SIZE, mPageSize * 2);
John Reckafb05212015-07-30 14:13:22 -0700159 mMaxAllocSize = mPageSize * MAX_WASTE_RATIO;
John Reck1ed72372015-04-23 15:45:54 -0700160 mPageSize = ALIGN(mPageSize);
161 }
162 mWastedSpace += mPageSize;
163 Page* p = newPage(mPageSize);
164 if (mCurrentPage) {
165 mCurrentPage->setNext(p);
166 }
167 mCurrentPage = p;
168 if (!mPages) {
169 mPages = mCurrentPage;
170 }
171 mNext = start(mCurrentPage);
172}
173
174void* LinearAllocator::alloc(size_t size) {
175 size = ALIGN(size);
176 if (size > mMaxAllocSize && !fitsInCurrentPage(size)) {
177 ALOGV("Exceeded max size %zu > %zu", size, mMaxAllocSize);
178 // Allocation is too large, create a dedicated page for the allocation
179 Page* page = newPage(size);
180 mDedicatedPageCount++;
181 page->setNext(mPages);
182 mPages = page;
183 if (!mCurrentPage)
184 mCurrentPage = mPages;
185 return start(page);
186 }
187 ensureNext(size);
188 void* ptr = mNext;
189 mNext = ((char*)mNext) + size;
190 mWastedSpace -= size;
191 return ptr;
192}
193
John Reckb5bc4542015-04-23 15:51:55 -0700194void LinearAllocator::addToDestructionList(Destructor dtor, void* addr) {
195 static_assert(std::is_standard_layout<DestructorNode>::value,
196 "DestructorNode must have standard layout");
197 static_assert(std::is_trivially_destructible<DestructorNode>::value,
198 "DestructorNode must be trivially destructable");
199 auto node = new (*this) DestructorNode();
200 node->dtor = dtor;
201 node->addr = addr;
202 node->next = mDtorList;
203 mDtorList = node;
204}
205
206void LinearAllocator::runDestructorFor(void* addr) {
207 auto node = mDtorList;
208 DestructorNode* previous = nullptr;
209 while (node) {
210 if (node->addr == addr) {
211 if (previous) {
212 previous->next = node->next;
213 } else {
214 mDtorList = node->next;
215 }
216 node->dtor(node->addr);
217 rewindIfLastAlloc(node, sizeof(DestructorNode));
218 break;
219 }
220 previous = node;
221 node = node->next;
222 }
223}
224
John Reck1ed72372015-04-23 15:45:54 -0700225void LinearAllocator::rewindIfLastAlloc(void* ptr, size_t allocSize) {
John Reckb5bc4542015-04-23 15:51:55 -0700226 // First run the destructor as running the destructor will
227 // also rewind for the DestructorNode allocation which will
228 // have been allocated after this void* if it has a destructor
229 runDestructorFor(ptr);
John Reck1ed72372015-04-23 15:45:54 -0700230 // Don't bother rewinding across pages
231 allocSize = ALIGN(allocSize);
232 if (ptr >= start(mCurrentPage) && ptr < end(mCurrentPage)
233 && ptr == ((char*)mNext - allocSize)) {
John Reck1ed72372015-04-23 15:45:54 -0700234 mWastedSpace += allocSize;
235 mNext = ptr;
236 }
237}
238
239LinearAllocator::Page* LinearAllocator::newPage(size_t pageSize) {
240 pageSize = ALIGN(pageSize + sizeof(LinearAllocator::Page));
Chris Craik81a1d2a2015-10-15 17:13:00 -0700241 ADD_ALLOCATION();
John Reck1ed72372015-04-23 15:45:54 -0700242 mTotalAllocated += pageSize;
243 mPageCount++;
244 void* buf = malloc(pageSize);
245 return new (buf) Page();
246}
247
248static const char* toSize(size_t value, float& result) {
249 if (value < 2000) {
250 result = value;
251 return "B";
252 }
253 if (value < 2000000) {
254 result = value / 1024.0f;
255 return "KB";
256 }
257 result = value / 1048576.0f;
258 return "MB";
259}
260
261void LinearAllocator::dumpMemoryStats(const char* prefix) {
262 float prettySize;
263 const char* prettySuffix;
264 prettySuffix = toSize(mTotalAllocated, prettySize);
265 ALOGD("%sTotal allocated: %.2f%s", prefix, prettySize, prettySuffix);
266 prettySuffix = toSize(mWastedSpace, prettySize);
267 ALOGD("%sWasted space: %.2f%s (%.1f%%)", prefix, prettySize, prettySuffix,
268 (float) mWastedSpace / (float) mTotalAllocated * 100.0f);
269 ALOGD("%sPages %zu (dedicated %zu)", prefix, mPageCount, mDedicatedPageCount);
270}
271
272}; // namespace uirenderer
273}; // namespace android