• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef foopulserefcnthfoo
2 #define foopulserefcnthfoo
3 
4 /***
5   This file is part of PulseAudio.
6 
7   Copyright 2006 Lennart Poettering
8 
9   PulseAudio is free software; you can redistribute it and/or modify
10   it under the terms of the GNU Lesser General Public License as
11   published by the Free Software Foundation; either version 2.1 of the
12   License, or (at your option) any later version.
13 
14   PulseAudio is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   General Public License for more details.
18 
19   You should have received a copy of the GNU Lesser General Public
20   License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
21 ***/
22 
23 #include <pulsecore/atomic.h>
24 #include <pulsecore/macro.h>
25 #include <pulsecore/log.h>
26 
27 /* #define DEBUG_REF */
28 
29 #define PA_REFCNT_DECLARE \
30     pa_atomic_t _ref
31 
32 #define PA_REFCNT_VALUE(p) \
33     pa_atomic_load(&(p)->_ref)
34 
35 #define PA_REFCNT_INIT_ZERO(p) \
36     pa_atomic_store(&(p)->_ref, 0)
37 
38 #ifndef DEBUG_REF
39 
40 #define PA_REFCNT_INIT(p) \
41     pa_atomic_store(&(p)->_ref, 1)
42 
43 #define PA_REFCNT_INC(p) \
44     pa_atomic_inc(&(p)->_ref)
45 
46 #define PA_REFCNT_DEC(p) \
47     (pa_atomic_dec(&(p)->_ref)-1)
48 
49 #else
50 
51 /* If you need to debug ref counting problems define DEBUG_REF and
52  * set $PULSE_LOG_BACKTRACE=5 or suchlike in the shell when running
53  * PA */
54 
55 #define PA_REFCNT_INIT(p)                       \
56     do {                                        \
57         pa_atomic_store(&(p)->_ref, 1);         \
58         pa_log("REF: Init %p", p);              \
59     } while (false)
60 
61 #define PA_REFCNT_INC(p)                        \
62     do {                                        \
63         pa_atomic_inc(&(p)->_ref);              \
64         pa_log("REF: Inc %p", p);               \
65     } while (false)                             \
66 
67 #define PA_REFCNT_DEC(p)                        \
68     ({                                          \
69         int _j = (pa_atomic_dec(&(p)->_ref)-1); \
70         if (_j <= 0)                            \
71             pa_log("REF: Done %p", p);          \
72         else                                    \
73             pa_log("REF: Dec %p", p);           \
74         _j;                                     \
75      })
76 
77 #endif
78 
79 #endif
80