DOMArena.h (2281B)
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 "mozilla/mozalloc_oom.h" // for mozalloc_handle_oom 10 #include "mozmemory.h" 11 #include "nsISupportsImpl.h" 12 #include "nsString.h" 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::dom { 33 34 class DOMArena { 35 public: 36 friend class DocGroup; 37 explicit DOMArena(const nsACString& aLabel) { 38 nsCString label = PromiseFlatCString("DOMArena "_ns + aLabel); 39 40 arena_params_t params; 41 params.mMaxDirtyIncreaseOverride = 7; 42 params.mFlags = ARENA_FLAG_THREAD_MAIN_THREAD_ONLY; 43 params.mLabel = label.get(); 44 mArenaId = moz_create_arena_with_params(¶ms); 45 } 46 47 NS_INLINE_DECL_REFCOUNTING(DOMArena) 48 49 void* Allocate(size_t aSize) { 50 void* ret = moz_arena_malloc(mArenaId, aSize); 51 if (!ret) { 52 mozalloc_handle_oom(aSize); 53 } 54 return ret; 55 } 56 57 private: 58 ~DOMArena() { moz_dispose_arena(mArenaId); } 59 arena_id_t mArenaId; 60 }; 61 } // namespace mozilla::dom 62 #endif