1 //===-- llvm/Support/ManagedStatic.h - Static Global wrapper ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ManagedStatic class and the llvm_shutdown() function.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_MANAGED_STATIC_H
15 #define LLVM_SUPPORT_MANAGED_STATIC_H
19 /// object_deleter - Helper method for ManagedStatic.
22 void object_deleter(void *Ptr) {
26 /// ManagedStaticBase - Common base class for ManagedStatic instances.
27 class ManagedStaticBase {
29 // This should only be used as a static variable, which guarantees that this
30 // will be zero initialized.
32 mutable void (*DeleterFn)(void*);
33 mutable const ManagedStaticBase *Next;
35 void RegisterManagedStatic(void *ObjPtr, void (*deleter)(void*)) const;
37 /// isConstructed - Return true if this object has not been created yet.
38 bool isConstructed() const { return Ptr != 0; }
43 /// ManagedStatic - This transparently changes the behavior of global statics to
44 /// be lazily constructed on demand (good for reducing startup times of dynamic
45 /// libraries that link in LLVM components) and for making destruction be
46 /// explicit through the llvm_shutdown() function call.
49 class ManagedStatic : public ManagedStaticBase {
55 return *static_cast<C*>(Ptr);
59 return static_cast<C*>(Ptr);
61 const C &operator*() const {
63 return *static_cast<C*>(Ptr);
65 const C *operator->() const {
67 return static_cast<C*>(Ptr);
71 void LazyInit() const {
72 RegisterManagedStatic(new C(), object_deleter<C>);
76 template<void (*CleanupFn)(void*)>
77 class ManagedCleanup : public ManagedStaticBase {
79 void Register() { RegisterManagedStatic(0, CleanupFn); }
83 /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables.
87 /// llvm_shutdown_obj - This is a simple helper class that calls
88 /// llvm_shutdown() when it is destroyed.
89 struct llvm_shutdown_obj {
90 llvm_shutdown_obj() {}
91 ~llvm_shutdown_obj() { llvm_shutdown(); }