1 /* 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 #ifndef ANDROID_LINEARALLOCATOR_H 27 #define ANDROID_LINEARALLOCATOR_H 28 29 #include <stddef.h> 30 31 namespace android { 32 33 /** 34 * A memory manager that internally allocates multi-kbyte buffers for placing objects in. It avoids 35 * the overhead of malloc when many objects are allocated. It is most useful when creating many 36 * small objects with a similar lifetime, and doesn't add significant overhead for large 37 * allocations. 38 */ 39 class LinearAllocator { 40 public: 41 LinearAllocator(); 42 ~LinearAllocator(); 43 44 /** 45 * Reserves and returns a region of memory of at least size 'size', aligning as needed. 46 * Typically this is used in an object's overridden new() method or as a replacement for malloc. 47 * 48 * The lifetime of the returned buffers is tied to that of the LinearAllocator. If calling 49 * delete() on an object stored in a buffer is needed, it should be overridden to use 50 * rewindIfLastAlloc() 51 */ 52 void* alloc(size_t size); 53 54 /** 55 * Attempt to deallocate the given buffer, with the LinearAllocator attempting to rewind its 56 * state if possible. No destructors are called. 57 */ 58 void rewindIfLastAlloc(void* ptr, size_t allocSize); 59 60 /** 61 * Dump memory usage statistics to the log (allocated and wasted space) 62 */ 63 void dumpMemoryStats(const char* prefix = ""); 64 65 /** 66 * The number of bytes used for buffers allocated in the LinearAllocator (does not count space 67 * wasted) 68 */ 69 size_t usedSize() const { return mTotalAllocated - mWastedSpace; } 70 71 private: 72 LinearAllocator(const LinearAllocator& other); 73 74 class Page; 75 76 Page* newPage(size_t pageSize); 77 bool fitsInCurrentPage(size_t size); 78 void ensureNext(size_t size); 79 void* start(Page *p); 80 void* end(Page* p); 81 82 size_t mPageSize; 83 size_t mMaxAllocSize; 84 void* mNext; 85 Page* mCurrentPage; 86 Page* mPages; 87 88 // Memory usage tracking 89 size_t mTotalAllocated; 90 size_t mWastedSpace; 91 size_t mPageCount; 92 size_t mDedicatedPageCount; 93 }; 94 95 }; // namespace android 96 97 #endif // ANDROID_LINEARALLOCATOR_H 98