layout/base/StackArena.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial