@@ -78,7 +78,7 @@ class SharedPtr {
78
78
SharedPtr (T* ptr): _ptr(ptr), _counter(NULL ) {
79
79
// allocate counter on the heap so it can be shared
80
80
if (_ptr != NULL ) {
81
- _counter = ( uint32_t *) malloc ( sizeof ( uint32_t )) ;
81
+ _counter = new uint32_t ;
82
82
*_counter = 1 ;
83
83
}
84
84
}
@@ -100,9 +100,7 @@ class SharedPtr {
100
100
SharedPtr (const SharedPtr& source): _ptr(source._ptr), _counter(source._counter) {
101
101
// increment reference counter
102
102
if (_ptr != NULL ) {
103
- core_util_critical_section_enter ();
104
- (*_counter)++;
105
- core_util_critical_section_exit ();
103
+ core_util_atomic_incr_u32 (_counter, 1 );
106
104
}
107
105
}
108
106
@@ -123,9 +121,7 @@ class SharedPtr {
123
121
124
122
// increment new counter
125
123
if (_ptr != NULL ) {
126
- core_util_critical_section_enter ();
127
- (*_counter)++;
128
- core_util_critical_section_exit ();
124
+ core_util_atomic_incr_u32 (_counter, 1 );
129
125
}
130
126
}
131
127
@@ -142,7 +138,7 @@ class SharedPtr {
142
138
143
139
if (ptr != NULL ) {
144
140
// allocate counter on the heap so it can be shared
145
- _counter = ( uint32_t *) malloc ( sizeof ( uint32_t )) ;
141
+ _counter = new uint32_t ;
146
142
*_counter = 1 ;
147
143
}
148
144
}
@@ -215,17 +211,13 @@ class SharedPtr {
215
211
* @details If count reaches zero, free counter and delete object pointed to.
216
212
*/
217
213
void decrement_counter () {
218
- if (_counter) {
219
- core_util_critical_section_enter ();
220
- if (*_counter == 1 ) {
221
- core_util_critical_section_exit ();
222
- free (_counter);
214
+ if (_ptr != NULL ) {
215
+ uint32_t new_value = core_util_atomic_decr_u32 (_counter, 1 );
216
+ if (new_value == 0 ) {
217
+ delete _counter;
223
218
_counter = NULL ;
224
219
delete _ptr;
225
220
_ptr = NULL ;
226
- } else {
227
- (*_counter)--;
228
- core_util_critical_section_exit ();
229
221
}
230
222
}
231
223
}
0 commit comments