Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 1 | //===-- ManagedStatic.cpp - Static Global wrapper -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ManagedStatic class and llvm_shutdown(). |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/ManagedStatic.h" |
| 15 | #include <cassert> |
| 16 | using namespace llvm; |
| 17 | |
| 18 | static const ManagedStaticBase *StaticList = 0; |
| 19 | |
| 20 | void ManagedStaticBase::RegisterManagedStatic(void *ObjPtr, |
| 21 | void (*Deleter)(void*)) const { |
| 22 | assert(Ptr == 0 && DeleterFn == 0 && Next == 0 && |
| 23 | "Partially init static?"); |
| 24 | Ptr = ObjPtr; |
| 25 | DeleterFn = Deleter; |
| 26 | |
| 27 | // Add to list of managed statics. |
| 28 | Next = StaticList; |
| 29 | StaticList = this; |
| 30 | } |
| 31 | |
| 32 | void ManagedStaticBase::destroy() const { |
Chris Lattner | d283566 | 2007-02-20 06:18:57 +0000 | [diff] [blame] | 33 | assert(DeleterFn && "ManagedStatic not initialized correctly!"); |
Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 34 | assert(StaticList == this && |
| 35 | "Not destroyed in reverse order of construction?"); |
| 36 | // Unlink from list. |
| 37 | StaticList = Next; |
| 38 | Next = 0; |
| 39 | |
| 40 | // Destroy memory. |
| 41 | DeleterFn(Ptr); |
| 42 | |
| 43 | // Cleanup. |
| 44 | Ptr = 0; |
| 45 | DeleterFn = 0; |
| 46 | } |
| 47 | |
| 48 | /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables. |
Chris Lattner | 151880b | 2006-09-29 18:43:14 +0000 | [diff] [blame] | 49 | void llvm::llvm_shutdown() { |
Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 50 | while (StaticList) |
| 51 | StaticList->destroy(); |
| 52 | } |
| 53 | |