Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #ifndef StackArena_h |
michael@0 | 6 | #define StackArena_h |
michael@0 | 7 | |
michael@0 | 8 | #include "nsError.h" |
michael@0 | 9 | #include "mozilla/Assertions.h" |
michael@0 | 10 | #include "mozilla/MemoryReporting.h" |
michael@0 | 11 | #include "mozilla/NullPtr.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | |
michael@0 | 15 | struct StackBlock; |
michael@0 | 16 | struct StackMark; |
michael@0 | 17 | class AutoStackArena; |
michael@0 | 18 | |
michael@0 | 19 | // Private helper class for AutoStackArena. |
michael@0 | 20 | class StackArena { |
michael@0 | 21 | private: |
michael@0 | 22 | friend class AutoStackArena; |
michael@0 | 23 | StackArena(); |
michael@0 | 24 | ~StackArena(); |
michael@0 | 25 | |
michael@0 | 26 | nsresult Init() { return mBlocks ? NS_OK : NS_ERROR_OUT_OF_MEMORY; } |
michael@0 | 27 | |
michael@0 | 28 | // Memory management functions. |
michael@0 | 29 | void* Allocate(size_t aSize); |
michael@0 | 30 | void Push(); |
michael@0 | 31 | void Pop(); |
michael@0 | 32 | |
michael@0 | 33 | size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; |
michael@0 | 34 | |
michael@0 | 35 | // Our current position in memory. |
michael@0 | 36 | size_t mPos; |
michael@0 | 37 | |
michael@0 | 38 | // A list of memory blocks. Usually there is only one |
michael@0 | 39 | // but if we overrun our stack size we can get more memory. |
michael@0 | 40 | StackBlock* mBlocks; |
michael@0 | 41 | |
michael@0 | 42 | // The current block. |
michael@0 | 43 | StackBlock* mCurBlock; |
michael@0 | 44 | |
michael@0 | 45 | // Our stack of mark where push has been called. |
michael@0 | 46 | StackMark* mMarks; |
michael@0 | 47 | |
michael@0 | 48 | // The current top of the mark list. |
michael@0 | 49 | uint32_t mStackTop; |
michael@0 | 50 | |
michael@0 | 51 | // The size of the mark array. |
michael@0 | 52 | uint32_t mMarkLength; |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | // Class for stack scoped arena memory allocations. |
michael@0 | 56 | // |
michael@0 | 57 | // Callers who wish to allocate memory whose lifetime corresponds to the |
michael@0 | 58 | // lifetime of a stack-allocated object can use this class. First, |
michael@0 | 59 | // declare an AutoStackArena object on the stack. Then all subsequent |
michael@0 | 60 | // calls to Allocate will allocate memory from an arena pool that will |
michael@0 | 61 | // be freed when that variable goes out of scope. Nesting is allowed. |
michael@0 | 62 | // |
michael@0 | 63 | // Individual allocations cannot exceed StackBlock::MAX_USABLE_SIZE |
michael@0 | 64 | // bytes. |
michael@0 | 65 | // |
michael@0 | 66 | class MOZ_STACK_CLASS AutoStackArena { |
michael@0 | 67 | public: |
michael@0 | 68 | AutoStackArena() |
michael@0 | 69 | : mOwnsStackArena(false) |
michael@0 | 70 | { |
michael@0 | 71 | if (!gStackArena) { |
michael@0 | 72 | gStackArena = new StackArena(); |
michael@0 | 73 | mOwnsStackArena = true; |
michael@0 | 74 | gStackArena->Init(); |
michael@0 | 75 | } |
michael@0 | 76 | gStackArena->Push(); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | ~AutoStackArena() { |
michael@0 | 80 | gStackArena->Pop(); |
michael@0 | 81 | if (mOwnsStackArena) { |
michael@0 | 82 | delete gStackArena; |
michael@0 | 83 | gStackArena = nullptr; |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | static void* Allocate(size_t aSize) { |
michael@0 | 88 | return gStackArena->Allocate(aSize); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | private: |
michael@0 | 92 | static StackArena* gStackArena; |
michael@0 | 93 | bool mOwnsStackArena; |
michael@0 | 94 | }; |
michael@0 | 95 | |
michael@0 | 96 | } // namespace mozilla |
michael@0 | 97 | |
michael@0 | 98 | #endif |