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