| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#ifndef ANDROID_LINEARALLOCATOR_H |
| 27 |
#define ANDROID_LINEARALLOCATOR_H |
| 28 |
|
| 29 |
#include <stddef.h> |
| 30 |
#include <type_traits> |
| 31 |
|
| 32 |
#include <vector> |
| 33 |
|
| 34 |
namespace android { |
| 35 |
namespace uirenderer { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class LinearAllocator { |
| 44 |
public: |
| 45 |
LinearAllocator(); |
| 46 |
~LinearAllocator(); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
template <class T> |
| 60 |
void* alloc(size_t size) { |
| 61 |
static_assert(std::is_trivially_destructible<T>::value, |
| 62 |
"Error, type is non-trivial! did you mean to use create()?"); |
| 63 |
return allocImpl(size); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
template <class T, typename... Params> |
| 71 |
T* create(Params&&... params) { |
| 72 |
T* ret = new (allocImpl(sizeof(T))) T(std::forward<Params>(params)...); |
| 73 |
if (!std::is_trivially_destructible<T>::value) { |
| 74 |
auto dtor = [](void* ret) { ((T*)ret)->~T(); }; |
| 75 |
addToDestructionList(dtor, ret); |
| 76 |
} |
| 77 |
return ret; |
| 78 |
} |
| 79 |
|
| 80 |
template <class T, typename... Params> |
| 81 |
T* create_trivial(Params&&... params) { |
| 82 |
static_assert(std::is_trivially_destructible<T>::value, |
| 83 |
"Error, called create_trivial on a non-trivial type"); |
| 84 |
return new (allocImpl(sizeof(T))) T(std::forward<Params>(params)...); |
| 85 |
} |
| 86 |
|
| 87 |
template <class T> |
| 88 |
T* create_trivial_array(int count) { |
| 89 |
static_assert(std::is_trivially_destructible<T>::value, |
| 90 |
"Error, called create_trivial_array on a non-trivial type"); |
| 91 |
return reinterpret_cast<T*>(allocImpl(sizeof(T) * count)); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
void rewindIfLastAlloc(void* ptr, size_t allocSize); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
template <class T> |
| 104 |
void rewindIfLastAlloc(T* ptr) { |
| 105 |
rewindIfLastAlloc((void*)ptr, sizeof(T)); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
void dumpMemoryStats(const char* prefix = ""); |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
size_t usedSize() const { return mTotalAllocated - mWastedSpace; } |
| 118 |
size_t allocatedSize() const { return mTotalAllocated; } |
| 119 |
|
| 120 |
private: |
| 121 |
LinearAllocator(const LinearAllocator& other); |
| 122 |
|
| 123 |
class Page; |
| 124 |
typedef void (*Destructor)(void* addr); |
| 125 |
struct DestructorNode { |
| 126 |
Destructor dtor; |
| 127 |
void* addr; |
| 128 |
DestructorNode* next = nullptr; |
| 129 |
}; |
| 130 |
|
| 131 |
void* allocImpl(size_t size); |
| 132 |
|
| 133 |
void addToDestructionList(Destructor, void* addr); |
| 134 |
void runDestructorFor(void* addr); |
| 135 |
Page* newPage(size_t pageSize); |
| 136 |
bool fitsInCurrentPage(size_t size); |
| 137 |
void ensureNext(size_t size); |
| 138 |
void* start(Page* p); |
| 139 |
void* end(Page* p); |
| 140 |
|
| 141 |
size_t mPageSize; |
| 142 |
size_t mMaxAllocSize; |
| 143 |
void* mNext; |
| 144 |
Page* mCurrentPage; |
| 145 |
Page* mPages; |
| 146 |
DestructorNode* mDtorList = nullptr; |
| 147 |
|
| 148 |
|
| 149 |
size_t mTotalAllocated; |
| 150 |
size_t mWastedSpace; |
| 151 |
size_t mPageCount; |
| 152 |
size_t mDedicatedPageCount; |
| 153 |
}; |
| 154 |
|
| 155 |
template <class T> |
| 156 |
class LinearStdAllocator { |
| 157 |
public: |
| 158 |
typedef T value_type; |
| 159 |
typedef T* pointer; |
| 160 |
|
| 161 |
explicit LinearStdAllocator(LinearAllocator& allocator) : linearAllocator(allocator) {} |
| 162 |
LinearStdAllocator(const LinearStdAllocator& other) : linearAllocator(other.linearAllocator) {} |
| 163 |
~LinearStdAllocator() {} |
| 164 |
|
| 165 |
|
| 166 |
template <class U> |
| 167 |
struct rebind { |
| 168 |
typedef LinearStdAllocator<U> other; |
| 169 |
}; |
| 170 |
|
| 171 |
template <class U> |
| 172 |
LinearStdAllocator(const LinearStdAllocator<U>& other) |
| 173 |
: linearAllocator(other.linearAllocator) {} |
| 174 |
|
| 175 |
T* allocate(size_t num, const void* = 0) { |
| 176 |
return (T*)(linearAllocator.alloc<void*>(num * sizeof(T))); |
| 177 |
} |
| 178 |
|
| 179 |
void deallocate(pointer p, size_t num) { |
| 180 |
|
| 181 |
linearAllocator.rewindIfLastAlloc(p, num * sizeof(T)); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
LinearAllocator& linearAllocator; |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
template <class T1, class T2> |
| 190 |
bool operator==(const LinearStdAllocator<T1>&, const LinearStdAllocator<T2>&) { |
| 191 |
return true; |
| 192 |
} |
| 193 |
template <class T1, class T2> |
| 194 |
bool operator!=(const LinearStdAllocator<T1>&, const LinearStdAllocator<T2>&) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
template <class T> |
| 199 |
class LsaVector : public std::vector<T, LinearStdAllocator<T>> { |
| 200 |
public: |
| 201 |
explicit LsaVector(const LinearStdAllocator<T>& allocator) |
| 202 |
: std::vector<T, LinearStdAllocator<T>>(allocator) {} |
| 203 |
}; |
| 204 |
|
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
#endif |