Freestanding.h (2103B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 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 https://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_freestanding_Freestanding_h 8 #define mozilla_freestanding_Freestanding_h 9 10 /** 11 * This header is automatically included in all source code residing in the 12 * /browser/app/winlauncher/freestanding directory. 13 */ 14 15 #if defined(__STDC_HOSTED__) && __STDC_HOSTED__ == 1 16 # error "This header should only be included by freestanding code" 17 #endif // defined(__STDC_HOSTED__) && __STDC_HOSTED__ == 1 18 19 #define MOZ_USE_LAUNCHER_ERROR 20 #include "mozilla/NativeNt.h" 21 22 namespace mozilla { 23 namespace freestanding { 24 25 /** 26 * Since this library is the only part of firefox.exe that needs special 27 * treatment with respect to the heap, we implement |RtlNew| and |RtlDelete| 28 * to be used instead of |new| and |delete| for any heap allocations inside 29 * the freestanding library. 30 */ 31 template <typename T, typename... Args> 32 inline static T* RtlNew(Args&&... aArgs) { 33 HANDLE processHeap = nt::RtlGetProcessHeap(); 34 if (!processHeap) { 35 // Handle the case where the process heap is not initialized because 36 // passing nullptr to RtlAllocateHeap crashes the process. 37 return nullptr; 38 } 39 40 void* ptr = ::RtlAllocateHeap(processHeap, 0, sizeof(T)); 41 if (!ptr) { 42 return nullptr; 43 } 44 45 return new (ptr) T(std::forward<Args>(aArgs)...); 46 } 47 48 template <typename T> 49 inline static void RtlDelete(T* aPtr) { 50 if (!aPtr) { 51 return; 52 } 53 54 aPtr->~T(); 55 ::RtlFreeHeap(nt::RtlGetProcessHeap(), 0, aPtr); 56 } 57 58 } // namespace freestanding 59 } // namespace mozilla 60 61 // Initialization code for all statically-allocated data in freestanding is 62 // placed into a separate section. This allows us to initialize any 63 // freestanding statics without needing to initialize everything else in this 64 // binary. 65 #pragma init_seg(".freestd$g") 66 67 #endif // mozilla_freestanding_Freestanding_h