RefCounter.h revision ede7bdf4cf4f9ad1a0b8d74c715dfa45e866b8d5
1//===-- RefCounter.h --------------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef liblldb_RefCounter_h_ 11#define liblldb_RefCounter_h_ 12 13#include "lldb/lldb-public.h" 14 15namespace lldb_utility { 16 17//---------------------------------------------------------------------- 18// A simple reference counter object. You need an uint32_t* to use it 19// Once that is in place, everyone who needs to ref-count, can say 20// RefCounter ref(ptr); 21// (of course, the pointer is a shared resource, and must be accessible to 22// everyone who needs it). Synchronization is handled by RefCounter itself 23// To check if more than 1 RefCounter is attached to the same value, you can 24// either call shared(), or simply cast ref to bool 25// The counter is decreased each time a RefCounter to it goes out of scope 26//---------------------------------------------------------------------- 27class RefCounter 28{ 29public: 30 typedef uint32_t value_type; 31 32 RefCounter(value_type* ctr); 33 34 ~RefCounter(); 35 36private: 37 value_type* m_counter; 38 DISALLOW_COPY_AND_ASSIGN (RefCounter); 39 40 template <class T> 41 inline T 42 increment(T* t) 43 { 44 return __sync_fetch_and_add(t, 1); 45 } 46 47 template <class T> 48 inline T 49 decrement(T* t) 50 { 51 return __sync_fetch_and_add(t, -1); 52 } 53 54}; 55 56} // namespace lldb_utility 57 58#endif // #ifndef liblldb_RefCounter_h_ 59