1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=2 sw=2 et tw=78:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6  */
7 #ifndef DOM_Arena_h___
8 #define DOM_Arena_h___
9 #include "nsISupportsImpl.h"
10 #include "mozmemory.h"
11 
12 #include "mozilla/mozalloc_oom.h"  // for mozalloc_handle_oom
13 
14 #define NS_DECL_DOMARENA_DESTROY void Destroy(void);
15 
16 #define NS_IMPL_DOMARENA_DESTROY(class)                              \
17   void class ::Destroy(void) {                                       \
18     if (StaticPrefs::dom_arena_allocator_enabled_AtStartup()) {      \
19       RefPtr<nsNodeInfoManager> nim = OwnerDoc()->NodeInfoManager(); \
20       RefPtr<DOMArena> arena =                                       \
21           HasFlag(NODE_KEEPS_DOMARENA)                               \
22               ? nsContentUtils::TakeEntryFromDOMArenaTable(this)     \
23               : nullptr;                                             \
24       this->~class();                                                \
25       MOZ_ASSERT(nim, "nsNodeInfoManager needs to be initialized");  \
26       nim->Free(this);                                               \
27     } else {                                                         \
28       delete this;                                                   \
29     }                                                                \
30   }
31 
32 namespace mozilla {
33 namespace dom {
34 
35 class DOMArena {
36  public:
37   friend class DocGroup;
DOMArena()38   DOMArena() { mArenaId = moz_create_arena(); }
39 
NS_INLINE_DECL_REFCOUNTING(DOMArena)40   NS_INLINE_DECL_REFCOUNTING(DOMArena)
41 
42   void* Allocate(size_t aSize) {
43     void* ret = moz_arena_malloc(mArenaId, aSize);
44     if (!ret) {
45       mozalloc_handle_oom(aSize);
46     }
47     return ret;
48   }
49 
50  private:
~DOMArena()51   ~DOMArena() { moz_dispose_arena(mArenaId); }
52   arena_id_t mArenaId;
53 };
54 }  // namespace dom
55 }  // namespace mozilla
56 #endif
57