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// The counter is decreased each time a RefCounter to it goes out of scope
24//----------------------------------------------------------------------
25class RefCounter
26{
27public:
28    typedef uint32_t value_type;
29
30    RefCounter(value_type* ctr);
31
32    ~RefCounter();
33
34private:
35    value_type* m_counter;
36    DISALLOW_COPY_AND_ASSIGN (RefCounter);
37
38    template <class T>
39    inline T
40    increment(T* t)
41    {
42        return __sync_fetch_and_add(t, 1);
43    }
44
45    template <class T>
46    inline T
47    decrement(T* t)
48    {
49        return __sync_fetch_and_add(t, -1);
50    }
51
52};
53
54} // namespace lldb_utility
55
56#endif // #ifndef liblldb_RefCounter_h_
57