GNU Octave
3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Pages
liboctave
util
oct-refcount.h
Go to the documentation of this file.
1
/*
2
3
Copyright (C) 2012-2013 Jaroslav Hajek
4
5
This file is part of Octave.
6
7
Octave is free software; you can redistribute it and/or modify it
8
under the terms of the GNU General Public License as published by the
9
Free Software Foundation; either version 3 of the License, or (at your
10
option) any later version.
11
12
Octave is distributed in the hope that it will be useful, but WITHOUT
13
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15
for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with Octave; see the file COPYING. If not, see
19
<http://www.gnu.org/licenses/>.
20
21
*/
22
23
#if !defined (octave_oct_refcount_h)
24
#define octave_oct_refcount_h 1
25
26
#ifndef OCTAVE_CONFIG_INCLUDED
27
# error "The file <octave/config.h> must be included before oct-refcount.h."
28
#endif
29
30
#if defined (USE_ATOMIC_REFCOUNT) && (defined (_MSC_VER) || defined (__GNUC__))
31
# if defined (_MSC_VER)
32
# include <intrin.h>
33
# define OCTREFCOUNT_ATOMIC_INCREMENT(x) _InterlockedIncrement((long*)x)
34
# define OCTREFCOUNT_ATOMIC_DECREMENT(x) _InterlockedDecrement((long*)x)
35
# define OCTREFCOUNT_ATOMIC_INCREMENT_POST(x) _InterlockedExchangeAdd((long*)x, 1)
36
# define OCTREFCOUNT_ATOMIC_DECREMENT_POST(x) _InterlockedExchangeAdd((long*)x, -1)
37
# elif defined (__GNUC__)
38
# define OCTREFCOUNT_ATOMIC_INCREMENT(x) __sync_add_and_fetch(x, 1)
39
# define OCTREFCOUNT_ATOMIC_DECREMENT(x) __sync_add_and_fetch(x, -1)
40
# define OCTREFCOUNT_ATOMIC_INCREMENT_POST(x) __sync_fetch_and_add(x, 1)
41
# define OCTREFCOUNT_ATOMIC_DECREMENT_POST(x) __sync_fetch_and_add(x, -1)
42
# endif
43
#else // Generic non-locking versions
44
# define OCTREFCOUNT_ATOMIC_INCREMENT(x) ++(*(x))
45
# define OCTREFCOUNT_ATOMIC_DECREMENT(x) --(*(x))
46
# define OCTREFCOUNT_ATOMIC_INCREMENT_POST(x) (*(x))++
47
# define OCTREFCOUNT_ATOMIC_DECREMENT_POST(x) (*(x))--
48
#endif
49
50
// Encapsulates a reference counter.
51
template
<
class
T>
52
class
octave_refcount
53
{
54
public
:
55
typedef
T
count_type
;
56
57
octave_refcount
(
count_type
initial_count) :
count
(initial_count) { }
58
59
// Increment/Decrement. int is postfix.
60
count_type
operator++
(
void
)
61
{
62
return
OCTREFCOUNT_ATOMIC_INCREMENT
(&
count
);
63
}
64
65
count_type
operator++
(
int
)
66
{
67
return
OCTREFCOUNT_ATOMIC_INCREMENT_POST
(&
count
);
68
}
69
70
count_type
operator--
(
void
)
71
{
72
return
OCTREFCOUNT_ATOMIC_DECREMENT
(&
count
);
73
}
74
75
count_type
operator--
(
int
)
76
{
77
return
OCTREFCOUNT_ATOMIC_DECREMENT_POST
(&
count
);
78
}
79
80
operator
count_type
(
void
)
const
81
{
82
return
static_cast<
count_type
const
volatile&
>
(
count
);
83
}
84
85
count_type
*
get
(
void
)
86
{
87
return
&
count
;
88
}
89
90
private
:
91
count_type
count
;
92
};
93
94
#endif
Generated on Mon Dec 30 2013 03:04:54 for GNU Octave by
1.8.1.2